Hello Community,
I need to create a variable in my dataset to indicate whether or not a patient received a second prescription of a medication within 34 days of their initial prescription (i.e., the patient has one or more unique prescription dates within 34 days of the index prescription date). Please see below for how my dataset is currently organized and how I would like the output to be like.
Note that my data is already sorted ascending by prescription date, therefore the first date listed for each patient is the index date (i.e., the first prescription received). Also note that sometimes multiple rows of data are entered for the same prescription, which can result in the same prescription date shown more than once for a patient. Therefore, it is important that the code identifies whether a UNIQUE date is within 34 days of the index date. Finally, please note that patients who did not receive the medication will have the prescription_date variable equal to missing. In these cases, it should be new_var=0 since the condition would be false for these patients as well. Any help with the coding for doing this would be greatly appreciated!!
Have:
Patient_id | Prescription_date |
001 | 17JUN2020 |
001 | 23JUL2020 |
001 | 31JUL2020 |
002 | 15FEB2020 |
002 | 15FEB2020 |
002 | 30MAR2020 |
003 | 02JAN2021 |
003 | 02JAN2021 |
004 | 21APR2021 |
004 | 20JUN2021 |
004 | 01JUL2021 |
005 | 19OCT2021 |
005 | 25OCT2021 |
005 | 25OCT2021 |
005 | 28SEP2021 |
006 | . |
Want:
Patient_id | New_var |
001 | 1 |
002 | 0 |
003 | 0 |
004 | 0 |
005 | 1 |
006 | 0 |
See this:
data have;
input Patient_id $;
input Prescription_date :date9.;
format Prescription_date yymmdd10.;
datalines;
001
17JUN2020
001
23JUL2020
001
31JUL2020
002
15FEB2020
002
15FEB2020
002
30MAR2020
003
02JAN2021
003
02JAN2021
004
21APR2021
004
20JUN2021
004
01JUL2021
005
19OCT2021
005
25OCT2021
005
25OCT2021
005
28SEP2021
006
.
;
data want;
set have;
by patient_id;
retain new_var index;
if first.patient_id
then do;
index = prescription_date;
new_var = 0;
end;
else if 1 le prescription_date - index le 43 then new_var = 1;
if last.patient_id;
keep patient_id new_var;
run;
Supplying example data in a data step with datalines makes helping you easier for us.
See this:
data have;
input Patient_id $;
input Prescription_date :date9.;
format Prescription_date yymmdd10.;
datalines;
001
17JUN2020
001
23JUL2020
001
31JUL2020
002
15FEB2020
002
15FEB2020
002
30MAR2020
003
02JAN2021
003
02JAN2021
004
21APR2021
004
20JUN2021
004
01JUL2021
005
19OCT2021
005
25OCT2021
005
25OCT2021
005
28SEP2021
006
.
;
data want;
set have;
by patient_id;
retain new_var index;
if first.patient_id
then do;
index = prescription_date;
new_var = 0;
end;
else if 1 le prescription_date - index le 43 then new_var = 1;
if last.patient_id;
keep patient_id new_var;
run;
Supplying example data in a data step with datalines makes helping you easier for us.
@Kurt_Bremser This is perfect. Thank you!!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.