views:

1805

answers:

3

In my code I have several macros. Macro A is the main macro. Macro A then calls macro B which in turn calls macro C.

In SAS, do I have to define them in backwards order? In other words, do I have to define macro C first, then macro B, then macro A last? Or does it matter since SAS reads all the code in before it actually hits the command to run the macros? For that matter, can I issue the command to run the macro as the first statement in my code and then define the macros below the command?

Thanks!

+1  A: 

You must define a macro before it is called, so the line with "%A" would need to follow the definition of macro A. The order of the other macro definitions does not matter, as long as they are defined before they are called. Typically in my programs I set up a main macro like you describe, then the last line of the program calls this macro.

Another option to consider is to set up a macro autocall library, which contains the definitions of many macros. This works best for reusable macros, so that you do not have to redefine them in each program.

+3  A: 

First, you must define a macro before it is called.

Second, it doesn't matter where the macro is invoked as long as you have loaded it before-hand.

To elaborate on your issue: The autocall library is your friend. If you SAS administrator won't allow you to put your macros in the autocall library, you can append the autocall like so:

filename mymacros 'c:\mysas'; 
/*this defines the directory you have stored your macros*/

options sasautos=(sasautos mymacros) mautosource;
AFHood
+2  A: 

a macro has to be defined before it is called. for performance reasons, it is best not to define a macro inside another -- if you do so, then it will be re-defined every time you call the outer macro. the following works fine:

%macro a;
  %put a;
  %b
%mend a;

%macro b;
  %put b;
  %c
%mend b;

%macro c;
  %put c;
%mend c;

%*-- %a is main --*;
%a
/* on log
a
b
c
*/
Chang Chung