Compiled Stored Macros - Updating

From sasCommunity
Jump to: navigation, search

Problem: When a compiled stored macro is updated, the catalog becomes locked until the session closes and cannot be read. Likewise, when a compiled stored macro catalog is read, it cannot be updated until the session closes.

Solution: copy the macro catalogs from/to a work space prior to reading or updating.

For example, to write the macro "test" to a macro catalog, use the following code:

 * Set up the temporary catalog for writing;
 %macro setMacroLib;
 
    * Only define the library once, so that this code can be executed several times per session;
    %if %bquote(%sysfunc(pathname(MACTMP))) = %str() %then 
        libname MACTMP "%sysfunc(pathname(WORK))";;
    options mstored sasmstore=MACTMP;
 
 %mend setMacroLib;
 %setMacroLib;
 
 * Write the macro to the temporary catalog;
 %macro test /store;
    %put This is a test of the emergency broadcast system;
 %mend test;
 
 * Copy the temporary catalog to a permanent location;
 libname MACLIB '/catalogPath';
 proc catalog catalog = MACTMP.SASMACR;
    copy out = MACLIB.SASMACR;
 run;

To read the macro catalog that contains "test," use the following code:

%macro loadMacroLib;
 
    libname MACLIB '/catalogPath';
    * The temporary catalog can only be defined once per session;
    %if %bquote(%sysfunc(pathname(MACTMP))) = %str() %then %do;
 
        * Initialize temporary location of catalog;
        libname MACTMP "%sysfunc(pathname(WORK))";
 
        * Copy permanent catalog;
        proc copy in=MACLIB out=MACS memtype=catalog;
            select sasmacr;
        run;
 
        * Load catalog in temporary location;
        options mstored sasmstore=MACTMP;
 
    %end;
 
%mend loadMacroLib;
%loadMacroLib;
 
%test;

One caveat to the above is that any time a macro catalog is updated, the session that uses the macro needs to be restarted. --Sterling Paramore 21:40, 12 May 2010 (UTC)