Sunteți pe pagina 1din 4

Expand Print

Text Version
Creating a Stored Compiled Macro

The stored compiled macro facility stores compiled macros in permanent SAS catalogs. There is no
need to recompile macro source code. This saves compilation time, which can be substantial for large
programs.
When you use the stored compiled macro facility, macro source code can be protected or hidden. To
enable macro storage using the stored compiled macro facility, you use the MSTORED and
SASMSTORE= system options. The MSTORED option enables the stored compiled macro facility,
and the SASMSTORE= option indicates where the macro is permanently stored. Notice that the
SASMSTORE libref cannot be work. The SASMSTORE= option designates a single library. To
include multiple libraries, concatenate the libraries.
You store a permanent compiled macro using options in the %MACRO statement. The options are
STORE, SOURCE, SECURE, and DES='text'.
The STORE option is required in order to store the compiled macro. This option stores the compiled
macro as a catalog entry in the library indicated by the SASMSTORE= system option.
The SOURCE option stores the macro source code with the compiled code. Use this if you want to
insure that your source code matches the compiled code. If the source is stored with the compiled
code, you can use the %COPY statement to retrieve the source into the log or a file.
The SECURE option encrypts the stored compiled macro and disables the %COPY statement and the
SYMBOLGEN, MLOGIC, and MPRINT options. This protects the code from being viewed or changed
in any way.
The DES='text' option specifies a description of the macro catalog entry, which can be helpful in
identifying the macro and its functionality.
This macro uses the STORE and SOURCE options, so the compiled macro is stored in a catalog
named SASMACR in the location specified in the SASMSTORE= system option with the macro
source code. When a macro is compiled and stored in a permanent library, a lock is placed on the
SASMSTORE=libref for the remainder of that session. Therefore the libref cannot be cleared or
reassigned while the lock remains in place. To close the stored compiled macro catalog, as well as to
release the lock and clear the libref, use the %SYSMSTORECLEAR statement.

Copyright 2015 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Text Version

Expand Print

Accessing a Stored Compiled Macro


To access a stored compiled macro in a new SAS session, you first assign a libref to the location
where the SASMACR catalog holds the user-defined compiled macros. Next, you activate the stored
compiled macro facility and point to the macro library. Then, you call the stored macro.

Although compiled code cannot be edited, the source code can be changed and the macro then
recompiled. You learned that the SOURCE option stores the macro source code with the permanent
compiled macro. To retrieve source code saved with the SOURCE option, you use the %COPY
statement with the SOURCE option.
Start with %COPY followed by the macro name and then a forward slash. After the slash you include
options such as LIBRARY= and OUT=, as well as the SOURCE keyword option. These options can
be placed on this statement in any order. End the statement with a semicolon.
Let's take a look at the options. You use the LIBRARY= option to specify a SAS library that contains a
catalog of stored compiled SAS macros. If no library is specified, the libref specified by the
SASMSTORE= option is used. The OUT= option specifies the output destination of the %COPY
statement. The value can be a fileref or an external file. If OUT= is omitted, the source code is written
to the SAS log.
Here is the code to copy the source code for the Calc macro to the SAS log. Because there is no
OUT= option, the source code is copied to the log.
Remember that if the SECURE option is used while storing the macro, the contents of the macro are
encrypted. If you use the SECURE and SOURCE options on a macro, no output is produced when
you use the %COPY statement, and a note appears in the log.

Copyright 2015 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Text Version

Expand Print

Creating an Autocall Macro


The autocall macro facility stores macro source code in external files or SAS catalogs. Autocall
macros are compiled automatically the first time they are called in a SAS session, and stored
inwork.sasmacr as session-compiled macros.
The autocall macro facility creates portable macros, allowing for convenience and cross-platform
availability. The macro facility includes an autocall library of SAS utility macros. You use SAS autocall
utility macros the same way that you use macro functions. SAS autocall macros are text files that are
installed with SAS and are placed into a storage location where the SAS application knows to access
them. You can concatenate your own library of utility macros to the SAS autocall library.
To enable the autocall macro facility, you use the MAUTOSOURCE system option. The SASAUTOS=
option specifies autocall library locations. The locations library-1through library-n are references to
source libraries that contain macro definitions. You specify one or more source libraries by placing the
name in quotation marks or by pointing with a fileref. The reserved fileref SASAUTOS is assigned to
the autocall library supplied by SAS.
If you don't include SASAUTOS in your statement, the autocall library delivered by SAS is disabled.
To store a macro as an autocall macro, you save it as a text file within the directory specified in the
SASAUTOS= system option. The file name must match the macro definition name, and use the
extension .sas. For instructions about saving in UNIX or z/OS, click theInformation button.

Copyright 2015 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Text Version

Expand Print

Accessing an Autocall Macro


To access a macro in the autocall library, first activate the autocall macro facility and point to the
autocall library. Then, call any macro in the library. If the macro is not already compiled during the
session, SAS attempts to locate the macro. Autocall libraries are searched in the order they appear in
the SASAUTOS= option. In this case the SAS-supplied autocall library appears last, so it is searched
last.
After it is located, the macro is compiled automatically, stored in work.sasmacr as a sessioncompiled macro, and executed. You can use the MAUTOLOCDISPLAY system option to determine
where the macro was located. This option displays the autocall macro source location in the log when
the macro is called. You simply add MAUTOLOCDISPLAY to the OPTIONS statement.
The note shows the path and filename of the newly invoked macro. Let's take a look at the way that
SAS searches for macros when they are called. The first place that SAS looks for a macro is in the
temporary work location work.sasmacr. This is where macros are stored by default when they are
compiled in a session. If the macro is found, it is executed from this location.
If the macro is not found in thework.sasmacr catalog and you provided the MSTORED and
SASMSTORE=options, SAS next looks for a stored compiled macro in the location provided in the
SASMSTORE= option. If the macro is found, it is executed from this location.
If the macro is still not found, and you provided MAUTOSOURCE and SASAUTOS= options, SAS
looks in the autocall library locations provided in the SASAUTOS= option and compiles and executes
the macro if found. If you provided MAUTOSOURCE but you did not provide SASAUTOS= options,
only the autocall macros delivered by SAS are found.
After the macro is compiled, it is stored as a session-compiled macro in the temporary work
location work.sasmacr.
If the macro is still not found, a warning is issued in the log.
To maximize efficiency, by default SAS searches autocall libraries for an undefined macro only one
time. If you want SAS to search again, use the MRECALL option. The MRECALL option makes SAS
search autocall libraries each time that the macro is invoked. Although this is less efficient, it can be
useful for testing, or if the specified library is incorrect.
Heres a question. Suppose you write and call the Calc autocall macro. It is compiled and stored
in work.sasmacr. You change the source code and re-save the macro definition. Does the new
version execute if you call Calc again in the same session? No. You must first delete the Calc macro
from the work.sasmacr catalog so that SAS searches for, compiles, stores and executes the new
version during the session. Use the %SYSMACDELETE statement to delete the Calc macro from
the work.sasmacr catalog.

Remember, if an autocall library macro was previously referenced and compiled during the session
and you want to repeat a search of the macro, you must delete the macro from
the work.sasmacr catalog first.

Copyright 2015 SAS Institute Inc., Cary, NC, USA. All rights reserved.

S-ar putea să vă placă și