DATA Step, Macro, Functions and more

Retrieve a macro varible from a different scope

Reply
Occasional Contributor
Posts: 13

Retrieve a macro varible from a different scope

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

Super User
Posts: 13,338

Re: Retrieve a macro varible from a different scope

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.

Ask a Question
Discussion stats
  • 1 reply
  • 114 views
  • 5 likes
  • 2 in conversation