Hi @Bounce
If you have overlapping ranges, you can try the following code, which will retrieve the min(start_date) and the max(end_date) if the lag is strictly more than 1.
PS: I have deliberately modified the entry data to show different cases.
My best,
data have;
input ID Start_Date End_Date;
informat Start_Date End_Date ddmmyy10.;
format Start_Date End_Date date9.;
cards;
1 01/01/2019 04/01/2019 1 07/01/2019 12/01/2019 1 10/01/2019 18/01/2019 1 14/01/2019 16/01/2019 1 16/01/2019 22/01/2019 2 01/01/2019 04/01/2019 2 07/01/2019 12/01/2019 2 10/01/2019 18/01/2019
;
run;
proc sort data=have;
by ID Start_Date End_Date;
run;
data have2;
set have;
format _lag date9.;
by ID;
_lag = lag(End_Date) ;
if first.ID then do;
_lag = .;
flag = 0;
end;
if _lag + 1 < Start_Date then flag + 1;
run;
proc sql;
create table want as
select ID,
min(Start_Date) as Start_date format=date9.,
max(End_date) as End_date format=date9.
from have2
group by ID, flag;
quit;
... View more