Hi abhi309,
For your requirement of "if the first prescription is within 180 days from 1st Jan 2020 then I don't want to include any subsequent prescriptions until the day difference between the consecutive prescriptions are more than 180 days and including all the prescriptions thereafter. But if the first prescription is after 180 days from 1st Jan 2020 then I can include all the prescriptions without bothering about the day difference", it can be simplified as: "do not include any prescription within 180 days from 01JAN2020". And thus my answer is created like this.
I also created a data set which calculate the days between two consecutive prescriptions for one subject(or participant, or patient whatever term you use) for possible further usage. The reference code for using LAG function to calculate numeric difference of any consecutive values for one subject is from SAS Programming 3: Advanced Techniques, course notes PDF, on page 32, no.4.
data preclaim;
input PT_ID PRES_DATE date9.;
format pres_date date9.;
datalines;
1 5Jan2020
1 8Feb2020
1 10Jan2021
1 8Feb2021
2 15Aug2020
2 19Sep2020
2 18Oct2020
3 9Jan2020
3 10Oct2020
3 15Oct2020
4 10Jul2020
4 15Jul2020
4 20Mar2021
;
run;
proc print data=preclaim;run;
/*add include flag as 0 or 1*/
data preclaim1;
set preclaim;
if pres_date<'01jan2020'd+179
then include=0;
else include=1;
run;
proc print data=preclaim1;run;
/*calculate the number of days between
any two consecutive prescription claims
for one subject, for futher usage*/
data preclaim2;
set preclaim;
by pt_id;
presdlag=lag1(pres_date);
if first.pt_id then presdlag=.;
diffdays=pres_date-presdlag;
format presdlag date9.;
run;
proc print data=preclaim2;run;
... View more