I am trying to count the duration of medication use. I would like to count days that every outpatient visit if they are not exceed 90 days between every visit.
The med_date is the date of every outpatient visit. I first use lag function to count the days between each visit(variable diff).
And I want to retain them and stop if the diff>90.
Then keep going with another id.
My code would stop if the diff>90, but I have no idea how to keep going
`data have;
input ID $ med_date:yymmdd.;
format med_date yymmdd10.;
cards;
101 2010-11-22
101 2010-11-23
101 2010-11-30
101 2015-03-08
102 2015-01-11
102 2015-01-15
102 2015-02-01
102 2015-02-15
103 2021-03-17
103 2021-03-26
103 2021-04-08
104 2022-02-05
104 2023-03-16
run;`
`data have1;
set have;
by id;
lag=lag(med_date);
format lag yymmdd10.;
if first.id then
dif=0;
else dif=med_date-lag;
run;`
enter image description here
The outcome would just stop in the first diff that>90 and would not keep going with another id
Taylor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.