Friday, March 23, 2012

SAS Catalogs

SAS catalogs are heavily underutilized in my experience. Catalogs are analogous to a Zip file and are used to store formats, macros, SCL, graphics and more in catalog entries based on its type. Because catalogs are part of the SAS/BASE product, it is also a great way to store dynamically generated code then call it later on. Another benefit of using SAS catalogs to store information is that it is portable and does not have dependencies on OS storage devices that can change (e.g. does everyone have a c:\temp\ folder?).

A catalog is fully identified by a four-level name such as libref.catalog.entry-name.entry-type:

libref - the library that will store the catalog (use WORK for throw away entries)
catalog - name of the catalog (think of it as a zip file)
entry-name - name of the catalog entry
entry-type - types include SOURCE, LOG, OUTPUT, FORMAT, MACRO, CATAMS, SCL, GRSEG

The below code uses the disposable WORK libref to dynamically write out and then execute SOURCE code using both the full four-level name and the abbreviated version.

filename fullname catalog "work.catalog.mycode1.source" ;
filename abbrev   catalog "work.catalog" ;

data _null_   ;
  file fullname ;
  put "data _null_ ;"
    / "  put 'Hello from fullname' ;"
    / "run ;" ;
  file abbrev(mycode2.source) ;
  put "data _null_ ;"
    / "  put 'Hello from abbrev' ;"
    / "run ;" ;
run ;

%inc fullname ;
%inc abbrev( mycode2.source ) ;

filename fullname clear ;
filename abbrev clear ;

Reference: Using a SAS Catalog to Develop and Manage a SAS Project, by David D Chapman

No comments:

Post a Comment