DATA Step, Macro, Functions and more

Creating Global Macro Variables within a Macro?

Reply
N/A
Posts: 0

Creating Global Macro Variables within a Macro?

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_;
Super User
Posts: 5,424

Re: Creating Global Macro Variables within a Macro?

Posted in reply to deleted_user
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
Respected Advisor
Posts: 3,799

Re: Creating Global Macro Variables within a Macro?

> 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]
Super Contributor
Super Contributor
Posts: 3,174

Re: Creating Global Macro Variables within a Macro?

Posted in reply to data_null__
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.
Super Contributor
Super Contributor
Posts: 3,174

Re: Creating Global Macro Variables within a Macro?

Posted in reply to deleted_user
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
Ask a Question
Discussion stats
  • 4 replies
  • 164 views
  • 0 likes
  • 4 in conversation