data have1;
input id start stop @@;
informat start stop mmddyy10.;
format start stop mmddyy10.;
datalines;
1 01/01/2019 02/01/2019
1 02/01/2019 03/01/2019
1 03/01/2019 04/01/2019
1 04/01/2019 05/01/2019
1 05/01/2019 06/01/2019
2 04/01/2019 05/01/2019
2 05/01/2019 06/01/2019
;
run;
data have2;
input id surgery1 surgery2 @@;
informat surgery1 surgery2 mmddyy10.;
format surgery1 surgery2 mmddyy10.;
datalines;
1 02/15/2019 04/10/2019
;
run;
data temp;
set have1;
n+1;
do date=start to stop;
output;
end;
keep id date n;
run;
proc transpose data=have2 out=temp2;
by id;
var surgery:;
run;
proc sort data=temp;by id date;run;
data want;
set temp temp2(keep=id col1 rename=(col1=date) in=inb );
by id date;
flag=inb;
if not missing(date);
format date mmddyy10.;
run;
data want;
update want(obs=0) want;
by id;
output;
run;
data want;
set want;
by id n notsorted;
if first.id then do;group=0;group2=0;end;
if first.n then group+1;
if flag then group2+1;
run;
proc sql;
create table final_want as
select id,group,group2,min(date) as start format=mmddyy10.,max(date) as stop format=mmddyy10.
from want
group by id,group,group2;
quit;