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.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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