BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Babado
Fluorite | Level 6

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?

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

 

View solution in original post

4 REPLIES 4
Reeza
Super User
No idea of how, but out of curiousity what's the use case for such macro development?

Dynamically writing macro's isn't a use case I've seen in 20 years of programming.
PaigeMiller
Diamond | Level 26

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.

 

--
Paige Miller
ballardw
Super User

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.

 

Tom
Super User Tom
Super User

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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1466 views
  • 3 likes
  • 5 in conversation