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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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