DATA Step, Macro, Functions and more

When exactly does a Local Symbol Table in SAS get created?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 89
Accepted Solution

When exactly does a Local Symbol Table in SAS get created?

I find it very Frustrating that sometimes the SAS Advanced Preparation Guide is misleading and for beginners this is indeed a real challenge.

For eg the below statement according to me is WRONG

"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 "

This under the Topic of "Understanding Local Symbol Tables" subsection "%LOCAL Statement " in Chapter 11- Creating and Using Macro Programs of SAS Certification Prep Guide: Advanced Programming for SAS 9, Fourth Edition  . All 4 editions of this Book have the same Statement.

The above should actually be

"A local Symbol Table is created whenever a Macro is called ie the compiled macro definition starts executing.If no Request is made to create a Local Macro variable then the Local Symbol Table is Empty".


Even the SAS Documentation on this topic is not very Clear. Not sure why there is such an ambiguity over this.


When the following code is Run , a local symbol tables is created is the moment the macro TEST  starts executing. But since we are not defining any parameters or local macro variables , the local symbol table is EMPTY.  This is my Understanding.

Does anyone in this Forum object to what I am saying here?

%macro Test;

%Put   THIS IS A TEST;

%mend Test;

%Test


Accepted Solutions
Solution
‎07-29-2015 08:03 PM
Super User
Posts: 5,516

Re: When exactly does a Local Symbol Table in SAS get created?

You are correct that some of the documentation is wrong.  There is no such thing as an empty symbol table.  Invoking a macro does not create a symbol table until it is actually required.  See:

http://blogs.sas.com/content/publishing/2015/05/20/macro-myth-the-closest-non-empty-symbol-table/

This means that "3" is the correct answer.  It also means that the very first statement you questioned is actually correct.

View solution in original post


All Replies
Respected Advisor
Posts: 4,173

Re: When exactly does a Local Symbol Table in SAS get created?

Hi,

Not sure if it's of any practical relevance when exactly the local symbol table gets created - but it's of course always a good thing to fully understand how things work as this can make the difference in "getting it right".

So yes, the way I'm reading the documentation the local symbol table gets always created SAS(R) 9.4 Macro Language: Reference, Fourth Edition

The macro processor recognizes a macro call and begins to execute macro APP, as follows:

  1. The macro processor creates a local symbol table for the macro. The macro processor examines the previously compiled definition of the macro. If there are any parameters, variable declarations, or computed GOTO statements in the macro definition, the macro processor adds entries for the parameters and variables to the newly created local symbol table.

How did you get to your conclusion that what's in the Prep Guide is wrong? If you're really convinced it's wrong then please open as SAS Tech Support track so that someone at SAS can look into this and fix it in the Prep Guide.

Thanks,

Patrick

Frequent Contributor
Posts: 89

Re: When exactly does a Local Symbol Table in SAS get created?

Yes  Patrick i too saw what you saw in the SAS Documentation Link you provided above.

But then i again saw this at the bottom of this Link  : SAS(R) 9.4 Macro Language: Reference, Fourth Edition

A macro's local symbol table is empty until the macro creates at least one macro variable. A local symbol table can be created by any of the following:

  • the presence of one or more macro parameters
  • a %LOCAL statement

macro statements that define macro variables, such as %LET and the iterative %DO statement (if the variable does not already exist globally or a %GLOBAL statement is not used)




Not Sure why   it says a Local Symbol  Table  can be created by any of three options options when infact it should say the   Local Symbol table can be populated by any of the following. It also contradicts "A macro's local symbol table is empty until the macro creates at least one macro variable."

My aim is to understand Macro Processing . Probably If i were to be asked a Question as shown below   I would  choose 4)  below .But then i may be Incorrect because of the ambiguity above.

Which of the following creates a Local Symbol Symbol Table?

  1. the presence of one or more macro parameters
  2. a %LOCAL statement or macro statements that define macro variables, such as %LET and the iterative %DO statement (if the variable does not already exist globally or a %GLOBAL statement is not used)
  3. Both  1 and 2
  4. Macro Invocation.


Solution
‎07-29-2015 08:03 PM
Super User
Posts: 5,516

Re: When exactly does a Local Symbol Table in SAS get created?

You are correct that some of the documentation is wrong.  There is no such thing as an empty symbol table.  Invoking a macro does not create a symbol table until it is actually required.  See:

http://blogs.sas.com/content/publishing/2015/05/20/macro-myth-the-closest-non-empty-symbol-table/

This means that "3" is the correct answer.  It also means that the very first statement you questioned is actually correct.

Frequent Contributor
Posts: 89

Re: When exactly does a Local Symbol Table in SAS get created?

Posted in reply to Astounding

Wow .I am very much surprised why SAS documentation has been wrong in this respect consistently since the time I started working with SAS  starting with Version 8.2.

Looks like this is not a very well understood topic for sure by many.

Super User
Super User
Posts: 7,076

Re: When exactly does a Local Symbol Table in SAS get created?

The description seems right to me.  Not sure if there is a difference between an EMPTY local symbol table and a local symbol table that has not been created.  The only function that I know that cares about that is the CALL SYMPUT() function.  If you use that function to set a value for a macro variable that DOES NOT ALREADY EXIST and the local symbol table is empty (or doesn't "exist") then CALL SYMPUT will make the variable in symbol table next higher table in the nested levels of symbol tables.

Here is a little test.

%macro xx ;

data _null_;

  call symput('X','X1');

run;

%put &=X;

%put _local_;

data _null_;

  call symput('Y','Y1');

run;

%put &=X &=Y;

%put _local_;

data _null_;

  call symputx('X','X2','L');

run;

%put &=X &=Y;

%put _local_;

data _null_;

  call symput('Y','Y2');

  call symput('Z','Z1');

run;

%put &=X &=Y &=Z;

%put _local_;

%mend ;

%symdel x y z/nowarn ;

%xx;

%put &=X &=Y &=Z;

Frequent Contributor
Posts: 89

Re: When exactly does a Local Symbol Table in SAS get created?

Tom, I am not entirely convinced that the SAS Documentation does a good Job of explaining that there is no  Difference between a Empty Local Symbol Table and a Local Symbol Table that does not Exist  and looking at Bob's Blog (provided by  user : Astounding above)  : http://blogs.sas.com/content/publishing/2015/05/20/macro-myth-the-closest-non-empty-symbol-table/    the very concept of Empty Local Symbol Table is Wrong .

SAS Documentation ought to Make this very clear ,not sure why there is no attempt to do this.

I always thought that the Local Symbol Table gets created as soon as a Macro is Invoked ( provided it is successfully compiled prior to this) and then it gets populated with macro variables as and  when requested by %LET,%LOCAL  etc .Ofcourse the  macro variable is assumed to be not already present in the next higher symbol table relative to this Macro. Even Patrick above was under the same Assumption i suppose.

But after reading Bob's blog above i have come to conclude that Local Symbol Table is never empty but gets created under any of the below situations

  • the presence of one or more macro parameters
  • a %LOCAL statement
  • macro statements that define macro variables, such as %LET and the iterative %DO statement (if the variable does not already exist globally or a %GLOBAL statement is not used).

In other words if there is a Local Symbol Table associated with a Macro then it should have atleast one macro variable in it otherwise it does not exist conceptually.

Super User
Super User
Posts: 7,076

Re: When exactly does a Local Symbol Table in SAS get created?

Still sounds like word games to me.  If a tree falls in a forest .....

I don't know how SAS actually builds the symbol "tables" and I don't really care.

As I said before as far as I know the only statement that cares about the concept of an "EMPTY" or "NOT YET CREATED" symbol table is CALL SYMPUT.

So if for some strange reason you have a macro

  • with no parameters (which by definition will be local)
  • you do not define your local macro variables using %LOCAL at the top of the macro as a well formatted macro should
  • you have NOT created any macro variables by
    • using %LET to define a macro variable that does not already exist
    • using PROC SQL to create automatic variables like SQLOBS that do not already exist
    • using PROC SQL INTO clause to create a macro variable that does not already exist
    • using CALL SYMPUTX with third argument set the  'L')
  • AND
    • you use CALL SYMPUT (or CALL SYMPUTX without a value for the third parameter) to set a value to a macro variable that does not already exist
  • THEN
    • The macro variable will be created in the next higher (non-"empty") parent scope instead of in the scope of the macro that is actually running the CALL SYMPUT statement.

And since SAS likes to maintain compatibility with old programs (and I am sure there are some user programs that take advantage of that feature of CALL SYMPUT()) it will be that way until SAS creates a whole new macro language.

Contributor
Posts: 22

Re: When exactly does a Local Symbol Table in SAS get created?

Thanks, all, for bringing this up and looking into it. It's a distinction that is sometimes important, as Tom has outlined, and one that I had never thought to consider. It is something I will have to clarify in my own writings on SYMPUTX and SYMPUT. I have sometimes recommended the use of a dummy parameter in the definition of a macro that does not use a parameter, and this might be an additional reason to use a dummy parameter, or a reason not to use one (though I will have to think carefully about this).

PROC Star
Posts: 1,324

Re: When exactly does a Local Symbol Table in SAS get created?

Posted in reply to RickAster

I suspect this complexity of CALL SYMPUT (it is unable to create a local symbol table) is an accident of time.  I can't think of a good reason for this 'feature.'  Would be great to hear from anyone who was there at the beginning, if this was originally a design feature or perhaps bug.

That said, agree with Tom that it is not likely to be changed, as it would break too much legacy code.

Usually when I'm writing macros, if I find that I'm writing a macro that does not have a parameter, I question why that is.  I subscribe to a working definition of a macro I learned from Ian Whitlock, 'A macro is a parameterized unit of code.'  The lack of a parameter becomes a warning sign of poor design.  And if I decide to ignore that warning, I'll often add at least a DEBUG= parameter, even if it's just a dummy parameter as Rick suggests.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 807 views
  • 3 likes
  • 6 in conversation