BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Syntas_error
Quartz | Level 8

I'm learning about macros for the SAS advanced programmer certification. I'm reading about the %GLOBAL and %LOCAL statements and I'm trying to decide whether this section is actually worth my time.

 

Global macro varibables ("with" an actual value) will be created anyways through the %LET-statement, so why on earth would I ever be using the %GLOBAL statement?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

If you create a macro variable inside a macro, it is not global and so it only has value inside the macro. You can't use that macro variable outside the macro. However, if you use the %global command, now the value of the macro variable can be used outside of the macro in which it was created. This might be useful if you want to pass a value from one macro to another, or pass a macro variable value to be used in open code.

--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

If you create a macro variable inside a macro, it is not global and so it only has value inside the macro. You can't use that macro variable outside the macro. However, if you use the %global command, now the value of the macro variable can be used outside of the macro in which it was created. This might be useful if you want to pass a value from one macro to another, or pass a macro variable value to be used in open code.

--
Paige Miller
ballardw
Super User

Macro variables have what is referred to as scope which relates to where the macro variable exists and where it receives values. This is critical to understanding what happens with macro coding especially when you have macros that call other macros. A missing %local may result in some dreadful errors, either of run time logic results or actual code failures that can be hard to trace. So you do want to know what happens with and without %local.

Examine the code below and determine what the output that appears in the log should be.

Then run code. Does it match your expectations.

%macro dummy(parm);
%put Parm first used in dummy is: &parm.;
   %dummy2(abc);
%put Parm after calling dummy2 is &parm.;
%mend;
%macro dummy2(p);
   %let parm= &p &p;
   %put p in dummy2 is: &p.;
   %put parm in dummy2 is:&parm;
%mend;

%macro dummy3(parm);
%put Parm first used in dummy3 is: &parm.;
   %dummy4(abc);
%put Parm after calling dummy4 is &parm.;
%mend;
%macro dummy4(p);
   %local parm;
   %let parm= &p &p;
   %put p in dummy4 is: &p.;
   %put parm in dummy4 is:&parm;
%mend;

%dummy(pdq)
%dummy3(pdq)