From your description, three cases are possible:
1- Tran before Enrol
2- Tran during Enrol
3- Tran after Expiry
This should handle all:
data have;
infile datalines missover;
input ID (TranDate EnrolDate ExpiryDate) (:date9.);
format TranDate EnrolDate ExpiryDate yymmdd10.;
datalines;
12345 30DEC2014 01JAN2015 31DEC2015 Tran before first Enrol
12345 04JAN2015 01JAN2015 31DEC2015
12345 03DEC2016 01DEC2016 30NOV2017
12345 04FEB2017 01APR2017 31MAR2018
12345 03JAN2018 01JAN2018 31DEC2018
12345 03JAN2019 01JAN2018 31DEC2018 Tran when expired
;
proc sql;
select unique
a.ID,
a.TranDate,
b.EnrolDate,
b.expiryDate,
intck('DAY', a.TranDate, b.Expirydate) as DaysLeftBeforeExpiry
from
have as a left join
have as b on a.ID=b.ID and b.enrolDate <= a.tranDate
group by a.ID, a.TranDate
having intck('DAY', a.TranDate, b.Expirydate) = max(intck('DAY', a.TranDate, b.Expirydate));
quit;
... View more