楼主: godtears
5217 30

[原创博文] 如何运用SAS对下列数据进行处理 [推广有奖]

11
losttemple 发表于 2009-8-22 12:01:47
把sum改成std

12
ddd1000 发表于 2009-8-22 12:03:10
Hope answer your question and get the rewards.
A general method.

%let n=2;
%let m=%eval(&n+1);
data tem;
input code day event x;
datalines;
1         02        0       1.9
1         03        0       3.3
1         04        1       -4
1         05        1       18
2         01        0        6
2         02        0       4
2         03        1        7
;
run;

proc sort data=tem;
    by code day;
run;


data tem;
    set tem;
   array m[&m] a1-a&m;
  retain a1-a&m;
    by code day;
  if first.code then count=.;
  count+1;
  if count<=&m then m[count]=x;
  else do;
     do i=1 to &n;
         m[i]=m[i+1];
     end;
     m[&m]=x;
  end;

  do j=1 to &n;
     sum=sum+m[i];
  end;
  avg=sum/&n;
run;

data out;
   set tem;
   if event=1;
run;

13
ddd1000 发表于 2009-8-22 12:05:55
a little mistake in code:
change sum=sum+m[j];

14
ddd1000 发表于 2009-8-22 12:19:11
revised total program.

Hope answer your question and get the rewards.
A general method.
%let n=2;
%let m=%eval(&n+1);
data tem;
input code day event x;
datalines;
1         02        0       1.9
1         03        0       3.3
1         04        1       -4
1         05        1       18
2         01        0        6
2         02        0       4
2         03        1        7
;
run;
proc sort data=tem;
    by code day;
run;

data tem;
    set tem;
   array m[&m] a1-a&m;
  retain a1-a&m;
    by code day;
  if first.code then do;
      count=.;
      do i=1 to &m;
          m[i]=.;
       end;
  end;

  count+1;
  if count<=&m then m[count]=x;
  else do;
     do i=1 to &n;
         m[i]=m[i+1];
     end;
     m[&m]=x;
  end;
  do j=1 to &n;
     sum=sum+m[j];
  end;
  avg=sum/&n;
run;
data out;
   set tem;
   if event=1;
run;

15
地狱小子 发表于 2009-8-22 12:21:07
人在外手机回复下,sum改成std,除数去掉

16
ddd1000 发表于 2009-8-22 12:42:35
The SQL method code is short but have a problem for some special situation.
for example:

day=04 event=1 before this record there are two records before it. if we want to calculate the 3 records before it. because there are only two records, the mean should be missing but the SQL code will just calculate the mean of two records before it.
This example is just calculate the moving average. Let's say 10 days average, before 10 days there are no 10 days average.
just for discussion. SQL  sometimes can give a quick answer. but we need to think some special situation and to make the code robust and strong

17
godtears 发表于 2009-8-22 12:47:31
14# ddd1000

哇,SAS高手好多啊!
正在领悟程序中。。。。。。。
相比,地狱小子的程序更简炼更好懂些。。。。。。
不管怎样,谢谢啦。。。。

18
godtears 发表于 2009-8-22 12:58:55
16# ddd1000

按你的意思,
假设我们要计算某事件之前三天的平均收益率,
只要这三天有一个收益率数值缺失,你的程序算出来的平均收益率也是为缺失。
而地狱小子给出的程序是:平均收益率是计算没有缺失的数值的均值,如果少一个,就算两个数值的平均数。
是这样子吗?

19
ddd1000 发表于 2009-8-22 13:09:00
right, It depends what you want. My program actually can do both. you just need a little bit change in the part of sum and mean calculation.

Let say a group of data

day  1  2   3   4   5   6   7   8  9  10
X      3   5   6  8  6    9   2  10 11 12

if calculate 5 days mean
the data sould looks like:
day  1  2   3   4   5   6   7   8  9  10
X      3   5   6  8  6    9   2  10 11 12
mn  .    .     .    .   .     (3+5+6+8+6)/5 on day 5
                                  (5+6+8+6+9)/5 on day 6 etc

if you want just calculate whatever data within 5 days before(like missing data or only 2 or 3 data
you just calculate of those data .

20
godtears 发表于 2009-8-22 15:27:02
19# ddd1000

Thanks a lot,
But,
the same question I have asked is:
If I want to get the standard deviation  not the average,
how to change the SAS codes you write.

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

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