楼主: econfj
2997 15

[有偿编程] 帮朋友求助一个sas的问题 [推广有奖]

  • 1关注
  • 3粉丝

已卖:2份资源

教授

19%

还不是VIP/贵宾

-

威望
0
论坛币
32534 个
通用积分
28.9504
学术水平
1 点
热心指数
5 点
信用等级
2 点
经验
1020 点
帖子
456
精华
0
在线时间
1621 小时
注册时间
2010-4-2
最后登录
2022-7-14

楼主
econfj 发表于 2012-9-3 11:38:11 |AI写论文
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 ...
关键词:sas的 something following Thank you turnover 朋友 average period

本帖被以下文库推荐

沙发
davil2000 发表于 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.*/

已有 4 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
数据分析师3K + 100 + 100 + 5 + 5 + 5 精彩帖子
南海游客 + 5 + 5 + 5 精彩帖子
有福有德 + 100 + 100 + 5 观点有启发
大数据之魂 + 100 + 5 + 5 + 5 a simple but efficient program!

总评分: 经验 + 300  论坛币 + 200  学术水平 + 20  热心指数 + 15  信用等级 + 15   查看全部评分

R是万能的,SAS是不可战胜的!

藤椅
haku_x300 发表于 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
                                  -------------------------------

板凳
econfj 发表于 2012-9-3 13:15:20
非常感谢haku_x300,

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

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

报纸
00810112 发表于 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;

地板
ziyenano 发表于 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

7
webgu 发表于 2012-9-3 13:24:36
  1. proc import out=turnover
  2.                   datafile="d:\stock.xls"
  3.                                   dbms=xls
  4.                                   replace;
  5.                                   getnames=yes;
  6. run;
  7. proc sql;
  8.   create table average1 as
  9.   select  share , date,avg(turnover) as turnover_avg1
  10.   from turnover
  11.   group by share , date;
  12. quit;



  13. proc sql;
  14.   create table average2 as
  15.   select date, avg(turnover_avg1) as avg2
  16.   from average1
  17.   group by  date;
  18. quit;
复制代码
result:                                       Obs    Share     Date        turnover_avg1  

                                               1       1      2000Q1       41.0
                                               2       1      2000Q2       30.5
                                               3       2      2000Q1       44.5
                                               4       2      2000Q2       43.0


                                                   Obs     Date      avg2

                                                      1     2000Q1    42.75
                                                      2     2000Q2    36.75

log:19   proc import out=turnover
20                     datafile="d:\stock.xls"
21                     dbms=xls
22                     replace;
23                     getnames=yes;
24   run;

NOTE: The import data set has 16 observations and 3 variables.
NOTE: WORK.TURNOVER data set was successfully created.
NOTE: PROCEDURE IMPORT used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


25   proc sql;
26     create table average1 as
27     select  share , date,avg(turnover) as turnover_avg1
28     from turnover
29     group by share , date;
NOTE: Table WORK.AVERAGE1 created, with 4 rows and 3 columns.

30   quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


31
32
33
34   proc sql;
35     create table average2 as
36     select date, avg(turnover_avg1) as avg2
37     from average1
38     group by  date;
NOTE: Table WORK.AVERAGE2 created, with 2 rows and 2 columns.

39   quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds

SAS资源
1. SAS 微信:StatsThinking
2. SAS QQ群:348941365

8
econfj 发表于 2012-9-3 13:36:15
webgu,您好,我现在是一点sas都不懂了,
从你的程序看,我不明白数据是怎么输入的,已经导入sas专用的数据文件了吗?非常感谢!

9
webgu 发表于 2012-9-3 13:38:48
econfj 发表于 2012-9-3 13:36
webgu,您好,我现在是一点sas都不懂了,
从你的程序看,我不明白数据是怎么输入的,已经导入sas专用的数据 ...
已添加更新。
SAS资源
1. SAS 微信:StatsThinking
2. SAS QQ群:348941365

10
econfj 发表于 2012-9-3 13:47:19
webgu,您好,感觉数据输入部分有typo:imort应该是import吧,d:\stock.xks   应该是d:\stock.xls吧

能不能麻烦在你的sas上跑一下,我没有装sas,得出结果,然后我转给我的朋友(他不认识中文),非常感谢!

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-9 11:32