I want to create several macros with similar names, for example, m1, m2, m3, m4, m5. Since their name might change, I would prefer to name them using a macro variable so that the only thing I have to change in case the name changes is the macro variable.
For example, instead of having
%macro m1;
...
%mend;
...
%macro m5;
...
%mend;
I would like to have
%let c=1;
%macro m&c.;
...
%mend;
...
%let c=5;
%macro m&c.;
...
%mend;
However, I believe you cannot use the '&' symbol in macro's names. Is there any other way to do this?
Sounds like a recipe for confusion.
Use the information to write a text file with the desired code and %include the file for the macro definition.
You can call macro with the macro variable in the name after it is defined.
Something like this:
%let c=p; data _null_; file "<path>\dummy&c.sas"; put "%macro dummy&c ();"; put " proc print data=sashelp.class;"; put " run;"; put "%mend;"; run; %include "<path>\dummy&c.sas"; %dummy&c.;
If you place the file in an AUTOCALL library location and are careful with the file names you wouldn't need the %include step to compile the macro. This works because the macro variable in the %macro statement gets resolved in the data step and written as text and not seen by the macro compiler until the %include attempts to compile it.
However, I believe you cannot use the '&' symbol in macro's names. Is there any other way to do this?
If you are getting errors, turn on the macro debugging options, run your code again, and show us the log. Don't make us guess what is wrong.
options symbolgen mlogic mprint;
Also, agreeing with @Reeza whatever you are doing, there are probably easier methods. And so this is critically important: explain what you are trying to do.
Sounds like a recipe for confusion.
Use the information to write a text file with the desired code and %include the file for the macro definition.
You can call macro with the macro variable in the name after it is defined.
Something like this:
%let c=p; data _null_; file "<path>\dummy&c.sas"; put "%macro dummy&c ();"; put " proc print data=sashelp.class;"; put " run;"; put "%mend;"; run; %include "<path>\dummy&c.sas"; %dummy&c.;
If you place the file in an AUTOCALL library location and are careful with the file names you wouldn't need the %include step to compile the macro. This works because the macro variable in the %macro statement gets resolved in the data step and written as text and not seen by the macro compiler until the %include attempts to compile it.
You cannot use macro expressions in the NAME of a macro.
Use some other code generator to generate the source code for your macros. Like CALL EXECUTE() or writing text to a file and using %INCLUDE to run it.
If you would explain why you thought that might be useful perhaps someone can show you another way that will work.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.