Hello,
I am trying to write a program that will enable me to count medication segments by thousands of patients. An example:
let's say a patient fill a Rx for the months of January, February and March 2017 and stops. Then the same member resumes the same medication(s) for June and July 2017 and stops. He (possibly) comes back in November and continues to December 2017. This will be counted as 3 treatments segments. I understand that this particular patient may be a rare case but I will not be able to know for sure.
How can I code to obtain this count. I already performed the transpose of the data by Fill_date and by Days_Supply. I still have the raw data. I am using 9.2 Any help will be greatly appreciated.
Thanks
First is to insure that your date variables are all SAS date valued variables which are numeric and if they appear to be date have a SAS date related format. This is important as calculations of intervals or durations are simple with the SAS date valued variables but massive headaches otherwise (unless the values are turned into SAS date values).
You will have to clearly specify how long of an interval is required to be treated as a "segment break" or what ever you may think of that interval.
It helps if you can provide a little example data in the form of a data step so we don't have to guess about the content of the data.
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 {i} icon or attached as text to show exactly what you have and that we can test code against.
Here may be something enough to get you started:
data need; set example; by id filldate; retain segment; lfd = lag(filldate); lds = lag(dayssupply); if first.id then do; daysbreak = 0; segment = 1; end; else daysbreak = filldate -(lfd+lds); if daysbreak> 0 then segment+1; drop lfd lds; run;
if you need to track individual and differing medications they the sort would be by ID Medication Filldate, the by in the data step the same, and the If First.ID would become If first.medication.
The FIRST. variable indicator comes with use of BY group processing and each variable on the by statement, along with a LAST. to indicate the start and end of groups.
The Daysbreak variable in the data set need contains the number of days from the end of prior prescription fill + days supplied and the current fill date. If the value is negative or 0 then no break occurs.
First is to insure that your date variables are all SAS date valued variables which are numeric and if they appear to be date have a SAS date related format. This is important as calculations of intervals or durations are simple with the SAS date valued variables but massive headaches otherwise (unless the values are turned into SAS date values).
You will have to clearly specify how long of an interval is required to be treated as a "segment break" or what ever you may think of that interval.
It helps if you can provide a little example data in the form of a data step so we don't have to guess about the content of the data.
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 {i} icon or attached as text to show exactly what you have and that we can test code against.
Here may be something enough to get you started:
data need; set example; by id filldate; retain segment; lfd = lag(filldate); lds = lag(dayssupply); if first.id then do; daysbreak = 0; segment = 1; end; else daysbreak = filldate -(lfd+lds); if daysbreak> 0 then segment+1; drop lfd lds; run;
if you need to track individual and differing medications they the sort would be by ID Medication Filldate, the by in the data step the same, and the If First.ID would become If first.medication.
The FIRST. variable indicator comes with use of BY group processing and each variable on the by statement, along with a LAST. to indicate the start and end of groups.
The Daysbreak variable in the data set need contains the number of days from the end of prior prescription fill + days supplied and the current fill date. If the value is negative or 0 then no break occurs.
Thank you so much ballardw,
I was able to obtain exactly what I needed from your answer. All I needed to add was some minor customization, based on the ask, and the result is correct. I tested a sample to be certain and everything is good.
Thanks a million.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.