Hello,
I'm trying to compute # of days (p_dayscovered) a patient is taking medication over the course of 365 days starting 11/18/2016. The Prescription Date is the day when the patient is prescribed or refills their medication and the # of days of prescription supplied is how many days they got the medication for.
For example, in the table below, on 11/8, the patient gets the prescription for 7 days. It is assumed they start taking the medication on the prescription date. So the medication would run out on 11/8 + 7 days = 11/14. The patient gets the next medication on 11/15 for 21 days which means there is no gap in the medication between first prescription and the refill.
However, after the refill on 12/6 for 28 days, the patient goes off the medication for a while before starting on another drug on 3/8/17. So, we would stop counting for p_dayscovered after 12/6 + 28 days, but restart starting 3/8/17.
data WORK.CLASS(label='Prescription Data'); infile datalines dsd truncover; input Drug:$1 Prescription_Date:MMDDYY8. Days_Supplied:32.; Datalines; A 11/8/16 7 A 11/15/16 21 A 12/6/16 28 B 3/8/17 30 B 4/7/17 -4 A 4/3/17 3 A 5/15/17 3 A 6/5/17 3 B 6/14/17 30 A 10/5/17 15 A 10/25/17 15 ;;;;
The table looks like this:
Drug | Prescription Date | # of days of prescription supplied |
A |
11/8/16 | 7 |
A | 11/15/16 | 21 |
A | 12/6/16 | 28 |
B | 3/8/17 | 30 |
B | 4/7/17 | -4 |
A | 4/3/17 | 3 |
A | 5/15/17 | 3 |
A | 6/5/17 | 3 |
B | 6/14/17 | 30 |
A | 10/5/17 | 15 |
A | 10/25/17 | 15 |
Here is my code to compute the p_dayscovered:
*"daydummy" array creates a dummy variable for each day in the review period;
*"filldates" & "days_supply" arrays groups the Prescription Date & # of Days supplied variables, setting up the DO loops;
data pdc_3M_12M;
set both_3M_12M;
array daydummy(365) day1-day365;
array filldates(*) svcdate1-svcdate%eval(&max_3M.);
array days_supply(*) daysupp1-daysupp%eval(&max_3M.);
do ii=1 to 365; daydummy(ii)=0;end;
do ii=1 to 365;
do i = 1 to dim(filldates) while (filldates(i) ne .);
*Flags the days of the review period that the patient was supplied the medication;
if filldates(i)<= start_dt + ii -1 <= filldates(i)+days_supply(i)-1
then daydummy(ii)=1;
end;
end;
drop i ii;
*dayscovered sums the daydummy variables. This sum is used as the numerator in calculating p_dayscovered, the proportion of days medication was supplied in the 365 day study period;
dayscovered= sum(of day1 - day365);label dayscovered= "Total Days Covered";
p_dayscovered=dayscovered/365; label p_dayscovered= "Proportion of Days Covered";
run;
The code works in all cases except the overlap edge case which I have highlighted in red, which is:
In this case, the counting for Drug B should stop on 4/2/17 and the counting for Drug A should begin starting on 4/3/17.
My code adds a '1' flag for all days starting 3/8/17 + 30 days = 4/6/17 whereas, I want the code to stop adding the '1' flag on 4/2/17 and then add the '1' flags starting 4/3/17 + 3 ending on 4/5/17.
The total p_dayscovered = 150 because of what I mentioned above, but my code because of not accounting for the overlap counts it as 151
Can someone help me figure out how to write this overlap exception condition?
Thanks so much!!
Please post data in usable form, those tables are nice to look at, but completely useless as data source.
It seems, by the way, strange, that there is no variable identifying the patient in your data.
Where does the macro variable &max_3m get its value? What value does it have in your example?
What the heck is negative number of days? Does someone mug the patient and take pills away?
Are you dates actual SAS date values or character values?
Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the </> icon or attached as text to show exactly what you have and that we can test code against.
Remove any sensitive information but I suspect that a patient identifier is going to be needed.
This may be easier to deal with odd cases by making a long data set. Start by sorting by Patient identifier, drug and prescription date (which pretty much requires an actual SAS date value).
You might have to show how each # of days figure is calculated for each line so we understand better.
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.