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
Bendy
source share