Saving Macros Using the Stored Compiled Macro
Overview of the Stored Compiled Macro Facility
The stored compiled macro facility compiles and saves compiled macros in a permanent
catalog in a library that you specify. This compilation occurs only once. If the stored
compiled macro is called in the current or later sessions, the macro processor executes
the compiled code.
In SAS 9.1.3 or higher, the stored compiled macro catalog is initially opened for Read-
Only access. When a stored compiled macro is being compiled or updated, the catalog is
immediately closed and reopened for Update access. After the macro is compiled and the
catalog has been updated or changed, the catalog is again immediately closed and
reopened for Read-Only access.
Compiling and Storing a Macro Definition
To compile a macro definition in a permanent catalog, you must first create the source
for each stored compiled macro. To store the compiled macro, use the following steps:
1. Use the STORE option in the %MACRO statement. You can use the SOURCE
option to store the source code with the compiled code. In addition, you can assign a
descriptive title for the macro entry in the SAS catalog, by specifying the DES=
option. For example, the %MACRO statement in the following definition shows the
STORE, SOURCE, and DES= options:
%macro myfiles / store source
filename file1 'external-file-1';
filename file2 'external-file-2';
Save your macro source code. You cannot re-create the source statements from
a compiled macro. Therefore, you must save the original macro source
statements if you want to change the macro. For all stored compiled macros, you
should document your macro source code well. You can save the source code
with the compiled code using the SOURCE option in the %MACRO statement or
you can save the source in a separate file. If you save the source in a separate file,
it is recommended that you save the source code in the same catalog as the
compiled macro. In this example, save it to the following library:
Note: To retrieve the source of a compiled stored macro, see “%COPY Statement”
on page 300.
2. Set the MSTORED system option to enable the stored compiled macro facility. For
more information, see “MSTORED System Option” on page 369.
Saving Macros Using the Stored Compiled Macro Facility 117
3. Assign the SASMSTORE option to specify the SAS library that contains or will
contain the catalog of stored compiled SAS macros. For example, to store or call
compiled macros in a SAS catalog named MyLib.SASMacr, submit these statements:
libname mylib 'SAS-library';
options mstored sasmstore=mylib;
For more information, see “SASMSTORE= System Option” on page 373.
4. Submit the source for each macro that you want to compile and permanently store.
You cannot move a stored compiled macro to another operating system or to a different
release of SAS. However, you can move the macro source code to another operating
system or to a different SAS release where you can then compile and store it. For more
information, see your host companion.
Storing Autocall Macros Supplied by SAS
If you use the macros in the autocall library supplied by SAS, you can save macro
compile time by compiling and storing those macros in addition to ones that you create
yourself. Many of the macros related to Base SAS software that are in the autocall
library supplied by SAS can be compiled and stored in a SAS catalog named SASMacr.
This action can be done by using the autocall macro COMPSTOR that is supplied by
SAS. For more information, see “%COMPSTOR Autocall Macro” on page 179.
Calling a Stored Compiled Macro
Once you have set the required system options, calling a stored compiled macro is just
like calling session compiled macros. However, it is important that you understand how
the macro processor locates a macro. When you call a macro, the macro processor
searches for the macro name using this sequence:
1. the macros compiled during the current session
2. the stored compiled macros in the SASMacr catalog in the specified library (if
options MSTORED and SASMSTORE= are in effect)
3. each autocall library specified in the SASAUTOS option (if options SASAUTOS=
and MAUTOSOURCE are in effect)
4. the stored compiled macros in the SASMacr catalog in the SASHelp library
You can display the entries in a catalog containing compiled macros. For more
information, see Chapter 10, “Macro Facility Error Messages and Debugging,” on page
118 Chapter 9 • Storing and Reusing Macros