Contributor
Posts: 27

# what is the logic of this macro?

%let x=15;

%macro a;

%let x=10;

%put x inside macro a= &x;

%mend a;

%a;

%put x outside the macro a = &x;

i was expecting the value of a outside is 15, however it is showing the value of a = 10. kindly explain?

Super Contributor
Posts: 3,176

## Re: what is the logic of this macro?

The code-piece as illustrated demonstrates the SAS topic "scope of macro variable quoting" (found in various SAS.COM support / documentation resources) where there are GLOBAL and LOCAL macro variables to consider.  The SAS OPTIONS MACROGEN SYMBOLGET MLOGIC MPRINT;  can help reveal more diagnostic output evidence to your SAS log.

Scott Barry

SBBWorks, Inc.

Super User
Posts: 10,787

## Re: what is the logic of this macro?

It has already been  updated .

You want see 15 make it LOCAL.

%macro a;

%local x ;

%let x=10;

%put x inside macro a= &x;

%mend a;

Occasional Contributor
Posts: 10

## Re: what is the logic of this macro?

first you are creating variable x in GST(global symbol Table) and assigning its value as 15 but again with same variable name

you are giving different value it is replacing with the latest value of x.

and finally you desire output is 10 not 15.

If you want your output as 15 then store second x value in LST(Local symbol table),that's it.

Contributor
Posts: 21

## Re: what is the logic of this macro?

Hi Ved,

A local symbol table is not created until a request is made to create a local variable. Macros that do not create local variables do not have a local table.

In you case no local variable is created hence the value of x is updated from 15 to 10 in Global Symbol Table. However, if you add %local x; in your macro then the program will create two symbol table one : Global Symbol Table having x = 15 and Local Symbol Table having x = 10. And outside the macro the value x will resolve to 15 because after the completion of execution of Macro local symbol table will be deleted.

Below table will give you better picture :

 Does `macvar` exist in the local symbol table? Yes Retrieve the value of `macvar` from the local symbol table. No Does `macvar` exist in the global symbol table? Yes Retrieve the value of `macvar` from the global symbol table. No Return the tokens to the word scanner. Issue a warning message to the SAS log to indicate that the reference was not resolved.
Contributor
Posts: 27

## Re: what is the logic of this macro?

Hello Amit,

Thanks for the explaination , however i would like to ask you one more thing, like in case i omit first %let statement where value of x is 15. now inside the macro defination, macro variable X created by %let statement will be saved into local or global table ? . as %let is global statement , but what i  understand, inside macro defination all macros created have local nature. kindly correct me in case i m wrong

Contributor
Posts: 21

## Re: what is the logic of this macro?

Hi Ved,

As I explained in my previous post, local macro variable is not created until a request is made. If any local symbol table is already present then all the macros created within the macro call are local. There could be two methods to trigger the Local symbol table

1. Using the keyword and/or positional parameters within the macro call

2. Using %LOCAL statement in the macro.

If I re-create your program like :

%let x=15;

%macro a(x = 11);

%let x=10;

%put x inside macro a= &x;

%mend a;

%a;

%put x outside the macro a = &x;

Now the value of 'x' will be 15. Because defining the Keyword parameter(x = 11) triggers the local symbol table and now all the macro created within the macro 'a' will be local in scope. This will not effect the value of 'x' outside the macro 'a'.

Br,Amit

Contributor
Posts: 27

## Re: what is the logic of this macro?

Hello Amit,

Thanks alot for your detailed explanation. you have cleared my doubt about the concept of local and global symbol table.

regards,

ved

Discussion stats
• 7 replies
• 469 views
• 9 likes
• 5 in conversation