楼主: 臻于完美
3068 13

sas 中如何修改前一个观测中的数据;第5个观测满足一定调节,则在4个观测内进行修改? [推广有奖]

11
chengys81 发表于 2014-2-18 00:49:20
这个链接用了proc expand,似乎是用了lead function就可以把t + 1 obs弄成t obs,然后就可以让t obs的b=0 if a > 10.
[http://www.ats.ucla.edu/stat/sas/faq/tsvars.htm]

12
chengys81 发表于 2014-2-18 00:52:03
用proc expand可以

proc expand data=sashelp.class out=lead;  
   convert weight=leadWeight  / method=none transformout=(lead 1);  
   run;  
proc print;  
   run;  

13
farmman60 发表于 2014-2-18 07:10:43
data a;
  input id  a b;
datalines;
1 1 1
2 3 3
3 5 5
4 7 7
5 11 11
6 2 2
7 4 4
8 6 6
9 8 8
10 12 12
11 13 13
12 14 14
13 9 9
14 3 3
15 6 6
;
run;

data _null_;
   if 0 then set a (rename=(a=_a));
   if _n_=1 then do;
     declare hash h(dataset:'a(rename=a=_a)',ordered:'a');
         h.definekey('id');
         h.definedata(all:'y');
         h.definedone();
         declare hiter hi('h');
         call missing(id,_a ,b);
        end;
        do point=1 to n;
        set a point=point nobs=n;
        if point>=5 and mod(point,5)=0 and a>=10 then do;
                   do while (hi.setcur(key:id)=0);
                      hi.prev();
                          a=0;
                          h.replace(key:id,data:id,data:a,data: b);
                          leave;
                        end;
                end;
   end;
   h.output(dataset:'want(rename=(_a=a)');
   stop;
   run;

14
臻于完美 发表于 2014-2-19 10:58:08
chengys81 发表于 2014-2-18 00:49
这个链接用了proc expand,似乎是用了lead function就可以把t + 1 obs弄成t obs,然后就可以让t obs的b=0 i ...
谢啦,,我要的就是这个lead function

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-19 02:41