Compile and store macros

From sasCommunity
Jump to: navigation, search

Q: How do I compile and store a macro?

A:

  • write macro
  • add both options store and source
  • save file
  • write program: compile and store macros
  • list macros in catalog
  • echo using the SAS-supplied macro Copy
  • echo statements in macro with options mfile and mprint

keywords: compiling and storing macros, stored macros, compiled macros

Example Macro

*name: DoThis.sas;
%Macro DoThis(data=)
/ des = 'description of macro'
  store source
  ;
*SAS statements here
run;
%Mend DoThis;

Program: Compile-Store-Macros

Libname             SiteMlib '<directory-specification>';
options SASmStore = SiteMLib
        Mstored;
%Include 'DoThis.sas';

Proc Catalog

Libname Library '<directory-specification>';
        *contains sasmacr.sas7bcat;
 
PROC Catalog catalog = Library.sasmacr;
             contents;
             run;
             quit;

echo using the SAS-supplied macro Copy

*note macro must have been saved with the option source;
 
%COPY DoThis/SOURCE;

Program: Echo statements in macro

Note: this example shows echo of a stored and compiled macro; this will work for non-compiled macros as well.

Libname             SiteMlib '<directory-specification>';
options SASmStore = SiteMLib
        Mstored;
 
filename       Mprint 'macro-statements.txt';
options  mfile Mprint;
 
%DoThis(data=sashelp.class);
 
DATA _Null_;
infile Mprint truncover;
input @1 Line $char72.;
putlog Line;
run;

List Processing: dump to log

Quick and Dirty, very dirty: you will get all kinds of errors about macro variables not assigned, etc.

YeahBut, you have the statements in the macros!

libname Library 'C:\Temp\sasmacr-cat';
options SASmStore = Library
        Mstored;
 
PROC Catalog catalog = Library.sasmacr;
             contents
                 out = Work.List;
             run;
             quit;
 
proc print data = &SysLast.;
PROC SQL; describe table Work.List;
          quit;
*endSAS;
 
DATA _Null_;
do until(EndoFile);
   set Work.List end = EndoFile;
   call execute(catt('%nrstr(%',Name,');'));
   end;
stop;
run;

References

--macro maven == the radical programmer 19:51, 29 October 2009 (UTC)