Using the Autoexec File at Startup in SAS

When I open up SAS, I have a few things I want to be there. I find myself using the same librefs again and again. Also I use the same macros again and again. I do not want to assign these every time I open a new session. Luckily, I can use the Autoexec file to my advantage (took me way too long to realize this). This post shows you how to localize, set up and use the Autoexec File when you start up a new session, along with a simple example of what autoexec code could look like.

The Autoexec File in SAS

SAS Autoexec File ExampleThe Autoexec file is a file containing executable statements. The statements in the file are executed right away when a new session is initiated. Frequent uses of the Autoexec file are specific system options, setting op libraries, displaying information in the log and so on.

The default SAS Autoexec file is located in the Current Folder. In most cases, the Current Folder is simply the directory containing the Base SAS exe file. If the file is not there, simply open a new program in SAS and save it in the directory with the name “Autoexec.sas”. For obvious reasons, you should keep a copy in a different directory and make the changes there.

Below, I have written a few lines of code. This is a very simple example how what the autoexec code could look like.

/* Point to libraries you frequently use */
%let path=/*** Insert path here ***/
libname MyLib "&path.";
 
/* Set up library of compiled macros */
%let macpath=/*** Insert path to macros here ***/
libname mymacs "&macpath.";
options mstored sasmstore=mymacs;
 
/* Say welcome in the log */
%put ************************************************************************************;
%put *                                                                                  *;
%put *                                                                                  *;
%put       Welcome back &SYSUSERID.! Its &SYSTIME. and time to get SASsy. Have Fun!      ;
%put *                                                                                  *;
%put *                                                                                  *;
%put ************************************************************************************;

First, I simply set up a library that I use frequently. My personal autoexec file contains dozens of libname statements. Next, I set up a library of compiled macros. This is good practice because you do not want to compile a macro each time you use it. Lastly, I write a message to myself in the log to welcome myself back. I use the SYSUSERID macro variable to personalize the message and the SYSTIME macro variable to print what time SAS was initialized.

Obviously there are many many different ways the code can look and it all depends on how you use SAS and what you are working on. Though regardless of your programming habits, I bet that you also have few things you do right away when starting a new session. That is why you should do yourself a favor and let the autoexec file do the work for you. It is worth it.

Summary

Using the autoexec file is a good way of automating some of the routine tasks that you frequently perform when you open SAS. This post is merely an introduction. For a more thorough examination on the topic, consult the SAS Documentation on the Autoexec File. The documentation shows you how to use multiple autoexec files, how to display the executed statements in the log and much more.

You can download the entire code from this post here.