BookmarkSubscribeRSS Feed
cjacobson45
Calcite | Level 5

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

 

2 REPLIES 2
Tom
Super User Tom
Super User

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;

image.png

Ksharp
Super User
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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 595 views
  • 3 likes
  • 3 in conversation