This program will check before 7 days for each record's date and if the count is ge 3 then will print the patient id and drug name corresponding to it.
data pat;
input ID DRUG : $10. d_DATE : yymmdd10.;
format d_date date9.;
cards;
100001 ADVIL 2010-03-25
100001 TILENOL 2010-04-08
100001 TILENOL 2010-04-10
100001 TILENOL 2010-04-12
100001 TILENOL 2010-04-14
100001 TILENOL 2010-04-28
100001 TILENOL 2010-05-01
100002 ADVIL 2010-04-01
100002 ADVIL 2010-04-09
100002 ADVIL 2010-04-10
100002 ADVIL 2010-04-12
100002 ADVIL 2010-04-14
100002 TILENOL 2010-04-28
100002 TILENOL 2010-05-01
;run;
proc sql;
select unique id, drug
from
(
select
id,
drug,
d_date,
count(*) as cnt
from
(
select
a.*,
b.id as b_id,
b.drug as b_drug,
b.d_date as b_d_date
from pat a
join pat b on a.id=b.id
and a.drug=b.drug
and a.d_date between b.d_date-7 and b.d_date
) a
group by 1,2,3
having count(*)>=3
);
quit;
... View more