Tight Looping with Macro Arrays

From sasCommunity
Revision as of 12:56, 13 September 2007 by Rjf2 (Talk | contribs)

Jump to: navigation, search

Tight Looping With Macro Arrays

Ted Clay



You can apply the power of macro %DO-looping in the tight space of a single statement by using macro arrays. A macro array is a list of macro variables that share the same prefix and a numeric suffix, for example, AA1, AA2, AA3, etc., plus an additional macro variable with a suffix of "N" that contains the length of the array. Two macros, %ARRAY and %DO_OVER, make it simple to create and use macro arrays. %ARRAY stores text in a macro array from an explicit list of values or from variables in a data set. Then, wherever you need those text values in your program, %DO_OVER loops over the macro array and substitutes the text values wherever you put a "?" in your code phrase. %DO_OVER(AA,PHRASE=”?”) generates a list with double-quotation marks around each text string. SET %DO_OVER(AA,PHRASE=mylib.?); concatenates a series of data sets. RENAME %DO_OVER(AA,PHRASE=?=pre_?); renames a list of variables by adding a prefix. %DO_OVER(AA,MACRO=mymacro) repeatedly executes a macro with a series of values. The code phrase can consist of many statements, and multiple arrays can be defined and looped-over in parallel. The %ARRAY and %DO_OVER macros turn many time-consuming programming tasks into quick work.

Three macros -- Array.sas, Do_Over.sas and NumList.sas -- are available: http://www.sascommunity.org/wiki/Image:Clay-TightLooping-macros.zip