Storing Macro Definitions in Catalog SOURCE Entries

Overview

Another way of permanently storing macros is to store a macro definition in a SOURCE entry in a SAS catalog. If you decide to store your macro programs in a SAS catalog, you must store each macro program in a separate SOURCE entry. It is a good idea to give each SOURCE entry the same name as the macro program that is stored within it. For example, a macro named Printit would be stored in a SOURCE entry that is also named Printit.
Note: SAS catalogs are members of SAS libraries that store program source code and other types of content.
To store a macro definition as a SOURCE entry in a SAS catalog, you use the Save As Object window.

Example

To save the Printit macro definition to the Sasuser.Mymacs catalog, perform these steps:
  1. Select File>Save As Object. In the Save As Object window, select the Sasuser library.
  2. If the Sasuser.Mymacs catalog does not already exist, you need to create it. You can either select the Create New Catalog icon or right-click the Save As Object window and select New in order to open the New Catalog window. Enter Mymacs as the name for the new catalog and click OK.
  3. Enter Printit in the Entry Name field. Make sure that the Entry Type is set to SOURCE entry (SOURCE), and click Save.
Tip
If you use the Program Editor, you could also use the SAVE command to save your macro definition as a catalog SOURCE entry. To use the SAVE command, you enter save libref.catalog.entry.source in the command line where libref.catalog.entry is the libref, the catalog name, and the entry name.

The CATALOG Procedure

If you store your macros in a SAS catalog, you might want to view the contents of a particular catalog to see the macros that you have stored there. You can use the Explorer window to view the contents of a SAS catalog by navigating to the catalog and double clicking it. You can also use the CATALOG procedure to list the contents of a SAS catalog. The CONTENTS statement of the CATALOG procedure lists the contents of a catalog in the procedure output.
General form, CATALOG procedure with CONTENTS statement:
PROC CATALOG CATALOG=libref.catalog;
CONTENTS;
QUIT;
Here is an explanation of the syntax:
libref.catalog
is a valid two-level catalog name.
Note: CAT= is an alias for CATALOG.

Example

You can use PROC CATALOG to view all of the macros that are stored in the temporary Work.Sasmacr catalog, as follows:
proc catalog cat=work.sasmacr;
   contents;
   title "Default Storage of SAS Macros";
quit;
This PROC CATALOG step produces results that are similar to the output shown below. The macros that are actually listed are the macros that have been compiled during the current SAS session.
Default Storage of SAS Macros
PROC CATALOG can display the names and attributes of compiled macros, but the macro definition itself cannot be viewed.

The CATALOG Access Method

If you store a macro definition in a SOURCE entry of a SAS catalog, you can use the CATALOG access method in a FILENAME statement in conjunction with the %INCLUDE statement to insert the macro definition into a SAS program.
General form, CATALOG access method to reference a single SOURCE entry:
FILENAME fileref
CATALOG 'libref.catalog.entry-name.entry-type';
%INCLUDE fileref;
Here is an explanation of the syntax:
fileref
is a valid fileref.
libref.catalog.entry-name.entry-type
is a four-level SAS catalog entry name.
entry-type
is SOURCE.

Example

Suppose you have stored the following macro definition as a SOURCE entry in the SAS catalog Sasuser.Mymacs:
%macro prtlast;
   %if &syslast ne _NULL_ %then %do;
      proc print data=&syslast(obs=5);
         title "Listing of &syslast data set";
      run;
   %end
   %else
      %put No data set has been created yet.;
%mend;
You can use the CATALOG access method along with the %INCLUDE statement to compile the macro Prtlast. Then you can reference the macro later in the program.
filename prtlast catalog 'sasuser.mymacs.prtlast.source';
%include prtlast;
proc sort data=sasuser.courses out=bydays;
   by days;
run;
%prtlast
You can also use the CATALOG access method to reference multiple SOURCE entries as long as the entries are stored in the same SAS catalog.
General form, CATALOG access method to reference multiple SOURCE entries:
FILENAME fileref CATALOG 'libref.catalog';
%INCLUDE fileref(entry-1);
%INCLUDE fileref(entry-2);
Here is an explanation of the syntax:
fileref
is a valid fileref.
libref.catalog
is a two-level catalog name.
entry-1 and entry-2
are names of SOURCE entries in library.catalog.

Example

Suppose you have two macros, named Prtlast and Sortlast, that are stored in a SAS catalog.
Catalog Entry: Sasuser.Mymacs.Prtlast.Source
%macro prtlast;
   %if &syslast ne _NULL_ %then %do;
      proc print data=&syslast(obs=5);
      title "Listing of &syslast data set";
      run;
   %end;
   %else
     %put No data set has been created yet.;
%mend;
Catalog Entry: Sasuser.Mymacs.Sortlast.Source
%macro sortlast(sortby);
   %if &syslast ne _NULL_ %then %do;
      proc sort data=&syslast out=sorted;
         by &sortby;
      run;
   %end;
   %else
     %put No data set has been created yet.;
%mend;
You can use the CATALOG access method in conjunction with the %INCLUDE statement to compile both macros. Then you can call the macros later in the program. In this example, assume that the macros have the same names as the SOURCE entries in which they are stored:
filename prtsort catalog 'sasuser.mymacs';
%include prtsort(prtlast) / source2;
%include prtsort(sortlast) / source2;

data current(keep=student_name course_title begin_date location);
   set sasuser.all;
   if year(begin_date)=2001;
   diff=year(today())-year(begin_date);
   begin_date=begin_date+(365*diff);
run;

%sortlast(begin_date)
%prtlast
This code produces the following output:
Listing of WORK.SORTED data set
..................Content has been hidden....................

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