全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
11841 6
2012-03-30
data stockindex;  set stockindex;    la0=_col4; la1=lag1(_col4); la2=lag2(_col4); la3=lag3(_col4); la4=lag4(_col4); la5=lag5(_col4);    la6=lag6(_col4); la7=lag7(_col4); la8=lag8(_col4); la9=lag9(_col4);     if _n_>=10 then avg=sum(_col4,la1,la2,la3,la4,la5,la6,la7,la8,la9)/n(_col4,la1,la2,la3,la4,la5,la6,la7,la8,la9);    keep _col0 _col4 avg;
run;
      这是论坛里一个高人给的移动平均的解答,但如果我想求的平均至是前100个或者更多观测的话,这样列出来就麻烦了。能不能用数组或者什么办法修改一下,不用一个一个地列呢?不知道各位大侠有没有办法啊,多谢了……

二维码

扫码加我 拉你入群

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

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

全部回复
2012-3-31 08:24:13
If you don't like lag functions, a simple solution would be to load data into and temporary and to calculate with array index.

%let n=20;
data t1;
   do dates=1 to &n;
      price+1;
          output ;
        end;
run;

data t2;
   set t1 ;
   array ph(&n) _temporary_;
   ph[_n_]=price;
   put ph[_n_];
   sump=.;
   if _n_>10 then do;   
     do i=_n_ to _n_-10+1 by -1;
           sump=sum(sump,ph[i]);
         end;
         pmv=sump/10;
    end;
        keep dates price pmv;
run;

proc print;run;
二维码

扫码加我 拉你入群

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

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

2012-3-31 09:38:54
array a[100] b1-b&p.;
        a[1]=lag(balance);
        do i=2 to 100;
                a=lag(a[i-1]);
        end;

这样可以实现100个lag,然后mean100=mean(of b1-b100);
二维码

扫码加我 拉你入群

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

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

2012-4-7 14:11:57
sunset1986 发表于 2012-3-31 09:38
array a[100] b1-b&p.;
        a[1]=lag(balance);
        do i=2 to 100;
多谢高人……
二维码

扫码加我 拉你入群

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

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

2012-4-7 14:12:34
bobguy 发表于 2012-3-31 08:24
If you don't like lag functions, a simple solution would be to load data into and temporary and to  ...
都好高深对于我来说,不过必须得感谢的……
二维码

扫码加我 拉你入群

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

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

2012-4-7 23:21:04
%macro ma(x); /*计算x天的移动平均线,例如x=5,就是计算5天的移动平均线*/
   ma&x=0; /*初始变量ma5=0*/
   array l&x.ags(&x);  /*申明一个数组,数组名字为l5ags*/
   l&x.ags(1)=close; /*close为收盘价*/
   ma&x=ma&x+l&x.ags(1);
   do i&x=2 to &x;
   l&x.ags(i&x)=lag(l&x.ags(i&x-1));
   ma&x=ma&x+l&x.ags(i&x);
   end;
   ma&x=ma&x/%eval_r(&x);
   drop i&x l&x.ags1-l&x.ags&x; /*将不必要的变量舍去*/
%mend;
二维码

扫码加我 拉你入群

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

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

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

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