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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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
  • 1573 views
  • 5 likes
  • 2 in conversation