BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I'm having problems again.

I have a piece of code that creates some numbers that I then assign to global macro variables to use later on. I want to run the piece of code three times, in order to create three versions of the numbers so that I can then use an average of the three numbers rather than the one off.

So I turned the code into a macro and it doesn't work. I tried some much simplified version of the code and that still doesn't work...

Here is the simple version:

data temp;
input xval $ yval;
cards;
A 1
B 2
C 3
;
run;


%macro mymacro (loop);

data _null_;
set temp;
macname=compress(xval||&loop);
call symput (macname, yval*&loop);
run;

%mend;

%mymacro (1);
%mymacro (2);
%mymacro (3);

%put _global_;
4 REPLIES 4
LinusH
Tourmaline | Level 20
Since your CALL SYMPUT is executed within a macro, the macro variables created will be local. Try to insert a CALL EXECUTE after you have assigned your macro variable a name:

%macro mymacro (loop);
data _null_;
set temp;
macname=compress(xval||&loop);
call execute('%global '||macname||';');
call symput (macname, yval*&loop);
run;
%mend;


/Linus
Data never sleeps
data_null__
Jade | Level 19
> Since your CALL SYMPUT is executed within a macro,
> the macro variables created will be local.

That is not exactly right. There are a few "depends on" that affect the action. Consider the following example. You can get the exact action you desire with SYMPUTX when you specify the symbol table option.

CALL SYMPUTX(macro-variable, value <,symbol-table>);

Having said that I don't think GLOBAL macro variables are the best choice here. I would think about keeping the data in a SAS data set would be better. I would need to see how these variables are being used to make a recomendation.

[pre]
161 options nostimer;
162 %symdel a;
163
164 %macro test(arg);
165 data _null_;
166 call symput('A','What is my scope');
167 run;
168 %put _global_;
169 %put _local_;
170 %mend;
171
172 %test
173
174 %symdel a;


TEST ARG
TEST A What is my scope
WARNING: Attempt to delete macro variable A failed. Variable not found.
175 %macro test;
176 data _null_;
177 call symput('A','What is my scope');
178 run;
179 %put _global_;
180 %put _local_;
181 %mend;
182
183 %test


GLOBAL A What is my scope
184
185
186 %symdel a;
187 %global a;
188 %macro test(arg);
189 data _null_;
190 call symput('A','What is my scope');
191 run;
192 %put _global_;
193 %put _local_;
194 %mend;
195
196 %test();


GLOBAL A What is my scope
TEST ARG
[/pre]
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
If you need a SAS global macro variable, simply move the RUN; statement to be outside the SAS macro definition. The DOC I referenced shows this condition in action. I tested your code and that works great. Amazing what the DOC can do for you, when referenced. And I found it by doing a Google advanced search below:

global macro variable data step site:sas.com

Scott Barry
SBBWorks, Inc.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Refer to this SAS support website http://support.sas.com/ DOC reference (9.2 but it applies to 9.1) on SYMPUT resolution, local and global.

Scott Barry
SBBWorks, Inc.


Scopes of Macro Variables
Special Cases of Scope with the CALL SYMPUT Routine
http://support.sas.com/documentation/cdl/en/mcrolref/59526/HTML/default/tw3514-symput.htm

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