BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
cosmid
Lapis Lazuli | Level 10

Hi,

 

The following example is from the SAS Help Center:

 

%let x=1;
%let items=2;
%macro test(val);

data _null_;
 call symputx('items', ' leading and trailing blanks removed ', 'L');
 call symputx('  x   ', 123.456);
run;

%put local items &items;
%mend test;
  
%test(100)
      
%put items=!&items!;
%put x=!&x!;

Question: Why is there a parameter val used for the macro test? What does that 100 do when used for val? I couldn't tell from the log. 

 

Thanks!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Good question!

 

One of the strange things about both CALL SYMPUTX() and CALL SYMPUT() is which symbol table it will use to create a new symbol (what most people normally call macro variables).

 

The strange thing is that SAS does not always create a symbol table for every running macro.  Macros that do not have any local macro variables do not get a symbol table defined.  In which case the new macro variable will be created in the next higher symbol table.  Which could be the local symbol table of the calling macro.

 

By adding a parameter to the macro definition they insured that the macro will have at least one local symbol so that there will be a local symbol table created when it runs.

 

Try this example.

%macro nosymtable;
* This macro will have no symbol table; 
data _null_;
  call symputx('nosymboltable','xxx');
run;
%mend nosymtable;

%macro makemvar(mvar,value);
data _null_;
  call symputx("&mvar","&value");
run;
%nosymtable;
%put _user_;
%mend makemvar;

%makemvar(test2,yyy);

What symbol table has the value xxx in the macro variable nosymboltable?

Spoiler
MAKEMVAR MVAR test2
MAKEMVAR NOSYMBOLTABLE xxx
MAKEMVAR TEST2 yyy
MAKEMVAR VALUE yyy
GLOBAL SQLEXITCODE 0
GLOBAL SQLOBS 1
GLOBAL SQLOOPS 136
GLOBAL SQLRC 0
GLOBAL SQLXOBS 0
GLOBAL SQLXOPENERRS 0
GLOBAL SYS_SQL_IP_ALL -1
GLOBAL SYS_SQL_IP_STMT

View solution in original post

1 REPLY 1
Tom
Super User Tom
Super User

Good question!

 

One of the strange things about both CALL SYMPUTX() and CALL SYMPUT() is which symbol table it will use to create a new symbol (what most people normally call macro variables).

 

The strange thing is that SAS does not always create a symbol table for every running macro.  Macros that do not have any local macro variables do not get a symbol table defined.  In which case the new macro variable will be created in the next higher symbol table.  Which could be the local symbol table of the calling macro.

 

By adding a parameter to the macro definition they insured that the macro will have at least one local symbol so that there will be a local symbol table created when it runs.

 

Try this example.

%macro nosymtable;
* This macro will have no symbol table; 
data _null_;
  call symputx('nosymboltable','xxx');
run;
%mend nosymtable;

%macro makemvar(mvar,value);
data _null_;
  call symputx("&mvar","&value");
run;
%nosymtable;
%put _user_;
%mend makemvar;

%makemvar(test2,yyy);

What symbol table has the value xxx in the macro variable nosymboltable?

Spoiler
MAKEMVAR MVAR test2
MAKEMVAR NOSYMBOLTABLE xxx
MAKEMVAR TEST2 yyy
MAKEMVAR VALUE yyy
GLOBAL SQLEXITCODE 0
GLOBAL SQLOBS 1
GLOBAL SQLOOPS 136
GLOBAL SQLRC 0
GLOBAL SQLXOBS 0
GLOBAL SQLXOPENERRS 0
GLOBAL SYS_SQL_IP_ALL -1
GLOBAL SYS_SQL_IP_STMT

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 379 views
  • 2 likes
  • 2 in conversation