- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
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