I usually try to avoid sorting datasets to reduce disk activity, but this might be a worthy exception:
data have;
input (startdate expirydate) (:mmddyy10.) amount ;
format startdate expirydate date9.;
cards ;
1/1/2010 2/2/2020 10
1/5/2011 2/3/2015 10
1/3/2012 2/2/2023 10
1/4/2013 2/2/2014 10
5/5/2015 2/2/2028 10
1/6/2016 2/2/2032 10
run;
proc sort data=have (keep=expirydate amount) out=expiration;
by expirydate;
run;
data want;
set expiration (rename=(expirydate=startdate) in=inexp) have (in=inhave);
by startdate;
cum_amount + ifn(inexp,-1*amount,amount);
if inhave;
run;
... View more