BookmarkSubscribeRSS Feed
BrahmanandaRao
Lapis Lazuli | Level 10

can any one details examples about local and global macros variables

 

 

3 REPLIES 3
Kurt_Bremser
Super User

Maxim 1: Read the Documentation

(documentation.sas.com, select Macro Language Reference, Understanding and Using the Macro Facility, Scopes of Macro Variables)

Everything you need to know about macro variable scope, including examples.

s_lassen
Meteorite | Level 14

Interestingly, the two links you got, from @andreas_lds and @Kurt_Bremser do not really answer your questions. The first reference says very little about the scope of macro variables, the second actually contains a factual error:

A local symbol table can be created by any of the following:
  • the presence of one or more macro parameters
  • a %LOCAL statement
  • macro statements that define macro variables, such as %LET and the iterative %DO statement (if the variable does not already exist globally or a %GLOBAL statement is not used)

The statement marked in red is not correct. A local symbol table is created if a macro variable is defined that does not exist in the calling environment. But that does not necessarily mean a %GLOBAL variable, the variable could also be %LOCAL to a calling macro, e.g.:

%macro a;
  %local i;
  %do i=1 %to 5;
     %b;
     %end;
%mend;

%macro b;
  %do i=1 %to 3;
    %put Hello World;
    %end;
%mend;

%a;

This very silly example shows the danger of forgetting to declare your macro variables %LOCAL. If you submit this, it will keep writing "Hello World" in the log, until the log runs full or you press <ctrl-break>. Why? Because the I variable in %B is the same as the one in %A. So, there was no %GLOBAL I variable, yet no local symbol table in %B either.

 

Forgetting to declare macro variables %LOCAL is a very common mistake. Many years ago we even had to write SAS Institute and ask them to declare the variable I %LOCAL in one of their utility macros (I think it was %VERIFY).

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1465 views
  • 0 likes
  • 4 in conversation