/***************************************************************************************************************** SAS file name: macro_array.sas File location: __________________________________________________________________________________________________________________ Purpose: To explore the concept of macro arrays by example of the %Array and %Do_Over macros. Author: Peter Clemmensen Creation Date: 16/06/2020 This program supports the blog post "Investigating the Macro Array in SAS" on SASnrd.com *****************************************************************************************************************/ /* %Array Macro Examples */ /* Simple values option */ %array(a, values=one two three) %put &a1; %put &a2; %put &a3; %put &an; /* List in values option - Numlist macro must be compiled! */ %array(num, values=1-3) %put &num1; %put &num2; %put &num3; %put &numn; /* Macro array from data set variable */ %array(names, data=sashelp.class(where=(sex="M")), var=name) %put &names1; %put &namesn; /* %Do_Over Macro Examples */ /* A simple %Do_Over example */ %array(a, values=one two three) %put %do_over(a); /* The Phrase Argument is very important */ %array(arr, values=one two three) %put %do_over(arr, phrase=?); %put %do_over(arr, phrase="?"); %put %do_over(arr, phrase="?", between=%str(,)); %put %do_over(arr, phrase="?", between=comma); /* Calling macros - Only positional parameter*/ %macro PutIt(x); %put &x.; %mend; %array(arr, values=one two three) %do_over(arr, macro=PutIt) /* Equivalent to */ %PutIt(&a1) %PutIt(&a2) %PutIt(&a3) /* Calling macros with positional parameters */ %macro PutIt2(x=); %put &x.; %mend; %array(arr, values=one two three) %do_over(arr, macro=PutIt2, keyword=x) /* Equivalent to */ %do_over(arr, phrase=%nrstr(%PutIt2(x=?))) /* Build entire statements and expressions */ %array(arr, values=one two three) options mprint; data test; %do_over(arr, phrase=?=1;) run; options nomprint; %array(names, data=sashelp.class(where=(sex="M")), var=name) options mprint; data test; set sashelp.class; %do_over(names, phrase=if name="?" then ?=1;, between=else) run; options nomprint;