BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
yma0424
Calcite | Level 5

Hello - I have a dataset with date admit and date discharge.  There are cases where the patient may have been admitted again without ever discharging.  In order to figure out the number of patients returning, I need to flag those that never actually discharged.  So for example, since this patient never discharged and was re-admitted on 1/23, I want observation 2 and 3 to be flagged as a patient who never discharged.  The issue is each group (Patient ID) has a different number of observations.  I couldn't figure out how to use lag to look back n times.  It seems to need a specific number.  I also can't just use first.ID or last.ID because the missing date may occur in the middle of the group.  Hope this makes sense!  

Patient Date___AdmitDate___Dischargen
13/26/2019.1
11/23/20206/19/20202
18/21/20207/19/20213

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Join with itself, with a filter on the second dataset:

data want;
merge
  have
  have (
    in=t2
    keep=patient date_discharge
    rename=(date_discharge=disc)
    where=(disc = .)
  )
;
if t2 then non_disc = 1;
drop disc;
run;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

Join with itself, with a filter on the second dataset:

data want;
merge
  have
  have (
    in=t2
    keep=patient date_discharge
    rename=(date_discharge=disc)
    where=(disc = .)
  )
;
if t2 then non_disc = 1;
drop disc;
run;
yma0424
Calcite | Level 5

Thank you!  This worked great!  Only thing is, I left something out.  I have to define: new patients and returning patients.  Those that you helped me with are neither new or returning.  They have an admission with no discharge, therefore they're still receiving services in some program.  The problem I'm running into is, if I use the merge technique, it overrides some of my defined returning patients.  So for example, Patient 1 is a return patient when they're admitted on 7/1/2021, but it's flagged as a neither.  Your technique helped me with Patient 2, as using lag does not work for that one (note: the patient was admitted on 7/10/20 - prior to their discharge date on 11/10/20 - that's why this patient is considered neither a new or retuning patient).  Do you know of a way to fix this issue?

Date___AdmitDate___DischargeID___PatientnWANT
9/27/20191/10/202011First_Time
1/28/20214/27/202112Return
7/1/2021.13Return
7/13/2021.14.
     
7/2/202011/10/202021First_Time
7/13/20204/6/202122.
10/22/2020.23.
11/10/20203/31/202124.
5/26/2021.25.
Kurt_Bremser
Super User

IMO, your last observation of patient 2 should be flagged "Return".

This code does it:

data have;
infile datalines dlm="09"x dsd truncover;
input
  date_admit :mmddyy10.
  date_discharge :mmddyy10.
  id_patient $
  n
;
format
  date_admit
  date_discharge yymmdd10.
;
datalines;
9/27/2019	1/10/2020	1	1
1/28/2021	4/27/2021	1	2
7/1/2021	.	1	3	
7/13/2021	.	1	4	.
7/2/2020	11/10/2020	2	1
7/13/2020	4/6/2021	2	2	
10/22/2020	.	2	3	
11/10/2020	3/31/2021	2	4
5/26/2021	.	2	5	
;

data want;
set have;
by id_patient;
retain l_disc;
if first.id_patient
then do;
  want = "First time";
  l_disc = .;
end;
else do;
  if l_disc ne . and date_admit > l_disc then want = "Return";
end;
if date_discharge ne . then l_disc = max(l_disc,date_discharge);
if coalesce(date_discharge,lag(date_discharge)) = . then want = "";
drop l_disc;
run;

Note how example data is presented in readily usable form as a data step with datalines; please do so yourself in the future.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 711 views
  • 0 likes
  • 2 in conversation