BookmarkSubscribeRSS Feed
Statsconsultancy
Fluorite | Level 6
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.
4 REPLIES 4
polingjw
Quartz | Level 8
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:

[pre]
proc sort data=prescription_dates;
by patient date;
run;

data prescription_dates;
set prescription_dates;
by patient;
if first.patient then lastdate=.;
else lastdate = lag(date);
datediff = date-lastdate;
run;

proc sql;
select distinct patient, max(datediff) as number_of_days
from prescription_dates
group by patient
having number_of_days gt 60;
quit;
[/pre]
ArtC
Rhodochrosite | Level 12
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;
run;
data lagage;
set class;
by sex;
if first.sex then lastage=.;
else lastage = lag(age);
run;
proc print data=lagage;
var sex age lastage;
run;[/pre]
The problem is solved by executing the LAG function for every observation!!
In your code the two statements become:
[pre]
lastdate = lag(date);
if first.patient then lastdate=.;[/pre]
Ksharp
Super User
Yes.
Arthur Carpenter is right.
Be careful to use LAG(), It returns the current value when Lag() executes lastly.
And there is short way to use function DIF() which stands for date - lag(date).


Ksharp
deleted_user
Not applicable
You can also try next solution:

data final (drop=date_calc a b);
set test;
by id;

retain date_calc delete a;

if first.id then do;
a=_n_;
delete=0;
end;
else if delete ne 1 then delete=((date-date_calc)>=60);

date_calc=date;

if last.id and delete=1 then do;
b=_n_;
do i=a to b;
set test point=i;
output;
end;
end;
run;

Marius

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 6650 views
  • 2 likes
  • 5 in conversation