program:
%let a=20;
%macro check;
%let a=50;
%put inside &a.;
%mend check;
%check;
%put outside &a.;
output :
Q: first i have defined variable a with %let , SAS will consider as global variable,
then same variable inside a macro ,SAS will consider as local variable.
But why it is printing both as 50, it should print inside 50 and outside 20 , right ?
can someone explain , why local variable is over writing global macro variable ?
You have not defined any local macro variable. So only the global macro variable exists.
Click on Spoiler below to see why.
@narravulap wrote:
program:
%let a=20;
%macro check;
%let a=50;
%put inside &a.;
%mend check;
%check;
%put outside &a.;
output :
inside 50outside 50
Q: first i have defined variable a with %let , SAS will consider as global variable,
then same variable inside a macro ,SAS will consider as local variable.
But why it is printing both as 50, it should print inside 50 and outside 20 , right ?
can someone explain , why local variable is over writing global macro variable ?
@ArtC @LinusH @richard_hu2003 @shidehrafigh @SPR
@narravulap wrote:
creating macro variable inside a macro , automatically consider as local right, ?
when i'm calling outside of a macro , why it is printing local macro variable?
%let statement inside a macro, creates a local macro variable
The first statement is true. (Unless you use CALL SYMPUTX() with the third argument set to 'G'.)
The last statement is false.
Having a %LET statement inside of macro definition just means it does not execute until you call the macro. The same as having a PROC PRINT statement inside of the macro definition.
When it does execute it only makes a NEW macro variable if the macro variable does not already exist.
Hi,
1) First, I highly recommend Maxim 1
The following link provides documentation page with precise diagrams explaining how macro variables are created, populated, and resolved:
2) yes, macro parameters are local by default, run the following code and see the log:
%let mv = I an global!;
%macro test(mv=am I local?);
%put _local_;
%put ##################;
%put _user_;
%mend;
%test()
Log shows:
1 %let mv = I an global!;
2
3 %macro test(mv=am I local?);
4 %put _local_;
5 %put ##################;
6 %put _user_;
7 %mend;
8
9 %test()
TEST MV am I local?
##################
TEST MV am I local?
GLOBAL MV I an global!
You can see there local table (with TEST prefix for scope) has one variable and global table has one too (with GLOBAL prefix for scope)
3) and 4) If you run the following code:
%let mv = I an global!;
%macro testInside(mv=I am local even more!!);
proc print data=sashelp.vmacro;
run;
%mend;
%macro test(mv=am I local?);
%put _local_;
%put ##################;
%put _user_;
%testInside()
%mend;
%test()
you will see that all "available" scopes (in this case two "local" [one for test, second for testInside] and the global one) can be retrieve from one SAS view in sashelp library.
Order of observations goes from the most inner one to most global one. So even if we conceptually think about 3 macro variables tables, we can get all information from one place.
Bart
1. so, if there is a macro variable already defined as global, it will be global until we mentioned as local ?
No. If you define a global macro variable it lives forever. Or at least it used to before SAS added the %SYMDEL macro statement.
But once you create a LOCAL macro variable with the same name you cannot see it. Only the "inner most" or "closest" version of the macro variable can be used.
The exceptions are that you can use CALL SYMPUTX() to modify global macro variables that are hidden (blocked) by a local macro variable. And if you want to retrieve the value of a global (or other local scope) macro variable that is hidden you can use this macro: https://github.com/sasutils/macros/blob/master/symget.sas
2. macro parameters are by default, local variables ?
Yes. The parameters of a macro are local macro variables.
3.Macros will be stored in different two tables right ?, then it can store separately, a=20 as global because i have derived it with open code let statement and can store a=50 in local table can be referred only during that particular macro. ?
Only while the macro is executing. Once the macro finishes its local macro variables are gone. Note that if your macro calls another macro that second macro can see and modify the first's local macro variables. Just like you can see and modify global macro variables. As long are they are not hidden.
4. how this storing process works in SAS for macro variables ?
No idea and it probably does not matter.
If you print the SASHELP.VMACRO view (or query the DICTIONARY.MACROS table it references) you will see that each macro instance has a SCOPE. The SCOPE is either GLOBAL (or AUTOMATIC which is just another name for GLOBAL macro variables the SAS created for you) or the name of the macro where they live.
3) Macros are stored globally. Only macro variables have scope.
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.
Ready to level-up your skills? Choose your own adventure.