BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SAS_inquisitive
Lapis Lazuli | Level 10

Hello,

 

What is the purpose of naming macro variable same as the  parameter name?

 

%macro test (param1 = , param2 = ,)
  %local param1 param2;
  %let param1 = &param1;
  %let param2 = &param2;
  
  .....
  .....
%mend test;
%test

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

There is no purpose.  All macro parameters are by definition local macro variables.

It does nothing to re-tell the macro processor that they are LOCAL.  You can repeat a %LOCAL statement as many times as you want.

Note that the %LET statement is also fine.  You are free to use the parameters the same as any other local macro variable.

The current code does nothing.  There is some times a value it setting a macro variable to its value, but that is only to remove leading or trailing spaces that are not macro quoted.  But for any leading or trailing spaces to be in the parameter value they would have to be macro quoted. So the %let statements are doing nothing.

View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

There is no purpose.  All macro parameters are by definition local macro variables.

It does nothing to re-tell the macro processor that they are LOCAL.  You can repeat a %LOCAL statement as many times as you want.

Note that the %LET statement is also fine.  You are free to use the parameters the same as any other local macro variable.

The current code does nothing.  There is some times a value it setting a macro variable to its value, but that is only to remove leading or trailing spaces that are not macro quoted.  But for any leading or trailing spaces to be in the parameter value they would have to be macro quoted. So the %let statements are doing nothing.

ballardw
Super User

Padding a work invoice if paid by the line of code written? 😈

Astounding
PROC Star

While I agree with the other posters, there is a similar useful case that you might be mixing this up with:

 

%test (param1=&param1, param2=&param2)

This is saying that there are GLOBAL macro variables named PARAM1 and PARAM2.  When running the %TEST macro, copy the GLOBAL version of PARAM1 to the LOCAL version that is part of %TEST.  Similarly, copy the GLOBAL version of PARAM2 to the LOCAL version that is part of %TEST.

SAS_inquisitive
Lapis Lazuli | Level 10
@ Astounding. Could you give an example showing this is useful? Thanks.
Tom
Super User Tom
Super User

@SAS_inquisitive wrote:
@ Astounding. Could you give an example showing this is useful? Thanks.

Where WHAT is useful? 

 

The purpose of parameter on a macro is to pass in a value to the macro.  All @Astounding showed is that you can resolve the value of a macro variable to generate the value to be passed. It doesn't make any difference if the macro being referenced in the call happens to have the same name as the parameter or not.

 

The reason to use a similar (or identical) name is more obvious when you use names that have some meaning to the humans reading/writing the code instead of PARAM1 and PARAM2.  So if your macro needs the name of a dataset then you might use a parameter name of DSNAME when creating a macro.  Similarly if you have some code that wants to call that macro and it needs to the name of the dataset in a macro variable you might also want to name the macro variable DSNAME.  So then it would make sense to call the macro with code like :  %mymacro(dsname=&dsname).   But if your calling environment was using NEXTDS as the macro variable name where it stored the dataset name then the call would be: %mymacro(dsname=&nextds) intstead.

 

Astounding
PROC Star

Here's a theoretical example of "useful".

 

Let's say you have a macro that is working well and does exactly what you need.  However, the macro utilizes some global macro variables created earlier in the program.  What happens if you want to use that macro somewhere else, in some other program that hasn't run all the earlier code?  The new program might not have the same set of global macro variables available, or it might want to supply different values for those global variables.  So you modify your beautiful, well-working macro by adding two parameters to the macro definition.  For the original program, calling the macro supplies the values of those global macro variables as the parameters.  But for a new program, you could supply other values to those parameters, values that are relevant to the new program.

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 6 replies
  • 1151 views
  • 5 likes
  • 4 in conversation