Difference between revisions of "Tight Looping with Macro Arrays"

From sasCommunity
Jump to: navigation, search
m (added link to zip)
(gardening on the download link and other formatting issues)
Line 9: Line 9:
 
You can apply the power of macro %DO-looping in the tight space of a single statement by using macro arrays. A
 
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,
 
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
+
etc., plus an additional macro variable with a suffix of "N" that contains the length of the array. Two macros:
and %DO_OVER, make it simple to create and use macro arrays. %ARRAY stores text in a macro array from an
+
* %ARRAY
explicit list of values or from variables in a data set. Then, wherever you need those text values in your program,
+
*%DO_OVER
%DO_OVER loops over the macro array and substitutes the text values wherever you put a "?" in your code phrase.
+
make it simple to create and use macro arrays.
%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.
+
%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,
RENAME %DO_OVER(AA,PHRASE=?=pre_?); renames a list of variables by adding a prefix.
+
*%DO_OVER loops over the macro array and substitutes the text values wherever you put a "?" in your code phrase.
%DO_OVER(AA,MACRO=mymacro) repeatedly executes a macro with a series of values.
+
*%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 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.
 
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:
+
[[Media:Clay-TightLooping-macros.zip|Download]] a zip file containing the three macros
http://www.sascommunity.org/wiki/Image:Clay-TightLooping-macros.zip
+
*Array.sas
 +
*Do_Over.sas
 +
*NumList.sas.
  
 
[[Category:Macro_function_sysfunc]]
 
[[Category:Macro_function_sysfunc]]

Revision as of 10:37, 29 January 2009

Tight Looping With Macro Arrays

Ted Clay

http://www2.sas.com/proceedings/sugi31/040-31.pdf

Abstract:

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
  • %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.

Download a zip file containing the three macros

  • Array.sas
  • Do_Over.sas
  • NumList.sas.