I have a lists of patients each with prescription dates. I would like to look at the prescription dates of each patient and see which patient has skipped medication say for 2months (60days). as an example, suppose patient A has prescription on the following dates, 01/Jan/2001, 01/Feb/2001,01/March/2001, 01/Jan/2002, 01/Feb/2002, 01/March/2002.
Because the number of days between the third and the fourth prescription is greater than 60 days, i would like the program to tell me that this patient has skipped medication. Am I making sense?? Can someone help.
You could sort by patient and date, use the lag function to calculate the number of days between successive prescription dates, and then select only those patients where the maximum number of days is greater than 60. For example:
proc sort data=prescription_dates;
by patient date;
if first.patient then lastdate=.;
else lastdate = lag(date);
datediff = date-lastdate;
select distinct patient, max(datediff) as number_of_days
group by patient
having number_of_days gt 60;
A caveat if I may. Be careful when conditionally executing the LAG function. Very often the result is not quite what you expect. Try the following, which is very similar code:
[pre]proc sort data=sashelp.class out=class;
by sex age;
if first.sex then lastage=.;
else lastage = lag(age);
proc print data=lagage;
var sex age lastage;
The problem is solved by executing the LAG function for every observation!!
In your code the two statements become:
lastdate = lag(date);
if first.patient then lastdate=.;[/pre]