DATA Step, Macro, Functions and more

Macros Variables created in Macro Procedures

Reply
N/A
Posts: 0

Macros Variables created in Macro Procedures

In reading a previous post I understood it to say that a Macro Variable created with SYMPUT within a Macro procedure creates a Local Macro Variable. If that is the case, what happens if the Macro Variable has the same name as an already existing Global Macro.

For example: In open code I use Symput to create a Macro Variable called memname. I execute a Macro procedure in which Symput creates a Macro Variable called memname. What happens?
Super Contributor
Super Contributor
Posts: 3,174

Re: Macros Variables created in Macro Procedures

Posted in reply to deleted_user
Here is some SAS code to exercise various SAS behavior - again I refer to the SAS DOC reference, which mentions that the RUN statement location is critical to how resolution with SYMPUT in a DATA step behaves. Sorry I can't find the link again - please check the archives.

Scott Barry
SBBWorks, Inc.

data _null_;
call symput('a','I am global macro var a value');
run;
%put global macro vars - diag1: ;
%put _global_;
%macro x;
data _null_;
call symput('a','I am new macro var a value');
run;
%put local macro vars - diag2: ;
%put _local_;
%put local macro vars - diag3: ;
%put _global_;
%mend x;
%x;
%put global macro vars - diag4: ;
%put _global_;
Frequent Contributor
Posts: 81

Re: Macros Variables created in Macro Procedures

Posted in reply to deleted_user
It is possible to have both local and global macro variables with same name since the local macro variable is stored in the local symbol table and the global macro variable is stored in the global symbol table.
So, if you already have a value assigned to global macro variable and you create a local macro variable with same name then the macro variable within the macro definition resolves the value from local symbol table. And outside the macro it would resolve the value form global symbol table.

For e.g.

%let dsn=global;
%macro printdsn;
%local dsn;
%let dsn=local;
%put The value of DSN inside Printdsn is &dsn;
%mend;
%printdsn
%put The value of DSN outside Printdsn is &dsn;

-Nick
SAS Super FREQ
Posts: 8,868

Re: Macros Variables created in Macro Procedures

Posted in reply to deleted_user
Hi:
The documentation outlines the rules:
http://support.sas.com/documentation/cdl/en/mcrolref/59526/HTML/default/tw3514-symput.htm

It is always best to follow these practices when creating macro variables:

1) use %GLOBAL and %LOCAL statements to explicitly control the symbol table for macro variables.
http://support.sas.com/documentation/cdl/en/mcrolref/59526/HTML/default/a000206954.htm
http://support.sas.com/documentation/cdl/en/mcrolref/59526/HTML/default/a000206835.htm

2) use %SYMEXIST to test the existence of a macro variable
http://support.sas.com/documentation/cdl/en/mcrolref/59526/HTML/default/a002635883.htm

3) check scope with %SYMGLOBL and %SYMLOCAL
http://support.sas.com/documentation/cdl/en/mcrolref/59526/HTML/default/a002635926.htm
http://support.sas.com/documentation/cdl/en/mcrolref/59526/HTML/default/a002635915.htm

4) try to avoid having the same named macro variable exist in both global and local symbol tables -- careful programming practices (#1, #2, #3) and naming conventions will help you achieve this goal.

5) read the documentation & test your macro program thoroughly

cynthia
N/A
Posts: 0

Re: Macros Variables created in Macro Procedures

Posted in reply to deleted_user
Thanks for your responses. They answered my question perfectly.
Ask a Question
Discussion stats
  • 4 replies
  • 147 views
  • 0 likes
  • 4 in conversation