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

本文介绍了SAS中双SET语句的特性和使用示例,用于整合两个数据集。通过示例展示了如何将kids_ids和immunizations数据集合并,创建一个新的dtap_kids数据集,保留特定条件的记录。

双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));
    set immunizations 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 SEQUENCE is 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 Created with DATA Step";
run;

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

 

得到的:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值