I have a program that I need to end up with a member and all their dates for admission and discharge, sometimes there are multiple dates that overlap, so if that is the case then I need to just have one record with the entire time period. Example:
Member Admit Date Discharge Date
111 17MAY2017 17MAY2017
111 18MAY2017 18MAY2017
111 16MAY2017 24MAY2017
222 02JAN2016 05JAN2016
222 03JAN2016 04JAN2016
333 03MAR2017 04MAR2017
333 22FEB2017 09MAR2017
333 01JUL2017 01JUL2017
What I would need here is to pull the following so I have a continuous date range for those overlapping :
Member Admit Date Discharge Date
111 16MAY2017 24MAY2017
222 02JAN2016 05JAN2016
333 22FEB2017 09MAR2017
333 01JUL2017 01JUL2017
You should be able to search for this result as it is a common question. In SAS it is easier to do in a data step since PROC SQL does not support the windowing function syntax of newer SQL dialects.
Make sure to order the data and that your records do not have discharge before admit.
proc sort;
  by member admit discharge; 
run;
data want ;
  set have ;
  by member ;
  retain first last ;
  format first last yymmdd10. ;
  if first.member then call missing(first,last);
  else if (admit-1 > last) then do;
    output; 
    call missing(first,last);
  end;
  first = min(first,admit);
  last = max(last,discharge);
  if last.member then output;
  drop admit discharge;
run;
data have;
input Member           AdmitDate   : date9.       DischargeDate : date9.;
format  AdmitDate  DischargeDate : date9.;
cards;
111                   17MAY2017         17MAY2017
111                   18MAY2017         18MAY2017
111                   16MAY2017         24MAY2017
222                   02JAN2016         05JAN2016
222                   03JAN2016         04JAN2016
333                   03MAR2017        04MAR2017
333                   22FEB2017         09MAR2017
333                   01JUL2017          01JUL2017
;
run;
data temp;
 set have;
 do date=AdmitDate to DischargeDate;
  output;
 end;
 format date date9.;
 keep member date;
run;
proc sort data=temp nodupkey;
by member date;
run;
data temp;
 set temp;
 by member;
 if first.member or dif(date) ne 1 then group+1;
run;
proc summary data=temp;
by member group;
var date;
output out=want min=AdmitDate max=DischargeDate;
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.
