BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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?
4 REPLIES 4
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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_;
NickR
Quartz | Level 8
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
Cynthia_sas
SAS Super FREQ
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
deleted_user
Not applicable
Thanks for your responses. They answered my question perfectly.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 699 views
  • 0 likes
  • 4 in conversation