全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
2941 15
2012-09-03
悬赏 2000 个论坛币 已解决
这是朋友的来信,不过我有7,8年没用过SAS,语法都忘光了,只能求助于各位朋友了。下面是朋友的来信:

---------------------------------------------------

Can I seek your help on SAS PROC MEAN?



Share

Turnover

Date

1

54

2000Q1

1

34

2000Q1

1

64

2000Q1

1

12

2000Q1

1

34

2000Q2

1

22

2000Q2

1

11

2000Q2

1

55

2000Q2

2

31

2000Q1

2

42

2000Q1

2

52

2000Q1

2

53

2000Q1

2

86

2000Q2

2

42

2000Q2

2

32

2000Q2

2

12

2000Q2




What I need is I want to first find the average for each stock for each period.



Then I want to average the stock's turnover average for each period.



As an example, I first need to know stock's 1 turnover average for 2000 Q1.


= (54+34+64+12)/4


= 41



Then I need to know stock's 2 turnover average for 2000 Q1.


= (31+42+52+53)/4


= 44.5



Then I need to find the simple average of all the stock's turnover for 2000 Q1.


= (41+44.5) / 2


= 42.75



Ultimately, I want to find out the 42.75 for each time period.



Right now the way I'm doing it is the following.



proc means no print data = turnover maxdec = 9;


output out = turnover1;


class date share;


variable turnover;


run;



Then I will get something like this



2000Q1        1        41


                   2       44.5



Then to find the simple average of all the stock's average turnover for 2000Q1, I will manually find the average by adding 41 to 44.5 and then divide it by 2.



However, this is incredibly time consuming because I have around 1000 stocks for each time period. If I sum it up manually and then divide it by the number of stocks.



So, may I ask if you have any simpler methods to help me obtain my final answer?



Thank you so much






最佳答案

davil2000 查看完整内容

x "cd d:\docs"; proc import datafile="data1.xls" out=data1 replace; /* sheet='sheet1';*/ getnames=yes; run; proc sort; by Share Date; run; proc means data = data1; class share date; output out=stat1 mean=avg_turnover std=std_turnover max=max_turnover min=min_turnover; var turnover; run; proc print data=stat1 noobs; run; data total_mean goup ...
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2012-9-3 11:38:12
data1.xls
大小:(13.5 KB)

 马上下载


x "cd d:\docs";
proc import datafile="data1.xls"
     out=data1 replace;
/*     sheet='sheet1';*/
     getnames=yes;
run;

proc sort;   by Share Date;
run;

proc means data = data1;
class share date;
output  out=stat1
    mean=avg_turnover  std=std_turnover
     max=max_turnover   min=min_turnover;
var turnover;
run;

proc print data=stat1 noobs;
run;

data  total_mean goup_mean;
set stat1;
if share=.
   then output total_mean;
   else output goup_mean;
run;

/*------------------The result in the output window---------------*/
                                                 avg_        std_         max_      min_
Share  Date   _TYPE_  _FREQ_   turnover   turnover  turnover  turnover
.             .         0            16        39.75       20.62        86         11
.     2000Q1       1             8         42.75       16.60        64         12
.     2000Q2       1             8         36.75       24.81        86         11
1            .          2            8         35.75       20.26        64         11
2            .          2            8         43.75       21.55        86         12
1     2000Q1      3             4         41.00       23.00        64         12
1     2000Q2      3             4         30.50       18.84        55         11
2     2000Q1      3            4          44.50       10.27        53         31
2     2000Q2      3            4          43.00       31.26        86         12
/*The 1st line show the mean, std, max, min of turnover for all the share and all the periods*/
/*The 2nd and 3rd lines show those statistics for all the share and the period of 2000Q1, 2000Q2 etc.*/
/*The 4th and 5th lines show those for share 1, 2 etc and all the periods.*/
/*The 6th-9th lines show those for share 1, 2 etc and the period of 2000Q1, 2000Q2 etc.*/

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-9-3 13:00:51
data turnover;
infile datalines;
input share turnover date$;
datalines;
1
54
2000Q1
1
34
2000Q1
1
64
2000Q1
1
12
2000Q1
1
34
2000Q2
1
22
2000Q2
1
11
2000Q2
1
55
2000Q2
2
31
2000Q1
2
42
2000Q1
2
52
2000Q1
2
53
2000Q1
2
86
2000Q2
2
42
2000Q2
2
32
2000Q2
2
12
2000Q2
;
run;
proc means mean data = turnover maxdec = 9;
class date share;
variable turnover;
output out = turnover1 mean=turnover;
run;
data turnover2;
   set turnover1(where=(date ne '' and share ne .) keep=date share turnover);
run;
proc means mean data=turnover2;
class date;
variable turnover;
output out=turnover3 mean=trunover;
run;

最后结果 Output:

                                        The MEANS Procedure

                                   Analysis Variable : turnover

                                                N
                                  date        Obs            Mean
                                  -------------------------------
                                  2000Q1        2      42.7500000

                                  2000Q2        2      36.7500000
                                  -------------------------------
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-9-3 13:15:20
非常感谢haku_x300,

您现在是手动输入数据,如果是直接调用一个xls数据文件,毕竟他有1000个股票,程序要怎么改?假设数据文件的名字是stock.xls

我发给我朋友,程序work,我马上把论坛币转给您:)
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-9-3 13:19:35
proc tabulate data = test;
        class share date;
        var turnover;
        table date*mean all*mean,share*turnover all*turnover;
        run;
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-9-3 13:20:31
means 过程可以直接计算simple average of all the stock's turnover
proc means data=turnover mean noprint;
class  date share;
var turnover;
output out=res(where=(_type_>=2) drop=_freq_) mean=/autoname;
run;
_type_=2即要求的 simple average of all the stock's turnover
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群