Add-Innovation Home

About Add-Innovation

Contact Us

  SAS Institute logo.    Use macros to control arrays rather than array references.

I once had a particularly difficult support call on a SAS batch job which had failed because the main data library ran out of space. Running out of space for that job wasn't unusual, but the problem was that the job (ran daily) took around 8 hours to run, and if it failed twice, it could impact on the next days run. Part of the processing involved cycling the values which represented weeks, and months across 60 arrays for millions of records. The processing time for the step was hours, and because of the way the cycling was required it was split across 18 do loops.

e.g.

Array  Value(52) 8;
Do Week=1 to 51;
    Value(week)=Value(week+1);
End;
Input NewValue;
Value(52)=NewValue;

My efficiency drive was to change the processing from a standard data step using arrays and do loops, to placing the whole data step within a macro, and replacing the standard do loops with macro %do loops. Instead of referring to the variables as wk(week), they were referred to as wk&week. After the macro was compiled, the resulting datastep was huge, resulting in 52 lines of code instead of 3 for a standard do loop. I had no reference to arrays at all however, and no resulting do loops. Instead of SAS having to look up a data value offset by the value of the do loop, it just referred directly to the value.

e.g. 

%macro bigup;
%Do Week=1 %to 51;
    Value&week=Value%eval(&week+1);
%End;
Input NewValue;
Value52=NewValue;
%mend;

Results in the code ;

Value1=Value2;
Value2=Value3;
Value3=Value4;
...
Value50=Value51;
Value51=Value52;
Input NewValue;
Value52=NewValue;

The processing time for the step reduced by 35 % - even more than I had expected. When I had originally thought of the idea of using macros to refer to the variables, I thought it would be laborious, make the code more complicated to maintain, and make reading the code in the SAS log more tedious, and I was right ! The time savings however, were well worth it, and applying the same techniques throughout the job shaved 3 hours off the run time.