Set up a Library of Compiled SAS Macros

First let me make one thing clear. I am not fund of the SAS Macro Language. Also I am not very good at writing macros in SAS. Though sometimes, I stumble upon a macro so smart that I have to take advantage of its features. So I steal it and save it (who writes code themselves right?).

Though I know very little about SAS macros, I do know that before using them, I have to compile them. Now, I do not want to open the file with the macro definition, compile it and close it again. Instead I like to have a library of already compiled macros. This post shows you how to set up such a library.

Set up Library of Compiled Macros in SAS

The following code example sets up a library of compiled macros. We use a simple Libname Statement to point to the relevant directory. Next, we use the options mstored sasmstore to tell SAS that macro definitions in the defined library above should be compiled.

%let path = /*** Insert Your Path Here ***/
libname mymacs "&path.";
options mstored sasmstore=mymacs;

Now, whenever you write a macro (or copy it from the internet as I do), you only have to compile it once. Just remember to use the STORE option in the macro definition, like this

%macro printdata(dsName) / store;
   proc print data=&dsName.;

In a previous post, I demonstrate How to Use the Autoexec File. The code above is part of my Autoexec file, because I only need to compile a macro once using the STORE Statement. Then I can use the macro whenever I want to, without having to compile it again and again.


There are many many SAS macro out there, all of which exceeds my capabilities in the macro language by landslides. One of my favorites is the Data to Data Step Macro by SASJedi, that I use again and again at the SAS Communities.

You can download the entire code from this post here.