s******a 发帖数: 388 | 1 我有一个data set.
name date ret
A 10/11/10 0.5
A 11/11/10 0.8
A 12/11/10 0.9
B 10/11/10 0.7
B 11/11/10 1.0
B 12/11/10 0.8
我现在要求A 在10/11/10的'ret'平均值, 即(0.5+0.8+0.9)/3
A在10/11/10的‘ret'平均值, 即(0.8+0.9)/2, 同样也求B在这两个时间点的'ret'
平均值。当然我的DADASET 比这大太多,我不知道是否说清楚了。 我想是否应该用
first.variable 和Macro,但不知从何下手。拜请赐教!谢谢!
|
b*******r 发帖数: 152 | 2 do u have a 'fixed' window size? |
s******a 发帖数: 388 | 3 What does "fixed window size" mean? My data is much bigger than this, Name
is from A to Z, Date is not only 3, A has 60 dates, B has 50 dates, the
Names have different Dates. Thank you! |
s******r 发帖数: 1524 | 4 I do think you state your problem clearly.
A 在10/11/10的'ret'平均值, 即(0.5+0.8+0.9)/3
you want to calcuate 10/11/10 twice?
ret' |
s******a 发帖数: 388 | 5 Sorry, 笔误, 第二个日期应该是11/11/10. 谢谢! |
x***x 发帖数: 3401 | 6 If the number of time points within each subject doesnt vary too much, why
dont you make a "long" to "short" transformation?
So the short form has the following variables:
NAME T1 RET1 T2 RET2 T3 RET3
then you can calculate the avgs
AVG123 AVG23 AVG3 etc...
ret'
【在 s******a 的大作中提到】 : 我有一个data set. : name date ret : A 10/11/10 0.5 : A 11/11/10 0.8 : A 12/11/10 0.9 : B 10/11/10 0.7 : B 11/11/10 1.0 : B 12/11/10 0.8 : 我现在要求A 在10/11/10的'ret'平均值, 即(0.5+0.8+0.9)/3 : A在10/11/10的‘ret'平均值, 即(0.8+0.9)/2, 同样也求B在这两个时间点的'ret'
|
s******y 发帖数: 352 | 7 hope this will get you stated. I assume the data was already sorted by name.
if not, you have to sort it before run the code.
data have;
input name $ date :mmddyy10. ret;
cards;
A 10/11/10 0.5
A 11/11/10 0.8
A 12/11/10 0.9
B 10/11/10 0.7
B 11/11/10 1.0
B 12/11/10 0.8
;
run;
data want;
do _n_=1 by 1 until(last.name);
set have;
by name;
array temp{999};
temp(_n_)=ret;
end;
do _j=1 to _n_;
set have;
do _i=_j to _n_;
all+temp(_i);
end;
ret=all/(_n_-_j+1);
output;
call missing(all);
en
【在 s******a 的大作中提到】 : 我有一个data set. : name date ret : A 10/11/10 0.5 : A 11/11/10 0.8 : A 12/11/10 0.9 : B 10/11/10 0.7 : B 11/11/10 1.0 : B 12/11/10 0.8 : 我现在要求A 在10/11/10的'ret'平均值, 即(0.5+0.8+0.9)/3 : A在10/11/10的‘ret'平均值, 即(0.8+0.9)/2, 同样也求B在这两个时间点的'ret'
|
s******r 发帖数: 1524 | 8 proc sql;
select a.name, a.date,
avg(b.ret) as avg_ret from
test a inner join test b
on a.name=b.name and b.date>=a.date
group by a.name, a.date
;quit;run;
ret'
【在 s******a 的大作中提到】 : 我有一个data set. : name date ret : A 10/11/10 0.5 : A 11/11/10 0.8 : A 12/11/10 0.9 : B 10/11/10 0.7 : B 11/11/10 1.0 : B 12/11/10 0.8 : 我现在要求A 在10/11/10的'ret'平均值, 即(0.5+0.8+0.9)/3 : A在10/11/10的‘ret'平均值, 即(0.8+0.9)/2, 同样也求B在这两个时间点的'ret'
|
s******a 发帖数: 388 | 9 谢谢 smileguy and scimitar, I'll try them. |