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

Hi everyone, 

    Is there a code or related option to get all macro parameters from a macro?

    Just like, someone told me that such a macro(%gen) can be used.

    Actually this macro has 2 macro parameters, %gen(in=,out=) .

    but I don't how to use it and how many parameters in it, so is there a macro or code or option can help me to put all macro parameters in log?

    Just like %gen(in=,out=) be print in log.

    Thanks!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

A properly-built production macro would return a help screen when called without the proper parameters, or called with the help keyword.

 %gen()

 %gen(help)

 

Short of this, try turning on options symbolgen and mlogic. You might see unhappy messages in the log about macro variables with invalid values. 

 

Can't you get the source code?

 

View solution in original post

8 REPLIES 8
Jagadishkatam
Amethyst | Level 16
I believe the best and only way to check the macro parameters is that we need to check the macro code and its parameters. I don't think there is any alternative way unless we see the macro code.
Thanks,
Jag
ChrisNZ
Tourmaline | Level 20

A properly-built production macro would return a help screen when called without the proper parameters, or called with the help keyword.

 %gen()

 %gen(help)

 

Short of this, try turning on options symbolgen and mlogic. You might see unhappy messages in the log about macro variables with invalid values. 

 

Can't you get the source code?

 

Reeza
Super User
No. You need to know and understand the macro to use it. Depending on the macro you can sometimes get the code behind it depending on how it's been compiled and stored.



Astounding
PROC Star

The good news in a nutshell:  this is relatively easy.

 

The bad news in a nutshell:  you may not be able or willing to do the work involved.

 

Here's the background.  Macro parameters are temporary.  Before a macro runs, they don't exist.  After a macro finishes executing, they get erased.  The only time you can retrieve their value is while the macro is executing.  Therefore to get what you are asking for, you will have to modify the definition of every macro.  The modification might be small, but you have to change every macro.  Here's an example of a change that would handle the simple cases.  As the first line within every macro definition, add a statement:

%put _local_;

As the macro begins to execute, the only %local variables  in its symbol table are the parameters.  So this statement will give you want you want, sort of.  This works well when each macro executes separately from all other macros.  But what if one macro calls another macro.  For example, suppose within the definition of %macro A there is a call to %B?  If the %PUT statement appears within %B, it writes the parameters for %B but also writes out all %local macro variables (parameters included) for %A.  So if you need less of that detail to appear, a simple %PUT statement might be insufficient.  The replacement code wouldn't be very lengthy, and could involve using PROC SQL to retrieve data from dictionary.macros.  However, that also has its drawbacks.  You can't just insert PROC SQL into the middle of any program without the possibility of causing errors.  So ...

 

Try inserting the %PUT statement at the top of a macro.  See if it generates what you are looking for.  Then if you are willing and able to change all macros in the same way, we can reconsider what the best approach would be.

smantha
Lapis Lazuli | Level 10

sashelp. is a good resource for anything SAS metadata related such as names of tables in a library, columns in the respective tables, macros, parameters etc. Refer to this article

https://www.lexjansen.com/nesug/nesug06/po/po06.pdf

Besides using symbolgen

ballardw
Super User

If you don't mind running the macro to find out you can use %put _local_;

Which will only display the local parameters even if no value is assigned.

 

%macro dummy(parm1= , Parm2=, otherparm=);
%put _local_;
%mend;

%dummy()

The log will show the name of the macro (helpful those folks at SAS) and the name of the variable plus the value if any.

WARNING: This will only show the parameters defined as of the line of code the %put executes. So any macro variable created later in the macro will not be reported. Though for your case of not knowing the possible parameters having it as a first line in the macro may help.

You will still have an issue about the order of variables to use in a call though as the order the local variables are displayed in the log will be alphabetical, not declared order.

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!

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
  • 8 replies
  • 1196 views
  • 5 likes
  • 7 in conversation