I always thought that %let
creates a local
variable if used inside of %macro . . . %mend
But when I run this code , the SAS log shows 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
changed to value1
. I was expecting it to be unchanged value2
. The %let
statement inside the %macro
should have impacted ONLY the local symbol table.
SAS documentation says:
When the macro processor executes a macro program statement that can create a macro variable, the macro processor creates the variable in the local symbol table if no macro variable with the same name is available to it
Knowing how to create and use macro variables is the first step in learning the SAS Macro Language. One of the primary techniques is through the %LET statement. Other ways of creating macro variables includes the use of the iterative %DO loop, macro parameters, the SQL INTO: clause, and the DATA step SYMPUT routine.
Avoid Nested Macro Definitions Nesting macro definitions inside other macros is usually unnecessary and inefficient. When you call a macro that contains a nested macro definition, the macro processor generates the nested macro definition as text and places it on the input stack.
Although you cannot use macros in R, R offers other features like functions and loops that can perform the same tasks as SAS macros.
Details. If the macro variable that is named in the %LET statement already exists in any enclosing scope, the %LET statement updates the value. If the macro variable that is named in the %LET statement does not exist, it is created in the closest enclosing scope and it is assigned the specified value.
The key is 'if no macro variable with the same name is available to it' - in this case, a macro variable with the same name is available, because you've already defined testVar as a global.
You can either give it a name that isn't shared with a global, or explicitly declare it as local:
%let testVar = value2;
%macro test;
%local testVar;
%let testVar = value1;
%mend;
%test
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With