Greetings!
Is there is a way to get a macro variable from a different scope. E.g.
%macro aaa;
%local a;
%let a = 123;
%bbb(a = &a)
%mend;
%macro bbb(a);
%let a = bbb;
%put {&a};
/* here I want to get the value '123' of A macro variable from AAA scope */
%mend;
%aaa;
I know the way to do so using PROC SQL DICTIONARY.MACROS table, but I need a pure SAS way.
Kind regards
For anyone interested in the answer, Scott Bass open-sourced a macro (originally Written by Tom Abernathy (via SAS-L posting), see header for more details) to utilize the above problem (implemented via SASHELP.VMACRO in a pure SAS Macro).
At any point in a program only one value a macro variable of a given name will be available.
Since you redefine the macro variable a in the macro BBB to have a value of bbb with a %let at that point the value available by referencing &a is the bbb. I am not sure why you pass a parameter a and then reassign the value as that is pretty nonsensical.
If you want the value of a from macro aaa either pass it to bbb and don't overwrite it with a let or use a different parameter name:
%macro aaa; %local a; %let a = 123; %bbb(b = &a) %mend; %macro bbb(b); %let a = bbb; %put {&b}; /* here I want to get the value '123' of A macro variable from AAA scope */ %mend; %aaa;
will do what you want.
Another option would be in the macro AAA to create a global macro variable suchas %global AAA_A;
and assign that the local a value: %let AAA_A = &a; and then in the bbb macro reference the global variable &AAA_A.
Personally I would pass the value I want as a parameter and most likely with a different name unless you actually want the bbb macro to modify the value for use inside the macro aaa.
For anyone interested in the answer, Scott Bass open-sourced a macro (originally Written by Tom Abernathy (via SAS-L posting), see header for more details) to utilize the above problem (implemented via SASHELP.VMACRO in a pure SAS Macro).
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!
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.