双set表整合出复杂的表(附有sas&n…

来源:互联网 发布:大庆油田网络客服电话 编辑:程序博客网 时间:2024/06/04 20:02

双set语句的特点:
1.每个set在内存中都开辟一个指针,指向数据集,有先后顺序。

2.一旦有一个指针指向了文件的结尾,则跳出循环。

3.双set语句读取数据集的记录时,相当于retain语句,保留记录值,直到下一条记录覆盖它为止。

 

以下附上code :

data kids_ids;
  attrib id length=$5
        dob informat=mmddyy10. format=mmddyy10.;
  input id $ dob;
datalines;
C0402 07/15/2001
C1593 06/30/2003
C1374 04/23/2007
C3811 02/01/2009
C1901 03/18/2009
;;;
proc print data=kids_ids;
  title "KIDS_IDS";
run;
data immunizations;
  attrib id length=$5
        type length=$10
        sequence length=3
        received informat=mmddyy10. format=mmddyy10.;
  input id $ type $ sequence received;
datalines;
C3811 POLIO 1 04/01/2009
C0402 DTAP  1 09/12/2001
C0402 POLIO 1 09/12/2001
C0402 DTAP  2 11/16/2001
C0402 POLIO 2 11/16/2001
C0402 DTAP  3 01/10/2002
C0402 POLIO 3 04/14/2002
C0402 CPOX  1 07/30/2002
C0402 MMR   1 07/30/2002
C0402 DTAP  4 11/20/2002
C0402 CPOX  2 04/15/2006
C0402 MMR   2 04/15/2006
C0402 DTAP  5 08/15/2006
C0402 POLIO 4 08/15/2006
C1593 DTAP  1 09/05/2003
C1593 POLIO 1 09/05/2003
C1593 DTAP  2 10/29/2003
C1593 POLIO 2 10/29/2003
C1593 DTAP  3 01/03/2004
C1593 CPOX  1 08/04/2004
C1593 MMR   1 08/04/2004
C1593 DTAP  4 10/20/2004
C1593 DTAP  5 07/16/2008
C1593 POLIO 3 07/16/2008
C1593 CPOX  2 08/23/2008
C1593 MMR   2 08/23/2008
C1374 DTAP  1 06/28/2007
C1374 POLIO 1 06/28/2007
C1374 DTAP  2 08/22/2007
C1374 POLIO 2 08/22/2007
C1374 DTAP  3 10/20/2007
C1374 POLIO 3 01/22/2008
C1374 CPOX  1 05/03/2008
C1374 MMR   1 05/03/2008
C0054 DTAP  1 07/01/2000
C0054 POLIO 1 07/01/2000
;;;;
proc datasets library=work;
  modify immunizations;
  index create id;
run;
quit;
proc print data=immunizations;
  title "IMMUNIZATIONS";
run;
data dtap_kids;
  set kids_ids;

  array allshots{5}dtap_date1-dtap_date5;
  format dtap_date1-dtap_date5 mmddyy10.;

  drop sequence received type;

  do until (_iorc_=%sysrc(_dsenom));
    setimmunizations key=id;

    select(_iorc_);
     when(%sysrc(_sok)) do;
       if type='DTAP' then do;
         if 1 <= sequence <= 5 then allshots{sequence}=received;
         else putlog 'ERROR: DTAP_DATE cannot be updated. Value of SEQUENCEis not 1-5.'/
                     id= sequence= received=;
       end;
     end;
     when (%sysrc(_dsenom)) do;
       if allshots{sequence} ne . then output;
       _error_=0;
     end;
     otherwise do;
       putlog "ERROR: Unexpected error _IORC_=" _iorc_;
       stop;
     end;
    end;
  end;
run;
proc print data=dtap_kids;
  title "Example 3.13 DTAP_KIDS Data Set Createdwith DATA Step";
run;

(以上代码由sas官网拷贝非原创)

 

得到的:

双set表整合出复杂的表(附有sas <wbr>code <wbr>)

0 0
原创粉丝点击