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

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

1 ACCEPTED SOLUTION

Accepted Solutions
sspkmnd
Obsidian | Level 7

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).

View solution in original post

2 REPLIES 2
ballardw
Super User

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.

sspkmnd
Obsidian | Level 7

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).

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1109 views
  • 5 likes
  • 2 in conversation