Monday, September 19, 2016

Hack 3.18 Moving Catalog Entries Between SAS Catalogs

SAS Programming Professionals,

Did you know that you can easily copy or move catalog entries between SAS catalogs?

The COPY statement of PROC CATALOG allows you to copy or to move catalog entries between SAS catalogs.  This can come in handy when you are working on creating lots of formats, and they end up in your Work library.  Or, when you receive a format catalog from a client and want to copy the entries to a catalog that is normally allocated in your SAS programs.  Here is an example:

libname cperm "c:\permanent_lib";

proc format;
      value $gender
      "M" = "Male"
      "F" = "Female"
      ;
      value agecat
       low - 12  = "Pre Teen"
       12 - 14   = "Young Teen"
       15 - high = "Older Teen"
       ;
run;

data cperm.class;
set  sashelp.class;

format sex $gender.
       age agecat.;
run;

/* Move catalog entries to permanent catalog*/
proc catalog catalog=work.formats;
      copy out=cperm.Myfmts move;
run;
quit;

/*Verify the catalog entries were moved*/
proc catalog catalog=cperm.Myfmts;
      contents;
run;
quit;

In the example, the LIBNAME statement, FORMAT procedure, and DATA step simply set up our example situation.  We create two format entries ($GENDER and AGECAT) in the default Work library catalog.  Then, we create a permanent SAS data set and assigned those formats to it.  We will be in “trouble” the next time we access the permanent data set, because the $GENDER and AGECAT formats will have evaporated when our SAS program, above, ends.

So, we use PROC CATALOG to move the formats to a permanent format catalog (CPERM.MYFMTS) that is always allocated in our autoexec.sas file when we run our SAS programs.  Note that the MOVE option specifies to move the catalog entries, not to simply copy them.  After that operation, WORK.FORMATS is going to be missing two format entries!

Here is the log from the final PROC CATALOG:

Contents of Catalog CPERM.MYFMTS

#    Name        Type               Create Date          Modified Date    Description
---------------------------------------------------------------------------------
1    AGECAT      FORMAT     13Feb12:13:27:32       13Feb12:13:27:32
2    AGEGROUP    FORMAT     17Sep07:10:55:21       17Sep07:10:55:21
3    GENDER      FORMATC    13Feb12:13:27:32       13Feb12:13:27:32

…showing that all of our hard work paid off!

Best of luck in all your SAS endeavors!

----MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)

Excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers

I plan to post each and every one of the hacks in the book to social media on a weekly basis.  Please pass them along to colleagues who you know would benefit.

No comments:

Post a Comment