Happy weekend, I am trying to find the time to readmission (last column). I ordered my data by ID and I wish to find the time from the previous hospital discharge to the next readmission. For example. for ID 1, I would like to know the time between 01/30/17 and 03/27/19, and 04/10/19 and 07/02/20. I am unsure of how to do this in SAS. Any help is appreciated
ID | BirthDate | Admit Date | Discharge Date | Times ill | Time to readmission |
1 | 12/07/95 | 01/01/17 | 01/30/17 | 1 |
|
1 | 12/07/95 | 03/27/19 | 04/10/19 | 2 |
|
1 | 12/07/95 | 07/02/20 | 07/25/20 | 3 |
|
2 | 12/24/99 | 03/13/16 | 03/27/16 | 1 |
|
2 | 12/24/99 | 10/20/19 | 11/03/19 | 2 |
|
3 | 07/26/98 | 05/10/17 | 05/30/17 | 1 |
|
4 | 09/29/82 | 06/30/18 |
| 1 |
|
data have;
input ID (BirthDate Admitdate DischargeDate) (:mmddyy10.) Timesill;
format BirthDate Admitdate DischargeDate mmddyy10.;
cards;
1 12/7/1995 1/1/2017 1/30/2017 1
1 12/7/1995 3/27/2019 4/10/2019 2
1 12/7/1995 7/2/2020 7/25/2020 3
2 12/24/1999 3/13/2016 3/27/2016 1
2 12/24/1999 10/20/2019 11/3/2019 2
3 7/26/1998 5/10/2017 5/30/2017 1
4 9/29/1982 6/30/2018 . 1
;
data want;
do until(last.id);
set have;
by id;
if not first.id then Timetoreadmission=Admitdate-_n_;
_n_=DischargeDate;
output;
end;
run;
UNTESTED CODE (If you want tested code, please provide the data as a SAS data step)
data want;
set have;
by id;
prev_discharge=lag(discharge_date);
if not first.id then time_to_readmission=admit_date-prev_discharge;
else time_to_readmission=.;
drop prev_discharge;
run;
data have;
input ID (BirthDate Admitdate DischargeDate) (:mmddyy10.) Timesill;
format BirthDate Admitdate DischargeDate mmddyy10.;
cards;
1 12/7/1995 1/1/2017 1/30/2017 1
1 12/7/1995 3/27/2019 4/10/2019 2
1 12/7/1995 7/2/2020 7/25/2020 3
2 12/24/1999 3/13/2016 3/27/2016 1
2 12/24/1999 10/20/2019 11/3/2019 2
3 7/26/1998 5/10/2017 5/30/2017 1
4 9/29/1982 6/30/2018 . 1
;
data want;
do until(last.id);
set have;
by id;
if not first.id then Timetoreadmission=Admitdate-_n_;
_n_=DischargeDate;
output;
end;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.