出处: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;
- %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;
计算5期移动平均的程序?
本文来自: 人大经济论坛 SAS专版 版,详细出处参考:http://www.pinggu.org/bbs/viewthread.php?tid=930483&page=1&from^^uid=1832606


雷达卡


京公网安备 11010802022788号







