全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
1953 2
2013-11-06
有很多公司的每日股票价格数据,

需要做每个当日价格前5天到前125天的移动平均。

不知道用SAS能实现么?

还请大家指点,谢谢了!
二维码

扫码加我 拉你入群

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

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

全部回复
2015-2-4 23:02:16
方法1:proc expand的用法
出处:http://elek.me/calculate-moving-average-using-proc-expand-in-sas.html

data have;
do group=2 to 6;
do value=1 to 20 by 3,21,28,29,50;
output;
end;
end;
run;
/*1. Backward moving average: five-period
Yt = ( Xt-4 + Xt-3 + Xt-2 + Xt-1 + Xt ) / 5*/
proc expand data=have method=none out=want1(drop=time);
by group notsorted;
convert value=value1 / transform=(movave 5 trimleft 4);
run;
/*2. Forward moving average: five-period
Yt = ( Xt + Xt+1 + Xt+2 + Xt+3 + Xt+4) / 5*/
proc expand data=have method=none out=want2(drop=time);
by group notsorted;
convert value=value2 / transform=(reverse movave 5 reverse trimleft 4);
run;
/*3. Centered moving average:

3.1 the centered moving time window operator is an odd number, for example, five-period
Yt = ( Xt-2 + Xt-1 + Xt + Xt+1 + Xt+2 ) / 5 */
proc expand data=have method=none out=want3(drop=time);
by group notsorted;
convert value=value3 / transform=(cmovave 5 trim 2);
run;
/*3.2 the centered moving time window operator is an even number, for example, 4-period
3.2.1 one more lead value than lagged value is included in the time window
Yt = ( Xt-1 + Xt + Xt+1 + Xt+2 ) / 4*/
proc expand data=have method=none out=want4(drop=time);
by group notsorted;
convert value=value4 / transform=(cmovave 4 trimleft 1 trimright 2);
run;
/*3.2.2 one more lagged value than lead value is included in the time window
Yt = ( Xt-2 + Xt-1 + Xt + Xt+1 ) / 4*/
proc expand data=have method=none out=want5(drop=time);
by group notsorted;
convert value=value5 / transform=(reverse cmovave 4 reverse trimleft 2 trimright 1);
run;

/*3.2.3 weighted moving time window operator; the lead value and lagged value are all weighted 0.5
Yt = ( 0.5 * Xt-2 + Xt-1 + Xt + Xt+1 + 0.5 * Xt+2 ) / 4*/
proc expand data=have method=none out=want6(drop=time);
by group notsorted;
convert value=value61 / transform=(cmovave (0.5 1 1 1 0.5) trim 2 *5/4);
run;
复制代码方法2:
复制代码
%macro move_avg(var,num);
%let lagnum=%eval(&num-1);
(&var
%do i=1 %to &lagnum;
+lag&i(&var)
%end;
)/&num
%mend move_avg;

/*前面5天的ltt均值作为当天的mean,只选取2008年来的数据*/
data r4(where=(m>=(&t1.+1)));
set r3;
aa=%move_avg(ltt,&t1.);/*这个参数可以改动*/
mean=lag(aa);
run;
来源论坛网友
二维码

扫码加我 拉你入群

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

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

2016-2-5 10:29:27
非常impressive
二维码

扫码加我 拉你入群

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

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

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

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