BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SASFREAK
Obsidian | Level 7

%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?

1 ACCEPTED SOLUTION

Accepted Solutions
AmitRathore
Obsidian | Level 7

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'.

Hope I answered your query.

Br,Amit

View solution in original post

7 REPLIES 7
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10

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.

Ksharp
Super User

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;

Dalveer
Calcite | Level 5

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.

AmitRathore
Obsidian | Level 7

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
then
Does macvar exist in the global symbol table?Yes
>
Retrieve the value of macvar from the global symbol table.
No
then
Return the tokens to the word scanner. Issue a warning message to the SAS log to indicate that the reference was not resolved.
SASFREAK
Obsidian | Level 7

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

thanks in advance.

AmitRathore
Obsidian | Level 7

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'.

Hope I answered your query.

Br,Amit

SASFREAK
Obsidian | Level 7

Hello Amit,

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

regards,

ved

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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