SAS: calling one macro from another ... Macro definition order

There are several macros in my code. Macro A is the main macro. Then macro A calls macro B, which in turn calls macro C.

In SAS, do I need to define them in reverse order? In other words, do you need to first define macro C, then macro B, then macro A last? Or does it matter, since SAS reads all the code before it actually gets into the command to run macros? In this case, can I run the command to run the macro as the first statement in my code, and then define the macros under the command?

Thanks!

+6
sas sas-macro
source share
4 answers

You must first define a macro before calling it.

Secondly, it doesn’t matter where the macro is called while you loaded it beforehand.

To tell you more about your problem: the autorun library is your friend. If the SAS administrator does not allow you to put your macros in the autocomplete library, you can add auto-caching as follows:

filename mymacros 'c:\mysas'; /*this defines the directory you have stored your macros*/ options sasautos=(sasautos mymacros) mautosource; 
+3
source share

a macro must be defined before it is called. for performance reasons, it’s best not to define a macro inside another - if you do, then it will be redefined every time you call an external macro. The following works just 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 */ 
+3
source share

You must define the macro before calling it, so the line with "% A" must follow the definition of macro A. The order of the other definitions of the macros does not matter if they are defined before they are called. Usually in my programs I set up the main macro as you describe, then the last line of the program calls this macro.

Another option to consider is to configure an autocomplete macro that contains definitions of many macros. This is best suited for multiple macros, so you do not need to redefine them in each program.

+1
source share

There are two aspects of macro in SAS: compiled macro and macro parameters:

Macrocode:

The macro code itself is very simple in that when the %macro token is encountered, the SAS system starts compiling the SAS macro and continues compiling until it enters the %mend token. The only real problems you may encounter is updating the macro code and not recompiling it before executing it - in these situations, it will still run the old version available in the macro library. If you try to compile a macro that calls another macro that has not already been defined, you will receive an error message. For these reasons, they should be programmed in the order in which they are called (as shown below:% level3 precedes% level2, which precedes% level1)

Macro variables: When defining macro variables, there are two areas: global and local. Once defined, global variables can be accessed anywhere and anytime. However, local variables exist only locally during the execution of the macro in which it was defined. By extension, if a macro where a local variable is defined calls any other macros, the local macro variable will still be available:

Working example:

In the following example, macros are defined in reverse order to prevent the SAS from returning an explicit macro definition call.

The diagram below illustrates the structure of the following macros in the following example:

 |-----------------------------| |GLOBAL | | |------------------------| | | |LEVEL1 | | | | |-------------------| | | | | |LEVEL2 | | | | | | |--------------| | | | | | | | LEVEL3 | | | | | | | |--------------| | | | | | |-------------------| | | | |------------------------| | |-----------------------------| 

Compile nested macros:

  %macro level3 ; %put **** START LEVEL3 **** ; %local G1; %let G1=Local ; %do i=1 %to 2 ; %put In the macro do loop I=&i ; %end ; %put The value of I at level3 is: &I ; %put Are we accessing global or local G1 variable here: &G1 ; %put **** END LEVEL3 ****; %mend level3 ; %macro level2 ; %put **** START LEVEL2 **** ; %*global L1 ; *<-- this would produce an error because the variable name has already been added to the local scope in %level1 ; %put Are we accessing global or local G1 variable here: &G1 ; %put Can we access local variables here: &L1 ; %level3 ; %put The value of I in level2 is: &I ; %put **** END LEVEL2 ****; %mend level2 ; 

Compile the top-level macro (which, in turn, calls the two macros above) and run it:

 %let G1=Global; %macro level1 ; %put **** START LEVEL1 **** ; %let L1=Yes; %put Are we accessing global or local G1 variable here: &G1 ; %put Can we access local variables here: &L1 ; %level2 ; %put The value of I outside of the local macro is: &I ; %put Are we accessing global or local G1 variable here: &G1 ; %put **** END LEVEL1 ****; %mend level1 ; %level1 ; 

Points to consider when viewing a magazine:

  • Beyond% level3, & I am returning a warning that the macro variable does not exist
  • Inside% level3, when & G1 is called, it returns the value stored in the local volume% level3.Once outside% level3, the value returns to the stored global value
0
source share

All Articles