This works for me if you wanted to do it in one step... data has to be sorted first though as it assumes max of lag4.
DATA example; INPUT ID $ date DATE9. VAR; DATALINES; BANK1 31DEC2014 3 BANK1 31MAR2015 5 BANK1 30JUN2015 3 BANK1 30SEP2015 8 BANK1 31DEC2015 5 BANK1 31MAR2016 4 BANK2 31MAR2015 9 BANK2 30JUN2015 4 BANK2 30SEP2015 7 BANK2 31MAR2016 4 BANK3 31DEC2014 8 BANK3 31DEC2015 3 BANK3 31MAR2016 2 ; DATA example_new (keep=id date var ind); attrib date year_end format=date9.; attrib store_val1 store_val2 store_val3 store_val4 format=date9.; attrib val1-val4 format=8.; set example; ind = .; store_val1 = lag1(date); store_val2 = lag2(date); store_val3 = lag3(date); store_val4 = lag4(date); val1 = LAG1(VAR); val2 = LAG2(VAR); val3 = LAG3(VAR); val4 = LAG4(VAR); year_end = intnx('year', date, 0, 'end'); if (intck('year', date , store_val1) = -1 and year_end = date) then IND = SUM(VAR,val1); else if intck('year', date , store_val2) = -1 and year_end = date then IND = SUM(VAR,val2); else if intck('year', date , store_val3) = -1 and year_end = date then IND = SUM(VAR,val3); else if intck('year', date , store_val4) = -1 and year_end = date then IND = SUM(VAR,val4); else ind =.; RUN;
... View more