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!
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?
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?
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.
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
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.