Wednesday, August 13, 2014

SAS: Can I use a macro variable within a macro name? (Yes)

I have need to use a macro variable within a macro name. (Sounds crazy, I know, and it borders on writing "write-only code", but it is a legitimate need.) Not knowing if this was possible, I wrote the following program:

* Can I use a macro variable inside a macro name? ;
* This would allow me to iterate through a series of macros. ;

%macro mac1;
%put mac1 was executed;
%mend mac1;

%macro mac2;
%put mac2 was executed;
%mend mac2;

%macro mac3;
%put mac3 was executed;
%mend mac3;

%macro main;

%do i = 1 %to 3;
    %mac&i;
%end;

%mend main;

%main;



Output was as I had hoped:

mac1 was executed
mac2 was executed
mac3 was executed



So the answer to my question is Yes.







No comments:

Post a Comment