Using Stored Compiled Macros

The Stored Compiled Macro Facility

Remember that when a macro is compiled, it is stored in the temporary SAS catalog Work.Sasmacr by default. You can also store compiled macros in a permanent SAS catalog. Then you can use the Stored Compiled Macro Facility to access permanent SAS catalogs that contain compiled macros.
There are several advantages to using stored compiled macros:
  • SAS does not need to compile a macro definition 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.
Two SAS system options affect stored compiled macros: MSTORED and SASMSTORE=. The MSTORED system option controls whether the Stored Compiled Macro Facility is available.
General form, MSTORED system option:
OPTIONS MSTORED | NOMSTORED;
Here is an explanation of the syntax:
NOMSTORED
is the default setting, and specifies that the Stored Compiled Macro Facility does not search for compiled macros.
MSTORED
specifies that the Stored Compiled Macro Facility searches for stored compiled macros in a catalog in the SAS library that is referenced by the SASMSTORE= option.
The SASMSTORE= system option controls where the macro facility looks for stored compiled macros.
General form, SASMSTORE= system option:
OPTIONS SASMSTORE=libref;
Here is an explanation of the syntax:
libref
specifies the libref of a SAS library that contains, or contains, a catalog of stored compiled SAS macros. This libref cannot be Work.
The MSTORED and SASMSTORE= system options can be set either at SAS invocation or with an OPTIONS statement during program execution.

Creating a Stored Compiled Macro

To create a permanently stored compiled macro, you must do the following:
  1. assign a libref to the SAS library in which the compiled macro is stored
  2. set the system options MSTORED and SASMSTORE=libref
  3. use the STORE option in the %MACRO statement when you submit the macro definition.
General form, macro definition with STORE option:
%MACRO macro-name <(parameter-list)> /STORE
<DES='description'>;
text
%MEND <macro-name>;
Here is an explanation of the syntax:
description
is an optional 156-character description that appears in the catalog directory.
macro-name
names the macro.
parameter-list
names one or more local macro variables whose values you specify when you invoke the macro.
text
can be
  • constant text, possibly including SAS data set names, SAS variable names, or SAS statements
  • macro variables, macro functions, or macro program statements
  • any combination of the above.
There are several restrictions on stored compiled macros.
  • Sasmacr is the only catalog in which compiled macros can be stored. You can create a catalog named Sasmacr in any SAS library. You should not rename this catalog or its entries.
  • You cannot copy stored compiled macros across operating systems. You must copy the source program and re-create the stored compiled macro.
  • The source cannot be re-created from the compiled macro. You should retain the original source program. For convenience, you can store the source program in an autocall library. Alternatively, you can store the source program as a source entry in the same catalog as the compiled macro.

Using the SOURCE Option

An alternative to saving your source program separately from the stored compiled macro is to 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. The %MACRO statement below shows the correct syntax for using the SOURCE option.
%macro macro-name<(parameter list)> /STORE SOURCE;
The source code that is saved by the SOURCE option begins with the %MACRO keyword and ends with the semicolon following the %MEND statement.
Tip
The SOURCE option cannot be used on nested macro definitions.

Example

Suppose you want to store the Words macro in compiled form in a SAS library. This example shows the macro definition for Words. The macro takes a text string, divides it into words, and creates a series of macro variables to store each word.
Notice that both the STORE option and the SOURCE option are used in the macro definition so that Words is permanently stored as a compiled macro and the macro source code is stored with it, as follows:
libname macrolib 'c:storedlib';
options mstored sasmstore=macrolib;

%macro words(text,root=w,delim=%str( ))/store source;
   %local i word;
   %let i=1;
   %let word=%scan(&text,&i,&delim);
   %do %while (&word ne );
      %global &root&i;
      %let &root&i=&word;
      %let i=%eval(&i+1);
      %let word=%scan(&text,&i,&delim);
   %end;
   %global &root.num;
   %let &root.num=%eval(&i-1);
%mend words;
If the Sasmacr catalog does not exist in the Macrolib library, it is automatically created. You can list the contents of the Macrolib.Sasmacr catalog to verify that the compiled macro was created, as follows:
proc catalog cat=macrolib.sasmacr;
   contents;
   title "Stored Compiled Macros";
quit;
Here is the output from the PROC CATALOG step if no other compiled macros are stored in Macrolib.Sasmacr.
Stored Compiled Macros

Accessing Stored Compiled Macros

In order to access a stored compiled macro, you must do the following:
  1. assign a libref to the SAS library that contains a Sasmacr catalog in which the macro was stored
  2. set the system options MSTORED and SASMSTORE=libref
  3. call the macro.

Example

The following program calls the Words macro. Assume that the Words macro was compiled and stored in an earlier SAS session.
libname macrolib 'c:storedlib';
options mstored sasmstore=macrolib;

%words(This is a test)
%put Number of Words (wnum): &wnum;
%put Word Number 1 (w1): &w1;
%put Word Number 2 (w2): &w2;
%put Word Number 3 (w3): &w3;
%put Word Number 4 (w4): &w4;
Here is a portion of the messages that are written to the SAS log when this code is submitted.
Table 12.2 SAS Log
9    libname macrolib 'c:storedlib';
NOTE: Libref MACROLIB was successfully assigned as follows:
      Engine:        V9
      Physical Name: c:storedlib
10       options mstored sasmstore=macrolib;
11
12       %words(This is a test)
13       %put Number of Words (wnum): &wnum;
Number of Words (wnum): 4
14       %put Word Number 1 (w1): &w1;
Word Number 1 (w1): This
15       %put Word Number 2 (w2): &w2;
Word Number 2 (w2): is
16       %put Word Number 3 (w3): &w3;
Word Number 3 (w3): a
17       %put Word Number 4 (w4): &w4;
Word Number 4 (w4): test

Accessing Stored Macro Code

If you use the SOURCE option of the %MACRO statement to store your macro source code along with the stored compiled macro, you can use the %COPY statement to access the stored source code.
General form, %COPY statement:
%COPY macro-name /SOURCE <other option(s)>;
Here is an explanation of the syntax:
macro-name
is the name of the macro whose source code is accessed.
SOURCE
specifies that the source code of the macro is copied to the output destination. If no output destination is specified, the source is written to the SAS log.
other options
include the following options:
  • LIBRARY= 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. The libref cannot be Work.
  • OUTFILE= specifies the output destination of the %COPY statement. The value can be a fileref or an external file.

Example

Suppose you submitted the program below to create a stored compiled macro named Words.
libname macrolib 'c:storedlib';
options mstored sasmstore=macrolib;

%macro words(text,root=w,delim=%str( ))/store source;
   %local i word;
   %let i=1;
   %let word=%scan(&text,&i,&delim);
   %do %while (&word ne );
      %global &root&i;
      %let &root&i=&word;
      %let i=%eval(&i+1);
      %let word=%scan(&text,&i,&delim);
      %end;
   %global &root.num;
   %let &root.num=%eval(&i-1);
%mend words;
The %COPY statement writes the source code for the Words macro to the SAS log. Here is an example:
%copy words/source;
The partial SAS log below shows the source code of the Words macro.
Table 12.3 SAS Log
17     %copy words/source;
%macro words(text,root=w,delim=%str( ))/store source;

        %local i word;
        %let i=1;
        %let word=%scan(&text,&i,&delim);
        %do %while (&word ne );
           %global &root&i;
           %let &root&i=&word;
           %let i=%eval(&i+1);
           %let word=%scan(&text,&i,&delim);
        %end;
        %global &root.num;
        %let &root.num=%eval(&i-1);
%mend words;
The Stored Compiled Macro Facility can be used in conjunction with the Autocall Facility and with session-compiled macros. When you submit a macro call such as %words, the macro processor searches for the macro Words as
  1. an entry named Words.Macro in the temporary Work.Sasmacr catalog.
  2. an entry named Words.Macro in the Libref.Sasmacr catalog. The MSTORED option must be specified, and the SASMSTORE= option must have a value of Libref.
  3. an autocall library member named Words that contains the macro definition for the macro Words. The MAUTOSOURCE option must be specified, and the value of the SASAUTOS= option must point to the autocall library.
Words.Macro
..................Content has been hidden....................

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