Using the Autocall Facility

Overview

You can make macros accessible to your SAS session or program by using the autocall facility to search predefined source libraries for macro definitions. These predefined source libraries are known as autocall libraries. You can store your macro definitions permanently in an autocall library, and you can set up multiple autocall libraries.
When you use this approach, you do not need to compile the macro in order to make it available for execution. That is, if the macro definition is stored in an autocall library, then you do not need to submit or include the macro definition before you submit a call to the macro.
Suppose you have stored a file that contains a macro definition in your autocall library. When you submit a call to that macro
  • the macro processor searches the autocall library for the macro
  • the macro is compiled and stored as it would be if you had submitted it (that is, the compiled macro is stored in the default location of Work.Sasmacr)
  • the macro is executed.
Once it has been compiled, the macro can be executed as needed throughout the same SAS session. At the end of the SAS session, the compiled macro is deleted from the Work.Sasmacr catalog, but the source code remains in the autocall library.

Creating an Autocall Library

An autocall library can be either
  • a directory that contains source files
  • a partitioned data set (PDS)
  • a SAS catalog.
The method for creating an autocall library depends on the operating environment that you are using.
To create an autocall library in a directory-based operating system, such as Windows or UNIX, create a directory in which to store macro definitions. Each macro definition in this directory is a separate file that has the extension .sas and that has the same name as the macro that it contains.

Example

Suppose you want to save the macro Prtlast in an autocall library. In a directory-based operating system, the first step is to create a directory that holds your macro source files. You can use the Save As window to create the directory, and to save the macro definition in that directory. With the Prtlast definition in an active code editing window, select File>Save As. In the Save As window, navigate to the location where you want to create your autocall library. Select New Folder, enter the directory name, and click OK. Then enter Prtlast as the filename, make sure the file type is .sas, and click Save.
Tip
You could also use the FILE command to save your macro definition in an autocall library. To use the FILE command, you enter file '<path>external-file-name' in the command line.

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
You might be familiar with SAS functions such as TRIM and LEFT. The macros that SAS supplies look like macro functions, but they are in fact macros. One of the useful things about these macros is that in addition to using them in your SAS programs, you can see their source code.

Example

The macro definition for the Lowcase macro is shown below. Notice that the comments that are included in this macro provide information about using the macro. All of the macros that SAS provides in the autocall library include explanatory comments so that they can be easy for you to understand and use.
%macro lowcase(string);
%******************************************************;
%*                                                    *;
%* MACRO: LOWCASE                                     *;
%*                                                    *;
%* USAGE: 1) %lowcase(argument)                       *;
%*                                                    *;
%* DESCRIPTION:                                       *;
%*   This macro returns the argument passed to        *;
%*   it unchanged except that all upper-case          *;
%*   alphabetic characters are changed to their       *;
%*   lower-case equivalents.                          *;
%*                                                    *;
%* E.g.:  %let macvar=%lowcase(SAS Institute Inc.);   *;
%* The variable macvar gets the value                 *;
%*  "sas institute inc."                              *;
%* NOTES:                                             *;
%*   Although the argument to the %UPCASE macro       *;
%*   function may contain commas, the argument to     *;
%*   %LOWCASE may not, unless they are quoted.        *;
%*   Because %LOWCASE is a macro, not a function,     *;
%*   it interprets a comma as the end of a parameter. *;
%******************************************************;
%sysfunc(lowcase(%nrbquote(&string)))
%mend;

Accessing Autocall Macros

Remember that an autocall library is either a SAS catalog, an external directory, or a partitioned data set. This is true both for the default autocall library that SAS supplies and for autocall libraries that you create.
In order to access a macro definition that is stored in an autocall library, you must use two SAS system options, as follows:
  • The MAUTOSOURCE system option must be specified.
  • The SASAUTOS= system option must be set to identify the location of the autocall library or libraries.
Both the MAUTOSOURCE and SASAUTOS= system options can be set either at SAS invocation or with an OPTIONS statement during program execution.
The MAUTOSOURCE system option controls whether the autocall facility is available.
General form, MAUTOSOURCE system option:
OPTIONS MAUTOSOURCE | NOMAUTOSOURCE;
Here is an explanation of the syntax:
MAUTOSOURCE
is the default setting, and specifies that the autocall facility is available.
NOMAUTOSOURCE
specifies that the autocall facility is not available.
The SASAUTOS= system option controls where the macro facility looks for autocall macros.
General form, SASAUTOS= system option:
OPTIONS SASAUTOS=library-1;
OPTIONS SASAUTOS=(library-1,...,library-n);
Here is an explanation of the syntax:
the values of library-1 through library-n
are references to source libraries that contain macro definitions. To specify a source library that you can
  • use a fileref to refer to its location
  • specify the pathname (enclosed in quotation marks) for the library.
Unless your system administrator has changed the default value for the SASAUTOS= system option, its value is the fileref Sasautos, and that fileref points to the location where the default autocall library was created during installation. The Sasautos fileref can refer to multiple locations that are concatenated.
Generally, it is a good idea to concatenate any autocall libraries that you create yourself with the default autocall library in the value of the SASAUTOS= system option. Otherwise, the new autocall library replaces the default or existing libraries in the value of SASAUTOS=, and the autocall facility has access to only the new autocall library.

Example

Suppose you want to access the Prtlast macro, which is stored in the autocall library C:Mysasfiles. You also want to make sure that the default autocall library (which the fileref Sasautos points to) is still available to the autocall facility. You would submit the following code:
options mautosource sasautos=('c:mysasfiles',sasautos);
%prtlast
Note: The MAUTOLOCDISPLAY option is a Boolean option that causes a note to be issued to the SAS log indicating where the source code was obtained to compile an autocall macro. The note is similar to the information displayed when using the MLOGIC option. The default setting of this option is NOMAUTOLOCDISPLAY.
When the autocall facility is in effect, if you invoke a macro that has not been previously compiled, the macro facility automatically
  1. searches the autocall library (or each autocall library in turn if multiple libraries are identified in the SASAUTOS= system option) for a member that has the same name as the invoked macro
  2. brings the source statements into the current SAS session if the member is found
  3. issues an error message if the member is not found
  4. submits all statements in the member in order to compile the macro
  5. stores the compiled macro in the temporary catalog Work.Sasmacr
  6. calls the macro.
The autocall facility does not search for a macro in the autocall library if the macro has already been compiled during the current SAS session. In that case, the session-compiled macro is executed.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.93.236