Previous Page | Next Page

Storing and Reusing Macros

Saving Macros Using the Stored Compiled Macro Facility


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
         des='Define filenames';
      filename file1 'external-file-1';
      filename file2 'external-file-2';
    %mend;

    CAUTION:
    Save your macro source code.

    You cannot recreate 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:

    mylib.sasmacro.myfiles.source

      [cautionend]

    Note:   To retrieve the source of a compiled stored macro, see %COPY Statement.  [cautionend]

  2. Set the MSTORED system option to enable the stored compiled macro facility. For more information, see MSTORED System Option.

  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-data-library';
    options mstored sasmstore=mylib;

    For more information, see SASMSTORE= System Option.

  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. You can, however, move the macro source code to another operating system where you can then compile and store it. You might need to recompile these macros if you use them in a different release of SAS. 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 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 by using the autocall macro COMPSTOR that is supplied by SAS. For more information, see %COMPSTOR Autocall Macro.


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 Macro Facility Error Messages and Debugging.

Previous Page | Next Page | Top of Page