Hello,
I have a dataset with claims of medication, each line / observation is one claim. I have the patient_id and the date of the claim. Now I created a variable, which counts up the several claims per patient between the years 2014 and 2020, beginning with 1 for the first claim, 2 for the second, 3 for the third... The maximum number viaries from patient to patient.
Now I want to create a new variable, which counts the number of days between one claim to the previous within each patient, meaning the number of days between the second to the first, the third to the second...
I would be very happy for your help, thank you in advance.
Example data helps. Such things as are your dates actually SAS date values (amazing how many people ask about manipulating "dates" that are actually character strings)? Is the data already sorted by patient_id and date?
Dummy code assuming the response to both questions above is yes:
data want; set have; by patient_id; days = dif(datevariable); if first.patiend_id then days=.; /* or 0 if peferred*/ run;
When you use BY group processing in a data step SAS creates automatic variables that indicate whether a record is the first or last of a group, that are 1/0 for true/false, and referenced with the First. or Last. (pay attention to the dot in there).
The DIF function returns the difference of the current value of a variable with that of the previous record if used carefully. Since SAS date values are a number of days then DIF is the number of days between dates.
Example data helps. Such things as are your dates actually SAS date values (amazing how many people ask about manipulating "dates" that are actually character strings)? Is the data already sorted by patient_id and date?
Dummy code assuming the response to both questions above is yes:
data want; set have; by patient_id; days = dif(datevariable); if first.patiend_id then days=.; /* or 0 if peferred*/ run;
When you use BY group processing in a data step SAS creates automatic variables that indicate whether a record is the first or last of a group, that are 1/0 for true/false, and referenced with the First. or Last. (pay attention to the dot in there).
The DIF function returns the difference of the current value of a variable with that of the previous record if used carefully. Since SAS date values are a number of days then DIF is the number of days between dates.
Do you ever have multiple claims with the same date from a single patient?
If so, what rule do you want to establish for the new variable? I can imagine a couple possibilities:
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.