Possible. I would create a temporary column with Medication Date value per PATIENT_ID and subtract DATE_CLAIM from Medication date. I accomplish it in 2 sql steps, but I believe there are ways to calculate it in less steps and more efficient ways. Days after claims get a negative (-) number, if you want only positive numbers, then could handle it using a simple arithmetic operations..
Eg: A little modification to my previous code;
proc sql;
create table want as
select*, min(date_claim) as start format=date11., max(date_claim) as end format=date11.,
case newuse when 1 then date_claim-min(date_claim) else . end as Days_Before_Claim,
case newuse when 1 then max(date_claim)-date_claim else . end as Days_After_Claim,
case newuse when 1 then date_claim else . end as Medication_date
from have
group by patient_id
order by patient_id, date_claim, newuse;
quit;
proc sql;
create table want1 as
select*, max(medication_date)-date_claim as days_count
from want
group by patient_id
order by patient_id, date_claim;
quit;
... View more