©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina 1
Reusing Macro Programs
Macros in temporary SAS catalogs are only available for execution during the current SAS session. Such catalogs are deleted at the end of the session.
Macros can be stored permanently for reuse later.
Methods for storing macros permanently:
– the % INCLUDE statement
– the autocall macro facility
– permanently stored compiled macros
2
Storing Macro Definitions in External
Files
Save macros in an external file.
Use a %INCLUDE statement to insert the statements into a program.
%INCLUDE file-specification </SOURCE2>;
– file-specification is the location of the file with the SAS code to be inserted
– SOURCE2 directs SAS to display the inserted
SAS code in the log
3
Advantage of Storing Macro Definitions in External Files and Using the
%INCLUDE Statement
Source code of the macro definition does not need to be in the program.
A single copy of the macro definition is accessible to other programs.
Macro definitions in external files are easily viewed and edited with any text editor.
No special SAS system options are required to access macros this way.
4
Storing Macro Definitions in Catalog
SOURCE entries
Store each macro program in a separate
SOURCE entry.
It is recommended to give each SOURCE entry the same name as the macro program being stored.
Reminder: SAS catalogs are members of SAS libraries and contain content other than just program source code. Use PROC CATALOG to view a list of the contents of the SAS Catalog.
5
Storing Macro Definitions in Catalog
SOURCE entries
To store program as SOURCE
-
Copy code to new Editor window
-
Select Save Object As..
-
Create new catalog in a directory that can be used as a library
-
Provide an entry name, preferably identical to the macro name
6
The CATALOG Procedure
The Explorer window can be used to view a
SAS Catalog’s contents by navigating to the catalog and double-clicking it.
PROC CATALOG CATALOG= libref.catalog
;
CONTENTS;
QUIT;
Libref.catalog
is a two-level catalog name
CAT= is an alias for CATALOG=
7
The CATALOG Access Method
Use the CATALOG access method in a
FILENAME statement in conjunction with the
%INCLUDE statement to insert the macro definition into a SAS program.
FILENAME fileref
CATALOG ' libref.catalog
.entry-name.entry-type';
%INCLUDE fileref;
libref.catalog
.entry-name.entry-type is a fourlevel SAS catalog entry name entry-type is SOURCE
8
The CATALOG Access Method
Referencing Multiple SOURCE Entries
Multiple SOURCE entries can be referenced as long as the entries are in the same SAS catalog.
FILENAME fileref CATALOG ' libref.catalog
';
%INCLUDE fileref(entry-1);
%INCLUDE fileref(entry-2);
libref.catalog
is a two-level catalog name entry-1 and entry-2 are names of SOURCE entries in libref.catalog
9
Using the AUTOCALL Facility
Permanently store macro definitions in source libraries called autocall libraries.
An autocall library, whether it be the default one or a user-created one, is either a SAS catalog, an external directory, or a partitioned data set.
SAS provides several macro definitions in a default autocall library.
Multiple autocall libraries can be concatenated.
Specify the SASAUTOS and MAUTOSOURCE system options.
10
Default Autocall Library and Autocall
Macros Provided with SAS Software
SAS provides several macros in a default autocall library.
The libraries provided by SAS will depend on the SAS products licensed to your site.
These autocall macros can be used without having to define or include them in your programs.
Upon SAS installation, the autocall libraries are included in the value of the SASAUTOS system option in the configuration file.
11
Accessing Autocall Macros
In order to access autocall macros, use two system options:
MAUTOSOURCE system option must be specified
SASAUTOS system options must identify location of autocall library or libraries
12
MAUTOSOURCE System Option
Specifies Autocall Facility is Available
OPTIONS MAUTOSOURCE | NOMAUTOSOURCE;
MAUTOSOURCE is the default and causes the macro processor to search the autocall libraries for a member with the requested name when a macro name is not found in the WORK library.
NOMAUTOSOURCE prevents the macro processor from searching the autocall libraries when a macro name is not found in the WORK library.
13
SASAUTOS Controls Where Macro
Facility Looks for Autocall Macros
Options SASAUTOS= library-1;
Options SASAUTOS= ( library-1, . . . , library-n );
library-1 is a location that contains library members that contain a
SAS macro definition. A location can be a SAS fileref or a hostspecific location name enclosed in quotation marks. Each member contains a SAS macro definition.
( library-1, . . . , library-n ) identifies two or more locations that contain library members that contain a SAS macro definition. When you specify two or more autocall libraries, enclose the specifications in parentheses and separate them with either a comma or a blank space.
14
Using Stored Compiled Macro Facility
When a macro is compiled, it is stored in the temporary SAS catalog default.
Work.Sasmacr
by
These temporarily-stored macros can be stored in a permanent SAS catalog.
Use the Stored Compiled Macro Facility compiled macros.
to access permanent SAS catalogs that contain
15
Advantages of Using Stored Compiled
Macros
Since the macros are already compiled, SAS does not need to compile them again when a macro call is made.
Session-compiled macros and the autocall facility are also available in the same session.
Users cannot modify compiled macros.
16
System Options that Affect Stored
Compiled Macros
MSTORED controls whether the Stored
Compiled Macro Facility is available.
SASMSTORE controls where the macro facility looks for stored compiled macros.
These can be set either at SAS invocation or with an OPTIONS statement during program execution.
17
MSTORED System Option
OPTIONS MSTORED | NOMSTORED;
MSTORED searches for stored compiled macros in a catalog in the SAS library referenced by the
SASMSTORE= option.
NOMSTORED does not search for compiled macros.
18
SASMSTORE= System Option
OPTIONS SASMSTORE= libref;
libref specifies the libref of a SAS library that contains, or will contain, a catalog of stored compiled SAS macros. This libref cannot be WORK.
19
Steps to Creating a Stored Compiled
Macro
1.
2.
3.
Assign a libref to the SAS library in which the compiled macro will be stored.
Set the system options MSTORED and
SASMSTORE=libref.
Use the STORE option in the %MACRO statement when you submit the macro definition.
20
Macro Definition with STORE Option
%MACRO macro-name <( parameter-list )>/STORE
<DES=’ description ’>; text
%MEND < macro-name >;
description is an optional 156-character description that appears in the catalog directory parameter-list names one or more local macro variables text can be constant text, macro variables, macro functions, macro program statements, or a combination of the above
21
Using the SOURCE Option
Instead of saving the source program separately from the stored compiled macro, use the SOURCE option in the
%MACRO statement to combine and store the source of the compiled macro with the compiled macro code.
The SOURCE option requires that the STORE option and the MSTORED option be set.
%macro macro-name <(parameter-list)>/STORE SOURCE;
The source code saved by the SOURCE option begins with the %MACRO keyword and ends with the semicolon following the %MEND statement.
Do not use the option on nested macro definitions.
22
Accessing Stored Compiled Macros
Follow these steps to access a stored compiled macro:
1.
Assign a libref to the SAS library that contains a Sasmacr stored.
catalog in which the macro was
2.
3.
Set the system options MSTORED and
SASMSTORE=libref.
Call the macro.
23
Accessing Stored Macro Code
If you used the SOURCE option with the %MACRO statement to store your macro source code with the stored compiled macro, use the %COPY statement to access the stored source code.
24
%COPY Statement
%COPY macro-name /SOURCE < other option(s)>;
macro-name is the ma cro name that the %COPY statement will use
SOURCE or SRC specifies that the source code of the macro will be copied to the output destination. If the OUTFILE= option is not specified, the source is written to the SAS log.
other options must be one or more of the following:
– LIBRARY= libref or LIB= specifies the libref of 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. Restriction: This libref cannot be
WORK.
– OUTFILE= fileref | ’external file’ or OUT= specifies the output destination of the %COPY statement. The value can be a fileref or an external file.
25