Search code examples
sassas-macro

SAS: How to save a user-defined macro to a user-defined library


I meet a problem when I use SAS macro to do some jobs.

It seems that the following code

%MACRO macro-name;
    macro-text
%MEND macro-name;

will always save the macro to the work library. I want to save it to another library, for example "mylib" which already exists.

Can anybody help me?


Solution

  • You have two options: compiled & stored, or autocall. Compiled will inherently result in faster code execution (albeit marginal) than autocall, as autocall macros need to be searched for & compiled when called for the first time in a single SAS session.


    Autocall is perhaps the easiest :

    %MACRO MYMACRO ;
      /* do stuff */
    %MEND ;
    

    Save this as mymacro.sas in a given location (e.g. C:\SAS\Macros\)

    Then in your code (or preferably autoexec.sas) :

    filename autos "C:\SAS\Macros" ;
    options append=sasautos=(autos) mrecall mautosource ;
    

    You can then call it as usual

    %MYMACRO ;
    

    http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a002062214.htm


    Compiled & Stored :

    libname maclib "C:\SAS\Macros" ;
    options mstored sasmstore=maclib ;
    
    %MACRO MYMACRO / store source ;
      /* do stuff */
    %MEND ;
    

    Then in your program (or again preferably, autoexec.sas)

    libname maclib "C:\SAS\Macros" ;
    options mstored sasmstore=maclib ;
    

    Call it as usual

    %MYMACRO ;
    

    http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a001328775.htm