全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
1650 2
2013-03-18
有这样一个数据,
Code        BeginDate        EndDate
001        08/15/1981        08/18/2007
001        12/10/2007        09/30/2009
001        10/01/2009        12/31/2014
002        08/15/1981        08/18/2007
002        12/10/2007        09/30/2009
003        08/15/1981        08/18/2007
003        12/10/2007        09/30/2009
003        10/01/2009        12/31/2014
004        08/15/1981      08/18/2007
004        12/10/2007        09/30/2009
004        10/01/2009        12/31/2014
005        08/15/1981        12/09/2007
005        12/10/2007        09/30/2009
005        10/01/2009        12/31/2014

我在想如何在同一个code下,把begin date 和enddate尽可能的合并在一起,举例来说,如果在code 001中,第二个时间07年12月10号至09年9月30号,与第三个时间09年10月1号至14年12月31号,他们其实是连续的,所以合并成为从07年12月10号至14年12月31号,这样001只剩下两个观测量。
又比如005中,从81年8月15日一直到14年12月31日一直都是连续的,所以最后005只会剩下一个观测量81年8月15日到14年12月31日。


我一开始的做法是比较begin date和lag(end date)+1的区别,如果相等,就把begin date赋值成为lag(enddate)+1,但是这样只能执行一步,像005中这种情况就不行了,所以想请大家探讨一下,多谢指点。
二维码

扫码加我 拉你入群

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

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

全部回复
2013-3-18 15:50:47
data test;
  input code $        BeginDate :mmddyy10.      EndDate : mmddyy10.;
  format begindate enddate mmddyy10.;
  cards;
001        08/15/1981        08/18/2007
001        12/10/2007        09/30/2009
001        10/01/2009        12/31/2014
002        08/15/1981        08/18/2007
002        12/10/2007        09/30/2009
003        08/15/1981        08/18/2007
003        12/10/2007        09/30/2009
003        10/01/2009        12/31/2014
004        08/15/1981      08/18/2007
004        12/10/2007        09/30/2009
004        10/01/2009        12/31/2014
005        08/15/1981        12/09/2007
005        12/10/2007        09/30/2009
005        10/01/2009        12/31/2014
;

data wanted (rename=(_begin=begindate _end=enddate));
        retain _begin _end;
        format _begin _end mmddyy10.;
        do until(flag=1);
            set test;
                by code begindate;
                if first.code then do; _begin=begindate; _end=enddate; end;
                if begindate-_end=1 then _end=enddate;
        if begindate-_end>1 or last.code then flag=1;
    end;
        output;
        if last.code and enddate ne _end then do;
          _begin=begindate; _end=enddate; output;
        end;
        _begin=begindate; _end=enddate;

        keep code _begin _end;
run;
二维码

扫码加我 拉你入群

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

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

2013-3-18 16:01:27
data have;
input Code   BeginDate : mmddyy10.     EndDate : mmddyy10.;
format   BeginDate mmddyy10.       EndDate  mmddyy10.;
datalines;
001        08/15/1981        08/18/2007
001        12/10/2007        09/30/2009
001        10/01/2009        12/31/2014
002        08/15/1981        08/18/2007
002        12/10/2007        09/30/2009
003        08/15/1981        08/18/2007
003        12/10/2007        09/30/2009
003        10/01/2009        12/31/2014
004        08/15/1981       08/18/2007
004        12/10/2007        09/30/2009
004        10/01/2009        12/31/2014
005        08/15/1981        12/09/2007
005        12/10/2007        09/30/2009
005        10/01/2009        12/31/2014
;

data  tmp;
  set have;
  by code notsorted;
LagBeg=lag(BeginDate);
LagEnd=lag(EndDate);
if  first.code then  call missing(LagBeg,LagEnd )  ;
if  BeginDate=LagEnd+1 then NewBeginDate=LagBeg;
LagNewBeginDate=lag(NewBeginDate);
run;

data want(keep=code BeginDate EndDate);
  set tmp;
  by code;
if  (not first.code ) and  (not missing(coalesce(LagNewBeginDate,NewBeginDate) ))  then BeginDate=coalesce(LagNewBeginDate,NewBeginDate);
run;

proc sort data=want  nodupkey;
     by code BeginDate;
run;
二维码

扫码加我 拉你入群

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

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

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

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