在 Sas 中计算每月每个观察的变量

发布于 2024-08-27 21:58:44 字数 1294 浏览 4 评论 0原文

一个简单的问题,我有以下类型的数据:

Ticker _ Date _ Fem Analyst(如果为真则虚拟 1) ___ 该月的变量,如 beta

AA _ 01/04/2001 _< /strong> 1 ___ 0.61

AA _ 05/04/2001 _ 1 ___ 0.62

AA _ 08/04/2001 _ 1 ___ 0.63

AA _ 01/05 /2002 _ 1 ___ 0.7

AA _ 04/05/2002 _ 1 ___ 0.71

AA _ 08/07/2002 _ 0 ___ 0.8

AA _ 07/04/2003 _ 1 ___ 0.4

等等.. 我想要收到以下内容:

股票代码 _ 日期 数量女性分析师 男性分析师数量 _ 总 ___变量

AA _ 04/2001 3 0 _ 3 ___ 0.63

AA _ 05/2002 2 < /strong> 0 _ 2 ___ 0.71

AA _ 07/2002 0 < /em> 1 _ 1 ___ 0.8

AA _ 04/2003 1 < strong> 0 _ 1 ___ 0.4

因此,计数算法允许我计算某个公司每月的女性和男性分析师数量(使用虚拟变量性别 0 或 1),并删除该月的所有观察结果,除了最近的一个(例如,对于 08/04/01,这变为 04/01,值为 0.63,这是 AA 公司 04/01 的 beta 的最新观察结果)我猜这个例子解释了这一切?

有什么想法吗?

a quick question, I have data of the following sort:

Ticker _ Date _ Fem Analyst (dummy 1 if true) ___ Variables of that month like beta

AA _ 01/04/2001 _ 1 ___ 0.61

AA _ 05/04/2001 _ 1 ___ 0.62

AA _ 08/04/2001 _ 1 ___ 0.63

AA _ 01/05/2002 _ 1 ___ 0.7

AA _ 04/05/2002 _ 1 ___ 0.71

AA _ 08/07/2002 _ 0 ___ 0.8

AA _ 07/04/2003 _ 1 ___ 0.4

and so on.. What I want to receive is the following:

Ticker _ Date Number of fem analyst Number of Male Analysts _ Total ___Variables

AA _ 04/2001 3 0 _ 3 ___ 0.63

AA _ 05/2002 2 0 _ 2 ___ 0.71

AA _ 07/2002 0 1 _ 1 ___ 0.8

AA _ 04/2003 1 0 _ 1 ___ 0.4

So a counting algorithm that allows me to count the number of female and male analyst for a certain company per month( using dummy variable gender 0 or 1) and deletes all observations for that month except the most recent one (for instance for 08/04/01 this becomes 04/01 with 0.63 which is the most recent observation for beta for 04/01 for company AA) The example explains it all I guess?

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等数载,海棠开 2024-09-03 21:58:44

您可能想要这样的东西:

/* Create the month variable into a string YYYY/MM */
data analysts0;
    set <your data>;
    format month $7.;
    month=cats(year(date),'/',put(month(date),z2.));
run;
/* Sort so you can do the by processing required for counting */
proc sort data=analyst0 out=analyst1;
    /* You need to include the date in the sort so the most recent is last */
    by ticker month date;
run;
/* Count */
data count;
    retain n_fem n_male 0;
    set analyst1;
    by ticker month;
    if first.ticker of first.month then do;
        n_fem=0;
        n_male=0;
    end;
    else do;
       if gender=1 then n_fem+1;
       else if gender=0 then n_male+1;
       else put 'Huh?';
    end;
    /* this outputs only the values you need.*/
    if last.ticker or last.month then output;
run;

这应该给您一个总体思路 - 我现在无法访问 SAS,因此无法检查代码。有关更多详细信息,请参阅数据步骤中保留和处理的文档。

You may want something like this:

/* Create the month variable into a string YYYY/MM */
data analysts0;
    set <your data>;
    format month $7.;
    month=cats(year(date),'/',put(month(date),z2.));
run;
/* Sort so you can do the by processing required for counting */
proc sort data=analyst0 out=analyst1;
    /* You need to include the date in the sort so the most recent is last */
    by ticker month date;
run;
/* Count */
data count;
    retain n_fem n_male 0;
    set analyst1;
    by ticker month;
    if first.ticker of first.month then do;
        n_fem=0;
        n_male=0;
    end;
    else do;
       if gender=1 then n_fem+1;
       else if gender=0 then n_male+1;
       else put 'Huh?';
    end;
    /* this outputs only the values you need.*/
    if last.ticker or last.month then output;
run;

This should give you the general idea - I don't have access to SAS right now so I can't check the code. See the documentation for retain and by processing in the data step for more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文