全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
3824 13
2012-02-17
data a;
input ID$ Num type;
cards;
001 1 1
001 2 1
001 3 1
002 2 1
002 1 0
002 2 1
003 1 1
003 4 1
;
data b;
   do _n_=1 by 1 until(last.id);
      set a;
      by id;
      if type=0 then del_flag=1;
   end;
   do _n_=1 to _n_;
      set a;
   output;
   end;
run;
proc print data=b;
run;
结果为:
       obs   ID     NUM    TYPE  del_flag
         1     001     1       1         .
         2     001     2       1         .
         3     001     3       1         .
         4     002     2       1         1
         5     002     1       0         1
         6     002     2       1         1
         7     003     1       1         .
         8     003     4       1         .
为什么ID为002的所有del_flag都为1呢?我的理解是只有TYPE为0 的那条记录为1 ???
整个PDV流程是怎样的呢?
二维码

扫码加我 拉你入群

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

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

全部回复
2012-2-17 12:59:34
data a;
input ID$ Num type;
cards;
001 1 1
001 2 1
001 3 1
002 2 1
002 1 0
002 2 1
003 1 1
003 4 1
;
data b;
   do _n_=1 by 1 until(last.id);
      set a;
      by id;
      if type=0 then del_flag=1;
put "第一次" del_flag=;
   end;

   do _n_=1 to _n_;
      set a;
          put "第二次" del_flag=;
          output;
   end;
run;
proc print data=b;
run;
这样测试一下就知道了
二维码

扫码加我 拉你入群

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

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

2012-2-17 13:02:41
即只要一组中有TYPE为0则del_flag就会为1
二维码

扫码加我 拉你入群

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

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

2012-2-19 10:29:02
This is an unconventional data step to process by block data structure. It is a very convenient way sometime.

In your case, the first loop block checks which block has any type value 0. Because a data set is accessed in an explicit do-loop, the del_flag retains its value automatically. So the second loop will assign it to the block. That is why the whole block has value 1 for del_flag. After that a next by block process goes on ...

I will do,

data b;
   do _n_=1 by 1 until(last.id);
      set a;
      by id;
      if type=0 then del_flag=1;
   end;

   do _n_=1 by 1 until(last.id);
      set a;
      by id;
   output;
   end;
run;

This will be much clear.
二维码

扫码加我 拉你入群

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

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

2014-5-17 09:54:10
data a;
input ID$ Num type;
cards;
001 1 1
001 2 1
001 3 1
002 2 1
002 1 0
002 2 1
003 1 1
003 4 1
;
data b;
   do _n_=1 by 1 until(last.id);
      set a;
      by id;
      if type=0 then do;del_flag=1;output;end;
      else do;del_flag=.;output;end;
   end;
run;
proc print data=b;
run;
二维码

扫码加我 拉你入群

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

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

2014-5-17 11:38:59
楼主PDV变化过程分析:
由于有三组ID,数据步b自动循环4次,前三次写入数据,第四次无数据结束,详细运行如下:
第一次开始时:
last.id=1 ID=  Num=. type=. FIRST.ID=1 del_flag=. _ERROR_=0 _N_=1
执行第一个程序循环后:
last.id=1 ID=001 Num=3 type=1 FIRST.ID=0 del_flag=. _ERROR_=0 _N_=3
接着执行第二个程序循环,写入第一组观测。

第二次自循环开始时:
last.id=1 ID=001 Num=3 type=1 FIRST.ID=0 del_flag=. _ERROR_=0 _N_=2
执行第一个程序循环后:
last.id=1 ID=002 Num=2 type=1 FIRST.ID=0 del_flag=1 _ERROR_=0 _N_=3
由于数据集第5个观察的type=0, 语句if type=0 then del_flag=1;被执行,所以del_flag=1,即在执行程序的第二个循环前,del_flag=1。所以第二组的三个观测都被写入del_flag=1。
第二组观察写入后:last.id=1 ID=002 Num=2 type=1 FIRST.ID=0 del_flag=1 _ERROR_=0 _N_=4
进入第三步自循环,开始时:
last.id=1 ID=002 Num=2 type=1 FIRST.ID=0 del_flag=. _ERROR_=0 _N_=3

执行第一个程序循环后:
last.id=1 ID=003 Num=4 type=1 FIRST.ID=0 del_flag=. _ERROR_=0 _N_=2(第三组有2个观察)
执行第二个程序循环写入2个观察后:
last.id=1 ID=003 Num=4 type=1 FIRST.ID=0 del_flag=. _ERROR_=0 _N_=3

进入第四步自循环,开始时:
last.id=1 ID=003 Num=4 type=1 FIRST.ID=0 del_flag=. _ERROR_=0 _N_=4

由于无观测,第四部自循环不执行,程序结束。
注:每次自循环开始, del_flag会被自动设置为缺失值。
初学sas,不当之处,敬请高手指点!







二维码

扫码加我 拉你入群

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

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

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

说点什么

分享

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