Why does% not allow creating a local macro variable?

I always thought that %let creates a local variable if used inside %macro . . . %mend %macro . . . %mend

But when I run this code, GLOBAL TESTVAR value1 displayed in the SAS GLOBAL TESTVAR value1

 %let testVar = value2; %macro test; %let testVar = value1; %mend; %test %put _all_; 

So, I can’t understand why the value of the global variable testVar has changed to value1 . I expected it to remain unchanged value2 . The %let statement inside %macro should ONLY reflect the local character table.

SAS Documentation :

When a macro processor executes a macro program instruction that can create a macro variable, the macro processor creates a variable in the local symbol table if a macro variable with the same name is not available for it

+6
source share
1 answer

The key is "if a macro with the same name is not available" - in this case, a macro with the same name is available, since you have already defined testVar as global.

You can either give it a name that is not shared with the global one, or explicitly declare it local:

 %let testVar = value2; %macro test; %local testVar; %let testVar = value1; %mend; %test 
+7
source

Source: https://habr.com/ru/post/979804/


All Articles