If I understand you correctly, you want to sum a number of variables, based on whether the variable name (which indicates a month) falls within a certain period. If (and only if) your variables occur in the table in the correct order, with no other variables in between, you can try something like this: DATA test;
SET mem(OBS=1);
DT_CHECK = intnx('month', EVENT_DT, -10);
DT_CHECK_STR = cats('Y', put(DT_CHECK, YEAR2.), '_', 'M', month(DT_CHECK));
HYST_DT_STR = cats('Y', put(EVENT_DT, YEAR2.), '_', 'M', month(EVENT_DT));
ARRAY vars[*] Y80_M1--Y12_M3;
DO i=1 TO dim(vars) until(varname(vars(i))=DT_CHECK_STR);
end;
do i=i to dim(vars) until(varname(vars(i))=HYST_DT_STR);
sum_n+vars{i};
end;
FORMAT DT_CHECK mmddyy10.; drop i;
RUN; The first DO loop finds the variable that matches DT_CHECK_STR, the second sums all the variables up to (and including) the one matching HYST_DT_STR. However, it should also be possible to calculate the starting index: data test;
set mem;
array vars(*) Y80_M1--Y12_M3;
DT_CHECK=intnx('month',EVENT_DT,-10);
start=intck('month','01jan1980'd,DT_CHECK)+1;
if start<1 then do;
error 'Start date out of range';
delete;
end;
else if start>dim(vars)-10 then do;
error 'End date out of range';
delete;
end;
do _N_=start to start+10;
sum_n+vars(_N_);
end;
format DT_CHECK mmddyy10.;
run; This method is faster, and it is also safer, due to the range checks. Both methods assume that there are columns for all the months in all the years.
... View more