So just to make sure I understand completely, for patient 1 we would sum all of the quantity_supplied and then divide by the sum of days_supplied, because the last record 6/18/2018 is less than 6 months after the index date. For patient 2 we would do the same thing with all records except the record for 8/31/2017 because this is more than 6 months after the index date.
Is this correct?
If so, try this (UNTESTED CODE):
proc summary data=have(where=(fill_date<intnx('month',index_date,6,'s'))) nway;
class patient_id;
var quantity_supplied days_supplied;
output out=sums sum=;
run;
data want;
set sums;
add = quantity_supplied/days_supplied;
run;
This assumes that your date values are valid numeric SAS date values (representing the integer number of days since 01JAN1960). Otherwise, the code will not work.
--
Paige Miller