Monday, March 19, 2018

Hack 7.7 Dynamically Concatenating SAS Libraries




SAS Programming Professionals,

Did you know that you can dynamically insert and append SAS format libraries and macro libraries to your list of available SAS libraries dynamically within a SAS program? 

The INSERT= SAS option allows you to insert a libref as the FIRST in a series of librefs that SAS will search.  The APPEND= SAS option allows you to append a libref as the LAST in a series of librefs that SAS will search.  These two options work to modify the following SAS system options’ settings:

·        CMPLIB=
·        FMTSEARCH=
·        MAPS=
·        SASAUTOS=
·        SASSCRIPT=

Here is an example of using the INSERT= option:

libname templib "c:\temp";

options insert=(fmtsearch=(templib));

… after the code, above, has run, PROC OPTIONS will reveal that the TEMPLIB format library is placed ahead of the other SAS format libraries that SAS searches for a particular format:

FMTSEARCH=(TEMPLIB WORK LIBRARY)

Conversely, in this example of the APPEND= option:

libname templib "c:\temp";

options append=(fmtsearch=(templib));

… after the code, above, has run, PROC OPTIONS will reveal that the TEMPLIB format library is placed after the other SAS format libraries that SAS searches for a particular format:

FMTSEARCH=(WORK LIBRARY TEMPLIB)

Prior to the implementation of the INSERT= and APPEND= options, you either had to modify the –INSERT and –APPEND options in your SAS CONFIG file (and then launch SAS), or you had to specify the search order of _ALL_ libraries via the FMTSEARCH=, SASAUTOS=, etc. options within a program.  Now, you can pre-append or append the various format, macro, etc. libraries for your various projects or users on the fly.  This is a BIG step forward thanks to the brainiacs at the SAS Institute! 

In summary, you can use:

·        INSERT= to pre-append libraries so they will be searched FIRST in the search pattern when specifying CMPLIB=, FMTSEARCH=, MAPS=, SASAUTOS=, and SASSCRIPT= option statements.

·        APPEND= to append libraries so they will be searched LAST in the search pattern when specifying CMPLIB=, FMTSEARCH=, MAPS=, SASAUTOS=, and SASSCRIPT= option statements.

Pretty nifty, eh?

Best of luck in all your SAS endeavors!

---MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)
Author of the new cult classic for computer programmers:  It Only Hurts When I Hit <ENTER>
Print edition:  http://tinyurl.com/z8bzx2e 
Kindle edition: http://tinyurl.com/zypgqa7 

The hack above is an excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers

No comments:

Post a Comment