1

如果它们重叠,我正在尝试合并日期。例如,

当前数据看起来像,

ID  START     END 
223 20130101  20130201
223 20130104  20130109
223 20130120  20130320
223 20130430  20130930
110 19981219  20010412
110 20000901  20010206
110 20000926  20010306
110 20001002  20010423
110 20001218  20010306
110 20001218  20010306

我需要的结果是,

ID  START     END 
223 20130101  20130320
223 20130430  20130930
110 19981219  20010412

我查找了这种类型的问题,但无法使可执行查询适合我的数据。我什至无法理解代码,非常感谢任何帮助

4

1 回答 1

3

对此的数据步骤解决方案相当简单。这是一般形式;您可能需要针对您的特定数据进行调整。

data have;
informat start end YYMMDD8.;
format start end DATE9.;
input ID  START     END;
datalines;
223 20130101  20130201
223 20130104  20130109
223 20130120  20130320
223 20130430  20130930
110 19981219  20010412
110 20000901  20010206
110 20000926  20010306
110 20001002  20010423
110 20001218  20010306
110 20001218  20010306
;;;;
run;
proc sort data=have;
by id start end;
run;

data want;
set have;
by id start end;
retain curstart curend;
format curstart curend DATE9.;
if first.id then do;   *clear the retained variables for first record of new id;
    curend=.;
    curstart=.;
end;
if start > curend then do;   *if there is a gap, or it is the first record of a new id;
    if not (first.id) then output;
    curstart=start;
    curend=end;
end;
else if end > curend then do;  *update the current period end;
    curend=end;
end;
if last.id then output;  *output the final record for each ID;
run;
于 2013-11-13T15:31:33.350 回答