When you submit a macro definition, the macro processor compiles and stores the macro in a SAS catalog in the WORK library by default. These macros, referred to as session compiled macros, exist only during the current SAS session. To save frequently used macros between sessions, you can use either the autocall macro facility or the stored compiled macro facility.
Differences from Standard Macros
Unlike standard macros, Autocall Macros are stored in specific libraries, enabling a more organized and standardized code structure. They can be called without prior definition, providing a seamless programming experience.
Autocall Macro Facility
SAS has a feature called the autocall macro facility. It stores macros in external files, which are known as autocall libraries. This makes it easier for different users and applications to manage and access macros. When autocall libraries are concatenated, the first time a macro is used in a session, it needs to be compiled. To avoid this, saved macros can be used instead.
Default Autocall Library
SAS provides several macros in a default autocall library for you. Some of the macros in the autocall library that SAS provides are listed here.
Macro Syntax | Purpose |
%LOWCASE(argument) | converts letters in its argument from uppercase to lowercase |
%QLOWCASE(argument) | converts letters in its argument from uppercase to lowercase, and returns a result that masks special characters and mnemonic operators |
%LEFT(argument) | removes leading blanks from the argument |
%TRIM(argument) | removes trailing blanks from the argument |
%CMPRES(argument) | removes multiple blanks from the argument |
%DATATYP(argument) | returns the string NUMERIC or CHAR, depending on whether the argument is an integer or a character string |
Setting Up the Autocall Macro Facility
To utilize Autocall Macros, specific requirements must be met, including having access to SAS software and understanding the basic macro processing.
Configuration Steps
Setting up Autocall Macros involves several critical steps:
- Defining the library
- Specifying the path
- Invoking the macros
Creating an Autocall Macro
Creating an Autocall Macro involves writing specific code, utilizing SAS’s unique syntax. Here’s an example code:
%macro print(dsn=,obs=);
proc print data=&dsn(obs=&obs);
quit;
%mend
Then, place the macros you plan to use in a pre-defined place, such as "/home/subhroster20070/9to5sas/AutocallMacros"
Please ensure that the name of the macro is identical to the filename for proper functionality.
You are done. The next time you open a SAS session on your local machine any of the macros in your macro library are available to use.
Invoking the Autocall Macro
Add the Filename Statement: You need to add the following statement, which points to the directory where you have stored your macros.
filename MyMacros '/home/subhroster20070/9to5sas/AutocallMacros';
Set MAUTOSOURCE and SASAUTOS
In SAS, the Autocall facility is used to store and execute macros that can be called from any part of a SAS session. The MAUTOSOURCE
option and the SASAUTOS
system options are closely related to this Autocall facility.
Set MAUTOSOURCE
MAUTOSOURCE
is a system option in SAS that allows you to enable or disable the Autocall facility.
- Enable: If MAUTOSOURCE is turned on, SAS searches the libraries specified in the SASAUTOS system option for macros when a
%macro-name
is encountered but not defined in the current session. - Disable: Turning MAUTOSOURCE off will prevent SAS from looking for macros in the Autocall libraries.
You can enable or disable the MAUTOSOURCE
option using the following commands:
- Enable:
options mautosource;
- Disable:
options noautosource;
SASAUTOS System Option
SASAUTOS
is a system option that specifies the library or libraries where the Autocall macros are stored. You can define one or multiple libraries that SAS will search when attempting to execute an undefined macro (when MAUTOSOURCE
is enabled).
Here’s an example to set up the SASAUTOS
option with a specific library:
filename MyMacros '/home/subhroster20070/9to5sas/AutocallMacros';
options mautosource SASAutos=(MyMacros);
These two options together allow a high level of flexibility and organization in managing and executing macros across different SAS programs and sessions, ensuring that you can effectively reuse code and maintain consistent coding practices.
Now, you can call the saved macro as you would call a regular macro.
%print(dsn=sashelp.class,obs=5);
you can also add the filename statement and the MAUTOSOURCE and SASAutos Options in the autoexec file.
To edit the autoexec file in SAS Enterprise Guide, follow these steps:
- Click on the “Tool” menu.
- Select “Options”.
- Select “SAS Programs”
- Check the “Submit SAS code when Server is connected” and click on “Edit”.
Copy the filename and options statement, then save it.
Showing the source of the macro.
You can show the location information of the macro by using the Mautolocdisplay option.
MAUTOLOCDISPLAY = It displays the source location of the autocall macros in the log when the autocall macro is invoked.
filename MyMacros '/home/subhroster20070/9to5sas/AutocallMacros';
options mautolocdisplay mautosource SASAutos=(MyMacros);
%print;
MAUTOLOCDISPLAY(PRINT2): This macro was compiled from the autocall file /home/subhroster20070/9to5sas/AutocallMacros/print.sas
Note: If an autocall library member contains more than one macro, the macro processor compiles all of the macros but executes only the macro with the name you invoked
Stored compiled macro facility
The stored compiled macro facility stores compiled macros in a SAS catalog in a SAS data library that you specify. By using stored compiled macros, you may save macro compilation time in your production-level jobs. However, because these stored macros are compiled, you must save and maintain the source for the macro definitions in a different location.
Steps to create a permanent SASMACR Catalog.
- Create the libname with the fileref where you want to store the macro.
libname mymacros '/home/subhroster20070/9to5sas/AutocallMacros';
- Activate the SYSTEM options MSTORED SASMSTORE = mymacros
- MSTORED=Allows the SAS System to search for compiled macros stored in the SASMACR catalog. It is a system option which enables the storage of compiled macros in a permanent SAS library.
- SASMSTORE= Specify the libname that stores the compiled macros in the catalog SASMACR. Libname cannot be WORK. It is a system option that designates a permanent library to store compiled macros.
When the macro processor finds the macro program in SASMACR catalog, it submits it for immediate execution (compilation was already done before).
Example:
%SYSMSTORECLEAR;
libname mymacros '/home/subhroster20070/9to5sas/AutocallMacros';
options mstored sasmstore=mymacros;
%macro print(dsn=,obs=)/store source des="Print Macro";
proc print data=&dsn.(obs=&obs);
quit;
%mend;
There are a few more options this time in a macro statement. These options are:
- STORE option = The compiled macro is saved in a SAS catalog which is part of a permanent SAS data library.
- SOURCE option = In SAS version 9.1, the macro source code and compiled code can be stored together using the SOURCE option, which requires the STORE and MSTORED options to be set. The saved code begins with the %MACRO statement and ends with the %MEND statement.
- DES option = To describe a macro entry in the macro catalog, you need to enclose the description in quotation marks. This description will then appear in the CATALOG window when you view the contents of the catalog that stores the compiled macro facility.
Using stored compiled macros
%SYSMSTORECLEAR;
libname mymacros '/home/subhroster20070/9to5sas/AutocallMacros';
options mstored sasmstore=mymacros;
%print(dsn=sashelp.class,obs=5);
Here’s a brief description of what each line is doing:
- %SYSMSTORECLEAR; This statement clears the macro catalog of all stored compiled macros. This is useful when you want to make sure that you are starting with a fresh macro environment.
- libname mymacros ‘/home/subhroster20070/9to5sas/AutocallMacros’;: This line is defining a library reference named “mymacros” to the given path. This is where SAS will look for macro files.
- options mstored sasmstore=mymacros;: This sets the options for using stored compiled macros and specifies the SAS macro store library (defined earlier).
- %print(dsn=sashelp.class,obs=5);: This line is calling a macro named
%print
with specific parameters. It assumes that the%print
macro is already defined and stored in the “mymacros” library. The macro is expected to print the first 5 observations (obs=5
) from the dataset referred to bydsn=sashelp.class
.
How do you find which macros are stored in a catalog?
If you need to locate all macro programs that have been saved in the SASMACR catalog, then you can use PROC CATALOG.
libname mymacros '/home/subhroster20070/9to5sas/AutocallMacros';
PROC CATALOG catalog=mymacros.sasmacr;
Contents;
Run;
You can use PROC SQL to obtain information about all compiled macros.
PROC SQL;
SELECT * FROM
DICTIONARY.CATALOGS WHERE MEMNAME IN ('SASMACR') and libname="MYMACROS";
Quit;
%COPY – a new statement
To copy specific items from a SAS macro library, use the %COPY statement. This statement allows you to access stored macro source code, which can then be written to the SAS log or an external file if preferred.
Here is an example of copying macro source code to a log window.
libname mymacros '/home/subhroster20070/9to5sas/AutocallMacros';
options mstored sasmstore=mymacros;
%COPY print / source;
Here’s an example of how to copy to an external file.
libname mymacros '/home/subhroster20070/9to5sas/AutocallMacros';
options mstored sasmstore=mymacros;
%COPY print /source out = '/home/subhroster20070/9to5sas/AutocallMacros/m.txt';
Having the ability to copy the code then permits the programmer to modify and recompile the macro as necessary.
SAS Version 9 has a new feature that simplifies the use of the compiled library approach. You can now hide executed code to prevent it from appearing in the log. To do this, store the code as a compiled macro in a storage location. As a result, the macro cannot be viewed in an editor.
It is important to note that the macro provides the ability to disable logging options that write information about the code to the log. Here is an example.
%macro print(dsn=,obs=)/ store des="Print Macro";
options nonotes nomlogic nomprint nosymbolgen nosource nosource2;
proc print data=&dsn.(obs=&obs);
quit;
%mend;
ERROR: The /SOURCE option was not specified when the macro PRINT was compiled.
By storing the code as a compiled macro, virtually no information is written to the log about the code. Only warnings and errors will be written in the log.
Deleting stored compiled Macro
To delete a stored compile macro, you can use the PROC DATASET procedure with the MEMTYPE=CATALOG option, followed by the DELETE statement and the library name.
proc datasets memtype=catalog library=mymacros;
delete mymacros;
run;