Search code examples
sassas-macro

Retrieve macro definition code


Say I have defined a macro function, and for some reason (e.g. a mistake), I deleted the code. I still have the macro though.

How can I retrieve the code I used to define it ?

To define the macro, I just executed:

%macro(param1,param2);
my code
%mend;

Solution

  • If you defined the macro with the source option specified and in a library you can retrieve it with the following:

     %copy MACRO_NAME / source;
    

    Official SAS answer, see original link below:

    There is no way to retrieve the original source code from a stored compiled macro. You must always save the original code in another file so that you can modify it later.

    Starting with SAS® 9.1, there is a new SOURCE option for the %MACRO statement. When used with the existing STORE option, the SOURCE option combines and stores the source of the compiled macro.

    The compiled macro code becomes an entry in a SAS catalog in a permanent SAS data library. The compiled macro and the source code are stored together in the same SASMACR catalog. The SOURCE option requires that the STORE option and the SAS option MSTORED be set. You can use the SAS option SASMSTORE= to identify a permanent SAS data library. You can store a macro or call a stored compiled macro only when the SAS option MSTORED is in effect.

    Note: The source code that is saved by the SOURCE option begins with the %MACRO keyword and ends with a semicolon following the %MEND statement. Now that you have a way to store the source code with the SOURCE option, you also need a way to retrieve this information. The answer is the new %COPY statement, which copies specified items from a SAS macro library.

    For example:

    libname test 'c:\';
    options mstored sasmstore=test;  
    
    %macro test(arg) / store source des="test of the source option";
      %put arg = &arg;
      data one;
        x=1;
        run;
    %mend test;
    
    %copy test / source;
    

    Source: http://support.sas.com/kb/22/352.html