How to Pass a Varying List of Parameters to a Macro
Ever found yourself coding something like this, where you call the same macro x number of times for a list of variables:
%macro MyMacro(MyVar); ...... Perform some function against &MyVar ...... %mend; %MyMacro(a); %MyMacro(b); %MyMacro(c);
Now consider this :
%macro MyList(MyVar)/parmbuff; %let i = 1; /* Get the first Parameter */ %let parm_&i = %scan(&syspbuff,&i,%str(%(,%))); /* While current parm is not blank */ %do %while (%str(&&parm_&i.) ne %str()); ...... Perform some function against &&parm_&i ...... %let i = %eval(&i+1); /* Get next parm */ %let parm_&i = %scan(&syspbuff,&i,%str(%(,%))); %end; %mend ; %MyList(a,b,c);
All we are doing in the here is calling the Macro with a varying list of parameters and then looping through that list, performing the same action against each entry. All we need do to achieve this is use the /PARMBUFF option on the macro definition, and then extract each item individually from the &SYSPBUFF macro variable.