I found a previous post that was similar to my needs. The field gap that counts the readmission that I'd like to count the first record within 30 days from the discharge to the readmission AND therefore, if there are more than one admission within 30 days, it should be removed or set to missing. In this case, records 10 (gap=7) and 11 (gap=2) are the second and third readmissions and should be excluded.
I'd appreciate experts in this community to provide assistance. I find it tricky to have both within 30 days for readmission yet only retaining the first readmission when multiple readmissions occur.
Thank you!
Mitch
data sample;
input Px_ID admission:DATE9. discharge:DATE9.;
format admission DATE9.;
format discharge DATE9.;
datalines;
003 01Jan2020 05Jan2020
002 06Aug2020 23Sep2020
002 30Sep2020 15Oct2020
003 05Jun2019 30Sep2019
002 17Jan2019 31Jan2019
002 29Feb2020 11Mar2020
003 24Dec2019 27Dec2019
003 10Mar2020 17Mar2020
003 21Mar2020 28Mar2020
003 04Apr2020 16Apr2020
003 18Apr2020 20Apr2020
003 10May2020 14May2020
003 01Sep2020 03Sep2020
;
*Sort the raw file by Px ID then by admission and discharge dates, respectively;
proc sort data=sample;
by Px_ID admission discharge;
run;
*** a correction ;
*** somewhat right not 100%**;
*Calculate total LOS for Px with readmissions <= 30 days;
data final;
set sample;
by Px_ID admission discharge;
*Add a sequence for the admissions per Px for programming purposes (this is helpful in my 6M+ record database);
retain hosp_seq;
if first.Px_ID then hosp_seq = 0;
hosp_seq = hosp_seq+1;
*Calculate length of stay - adding 1 ensures that Day 1 of admission is counted;
los_calc = discharge - admission + 1;
*Calculate the number of days between Px admissions;
ref_date = LAG(discharge);
format ref_date DATE9.;
gap = admission - ref_date;
if first.Px_ID then do;
ref_date = .;
gap = .;
end;
*Calculate total LOS;
retain total_los;
if first.Px_ID then do;
total_los = .;
end;
** change the following gap with the following one;
if 0 <= gap <= 30 then total_los + los_calc ;
else total_los = .;
if gap>30 then gap=..;
run;
proc print;
run;
results below:
2 | 2 | 29FEB2020 | 11MAR2020 | 2 | 12 | 31JAN2019 | . | . |
3 | 2 | 06AUG2020 | 23SEP2020 | 3 | 49 | 11MAR2020 | . | . |
4 | 2 | 30SEP2020 | 15OCT2020 | 4 | 16 | 23SEP2020 | 7 | 16 |
5 | 3 | 05JUN2019 | 30SEP2019 | 1 | 118 | . | . | . |
6 | 3 | 24DEC2019 | 27DEC2019 | 2 | 4 | 30SEP2019 | . | . |
7 | 3 | 01JAN2020 | 05JAN2020 | 3 | 5 | 27DEC2019 | 5 | 5 |
8 | 3 | 10MAR2020 | 17MAR2020 | 4 | 8 | 05JAN2020 | . | . |
9 | 3 | 21MAR2020 | 28MAR2020 | 5 | 8 | 17MAR2020 | 4 | 8 |
10 | 3 | 04APR2020 | 16APR2020 | 6 | 13 | 28MAR2020 | 7 | 21 |
11 | 3 | 18APR2020 | 20APR2020 | 7 | 3 | 16APR2020 | 2 | 24 |
12 | 3 | 10MAY2020 | 14MAY2020 | 8 | 5 | 20APR2020 | 20 | 29 |
13 | 3 | 01SEP2020 | 03SEP2020 | 9 | 3 | 14MAY2020 | . | . |
Is the posted result your desired result or the result you get from your posted code?
Closely but need one more condition. I am reposting the syntax and the output. The values in Gap field are the days from the readmission to the previous discharge. Currently, they are within 30 days meeting the requirement. However, one additional condition is that the re-admission can only count the first if multiple readmissions occur within 30 days.
In this case, obs is the first readmission (21Mar) from discharge (17Mar), however, obs 10 is also the readmission 04Apr (from discharge 17Mar) but the second readmission within 30 days from discharge 17Mar. therefore, should not be counted. The same applies to obs 11. Obs 12 starts a new readmission because it has passed 30 days from the first readmission (17Mar).
the output should be:
Obs Px_ID admission discharge hosp_seq los_calc ref_date gap total_los
. | ||||||||
. | ||||||||
2 | 2 | 29FEB2020 | 11MAR2020 | 2 | 12 | 31JAN2019 | . | . |
3 | 2 | 06AUG2020 | 23SEP2020 | 3 | 49 | 11MAR2020 | . | . |
4 | 2 | 30SEP2020 | 15OCT2020 | 4 | 16 | 23SEP2020 | 7 | 16 |
5 | 3 | 05JUN2019 | 30SEP2019 | 1 | 118 | . | . | . |
6 | 3 | 24DEC2019 | 27DEC2019 | 2 | 4 | 30SEP2019 | . | . |
7 | 3 | 01JAN2020 | 05JAN2020 | 3 | 5 | 27DEC2019 | 5 | 5 |
8 | 3 | 10MAR2020 | 17MAR2020 | 4 | 8 | 05JAN2020 | . | . |
9 | 3 | 21MAR2020 | 28MAR2020 | 5 | 8 | 17MAR2020 | 4 | 8 |
10 | 3 | 04APR2020 | 16APR2020 | 6 | 13 | 28MAR2020 | . | 21 |
11 | 3 | 18APR2020 | 20APR2020 | 7 | 3 | 16APR2020 | . | 24 |
12 | 3 | 10MAY2020 | 14MAY2020 | 8 | 5 | 20APR2020 | 20 | 29 |
13 | 3 | 01SEP2020 | 03SEP2020 | 9 | 3 | 14MAY2020 | . | . |
data sample;
input Px_ID admission:DATE9. discharge:DATE9.;
format admission DATE9.;
format discharge DATE9.;
datalines;
003 01Jan2020 05Jan2020
002 06Aug2020 23Sep2020
002 30Sep2020 15Oct2020
003 05Jun2019 30Sep2019
002 17Jan2019 31Jan2019
002 29Feb2020 11Mar2020
003 24Dec2019 27Dec2019
003 10Mar2020 17Mar2020
003 21Mar2020 28Mar2020
003 04Apr2020 16Apr2020
003 18Apr2020 20Apr2020
003 10May2020 14May2020
003 01Sep2020 03Sep2020
;
*Sort the raw file by Px ID then by admission and discharge dates, respectively;
proc sort data=sample;
by Px_ID admission discharge;
run;
*Calculate total LOS for Px with readmissions <= 30 days;
data final;
set sample;
by Px_ID admission discharge;
*Add a sequence for the admissions per Px for programming purposes (this is helpful in my 6M+ record database);
retain hosp_seq;
if first.Px_ID then hosp_seq = 0;
hosp_seq = hosp_seq+1;
*Calculate length of stay - adding 1 ensures that Day 1 of admission is counted;
los_calc = discharge - admission + 1;
*Calculate the number of days between Px admissions;
ref_date = LAG(discharge);
format ref_date DATE9.;
gap = admission - ref_date;
if first.Px_ID then do;
ref_date = .;
gap = .;
end;
*Calculate total LOS;
retain total_los;
if first.Px_ID then do;
total_los = .;
end;
** change the following gap with the following one;
*if 0 <= gap <= 30 then total_los + los_calc + lag(total_los);
if 0 <= gap <= 30 then total_los + los_calc ;
else total_los = .;
if gap>30 then gap=.;
run;
proc print ;
run;
2 | 2 | 29FEB2020 | 11MAR2020 | 2 | 12 | 31JAN2019 | . | . |
3 | 2 | 06AUG2020 | 23SEP2020 | 3 | 49 | 11MAR2020 | . | . |
4 | 2 | 30SEP2020 | 15OCT2020 | 4 | 16 | 23SEP2020 | 7 | 16 |
5 | 3 | 05JUN2019 | 30SEP2019 | 1 | 118 | . | . | . |
6 | 3 | 24DEC2019 | 27DEC2019 | 2 | 4 | 30SEP2019 | . | . |
7 | 3 | 01JAN2020 | 05JAN2020 | 3 | 5 | 27DEC2019 | 5 | 5 |
8 | 3 | 10MAR2020 | 17MAR2020 | 4 | 8 | 05JAN2020 | . | . |
9 | 3 | 21MAR2020 | 28MAR2020 | 5 | 8 | 17MAR2020 | 4 | 8 |
10 | 3 | 04APR2020 | 16APR2020 | 6 | 13 | 28MAR2020 | 7 | 21 |
11 | 3 | 18APR2020 | 20APR2020 | 7 | 3 | 16APR2020 | 2 | 24 |
12 | 3 | 10MAY2020 | 14MAY2020 | 8 | 5 | 20APR2020 | 20 | 29 |
13 | 3 | 01SEP2020 | 03SEP2020 | 9 | 3 | 14MAY2020 | . | . |
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.