Always save your code when creating a stored compiled macro

6

The stored compiled macro facility enables you to compile and save your macro definition in a permanent catalog in a library that you specify. The macro is compiled only once. When you call the macro in the current and subsequent SAS® sessions, SAS executes the compiled code from the macro catalog that you created when you compiled the macro.

The stored compiled facility has two main purposes. The first is that it enables your code to run faster because the macro code does not need to be compiled each time it is executed. The second purpose is to help you protect your code. Sometimes you need to share code that you’ve written with other users, but you do not want them to be able to see the code that is being executed. The stored compiled macro facility enables you to share the program without revealing the code. Compiling the macro with the SECURE option prevents the output of the SYMBOLGEN, MPRINT, and MLOGIC macro debugging options from being written to the log when the macro executes. This means that no code is written to the log when the code executes. After the macro has been compiled, there is no way to decompile it to retrieve the source code that created the catalog entry. This behavior prevents the user from being able to retrieve the code. However, it also prevents you from being able to recover the code.

It is very important to remember that there is no way to get back the code from a stored compiled macro. Because of this behavior, you should ALWAYS save your code when creating a stored compiled macro catalog. In order to update a stored compiled macro, you must recompile the macro. The only way to do this is to submit the macro definition again. Another important fact is that a stored compiled macro catalog can be used only on the same operating system and release of SAS that it was created on. So, in order to use a stored compiled macro on another operating system or release of SAS, that macro must be compiled in the new environment. Again, the only way to compile the macro is to resubmit the macro definition.

Save the Macro Source Code

To make it easier for you to save your code, the %MACRO statement contains the SOURCE option. When you create a stored compiled macro, the SOURCE option stores the macro definition as part of a catalog entry in the SASMACR catalog in the permanent SAS library listed on the SASMSTORE= system option.

Here is the syntax needed to create a stored compiled macro with the SOURCE option set:

libname mymacs 'c:\my macro library';   ❶                                                                                                
options mstored sasmstore=mymacs;       ❷                                                                                              
 
%macro test / store source;             ❸                                                                                                          
 
  libname mylib1 'path-to-my-first-library';                                                                                            
  libname mylib2 'path-to-my-second-library';                                                                                           
 
%mend;

 

❶ The LIBNAME statement points to the SAS library that will contain my stored compiled macro catalog.

❷ The MSTORED system option enables the stored compiled facility. The SASMSTORE= option points to the libref that points to the macro library.

❸ The STORE option instructs the macro processor to store the compiled version of TEST in the SASMACR catalog in the library listed in the SASMSTORE= system option. The SOURCE option stores the TEST macro definition in the same SASMACR catalog.

Note that the contents of the SASMACR catalog do not contain an entry for the macro source. The source has been combined with the macro entry that contains the compiled macro. To verify that the source has been saved, add the DES= option to the %MACRO statement. The DES= option enables you specify a description for the macro entry in the SASMACR catalog. So for example, you could add the following description when compiling the macro to indicate that the source code has been saved:

%macro test / store source des=’Source code saved with entry’;

 

You can look at the contents of the macro catalog using the CATALOG procedure:

proc catalog cat=a.sasmacr;                                                                                                            
contents;                                                                                                                               
run;                                                                                                                                    
quit;

 

You see the description indicating that the source code was saved with the macro entry in the output from PROC CATALOG:

Retrieve the Macro Source Code

When you need to update the macro or re-create the catalog on another machine, you can retrieve the macro source code using the %COPY statement. The %COPY statement enables you to retrieve the macro source code and write the code to a file. Here is the syntax:

%copy test / source outfile='c:\my macro library\test.sas';

 

This %COPY statement writes the source code for the TEST macro to the TEST.SAS file. Using TEST.SAS, you are now able to update the macro or compile the macro on another machine.

Remember, you should always save your source code when creating a stored compiled macro. Without the source code, you will not be able to update the macro or move the macro to a new environment.

Here are the relevant links for this article:

Share

About Author

Kevin Russell

SAS Technical Support Engineer, CAS and Open Source Languages

Kevin Russell is a Technical Support Engineer in the CAS and Open Source Languages group in Technical Support. He has been a SAS user since 1994. His main area of expertise is the macro language, but provides general support for the DATA step and Base procedures. He has written multiple papers and presented them at various SAS conferences and user events.

6 Comments

  1. Rowinder Bhageloe on

    @Edward SASAUTOS is used to define a storage location (or multiple locations) to fetch sourcecode from to turn into complied code. This happens upon calling a macro that hasnt been compiled yet. If you want to use a compiled macro thats stored in a catalog, use the MSTORED option.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Example of how to use SASAUTOS:
    Situation: You are calling the macro test by using the code "%test". This macro has not been compiled yet, but you do have the source code stored in a file in the windows folder C:\library. In order to still be able to autocall the (default).macros in sasautos, you can use this code:

    Options mautosource sasautos=('C:\library",sasautos);
    %test

    Upon running above code, SAS will search those libraries for an entry called test. If found, it gets compiled. If it's not found, you will get an error in the log stating the called macro could not be found.
    In case this macro had already been compiled, SAS will not search for code to compile in the defined areas.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Example of how to use MSTORED:
    Situation: You have a compiled macro called test stored in a library.

    Code:
    libname testlib 'C:\library;'
    OPTIONS MSTORED SAMSTORE=testlib;
    %test

    Upon running this code, SAS will look for precompiled code in the default catalog (work.sasmacr/work.sasmac1) and testlib.sasmacr.

    • Rowinder Bhageloe on

      correction to: Options mautosource sasautos=('C:\library",sasautos);
      ==>
      Options mautosource sasautos=('C:\library',sasautos);
      dont mix single and double quotes 🙂

  2. Hi Kevin,
    Thanks for your sharing.

    May I ask you how to invoke the macro defined in a catalog?
    Is it possible to put into SASAUTO?
    Thanks in advance.

    BR./Edward

Leave A Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top