Using the Autocall Facility

A Brief 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 store macro definitions in an autocall library, you do not need to compile the macro definition ahead of time 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. Here is what happens 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 in either of these forms:
  • a directory that contains source files
  • a SAS catalog
To create an autocall library in a directory-based operating system, create a directory in which to store macro definitions. Each macro definition in this directory will be a separate file that has the extension .sas and that has the same name as the macro that it contains.
Note: On UNIX the file name containing the macro definition must be in all lowercase letters. It is considered a best practice to use all lowercase letters for macro definition program files on all operating systems.

Example: Saving a Macro

To save the definition for the macro PrintLast in an autocall library, first determine where you want to save your autocall macro file. You can either use an existing folder containing other autocall macro programs (if you have Write access) or you can create a new folder to store your autocall macros. Then, save your macro definition program in a file named printlast.sas in the designated folder.

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: Using the LOWCASE Macro

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, making them easy for you to understand and use.
Log 10.2 SAS Log
%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

Overview

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

The MAUTOSOURCE system option controls whether the autocall facility is available.
Syntax, MAUTOSOURCE system option:
OPTIONS MAUTOSOURCE | NOMAUTOSOURCE;
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

The SASAUTOS= system option controls where the macro facility looks for autocall macros.
Syntax, SASAUTOS= system option:
OPTIONS SASAUTOS=library-1;
OPTIONS SASAUTOS=(library-1,...,library-n);
the values of library-1 through library-n
are references to source libraries that contain macro definitions. To specify a source library:
  • 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.
Tip
Remember to concatenate any autocall libraries that you create yourself with the default autocall library supplied by SAS. Otherwise, the new autocall library will replace the default or existing libraries in the value of SASAUTOS=, and the autocall facility will have access to only the new autocall library.

Example: Accessing Autocall Macros

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
When the autocall facility is in effect, if you invoke a macro that has not been previously compiled, here is what the macro facility automatically does:
  1. It searches the autocall library (or each autocall library in turn if multiple libraries are identified in the SASAUTOS= system option) for a library member that has the same name as the invoked macro.
  2. It brings the source statements into the current SAS session if the library member is found.
  3. It issues an error message if the library member is not found.
  4. It submits all statements in the library member in order to compile the macro.
  5. It stores the compiled macro in the temporary catalog Work.Sasmacr.
  6. It 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.
Note: To see what SASAUTOS is set to, run the following statements:
%put %sysfunc(getoption(sasautos));
%put %sysfunc(pathname(sasautos)); 
Last updated: October 16, 2019
..................Content has been hidden....................

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