Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why doesn't %let create a local macro variable?

Tags:

sas

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

like image 699
Alex Avatar asked Dec 17 '14 18:12

Alex


People also ask

How do I create a local macro variable in SAS?

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.

Why nested macro calls are not allowed?

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.

Can you create a macro in R?

Although you cannot use macros in R, R offers other features like functions and loops that can perform the same tasks as SAS macros.

What does %let in SAS mean?

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.


1 Answers

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 
like image 91
MDe Avatar answered Nov 16 '22 02:11

MDe