How to use the SAS Auto Call facility

3

Many SAS programmers use macros. I have seen students in my SAS classes use several methods to activate their macros. One way is to load the macro in the Display manager or editor in SAS OnDemand for Academics and submit it. Another technique is to use the statement %Include macro-name.

The best way to make all of your macros available in every SAS session is to take advantage of the Auto Call facility. There are several steps to make this work. I will show you how this works in SAS running on your local computer or SAS OnDemand for Academics.

Instructions for SAS on your local computer

First, place the macros that you plan to use in a pre-defined place, such as "C:\MySASFiles\MyMacros."

Next, edit your Autoexec.sas file. Remember, all the statements in this file are executed every time you open a SAS session. Add the following statements:

filename MyMacros 'C:\MySASFiles\MyMacros;'

(or wherever you placed your macros)

options mautosource SASAutos=(MyMacros);

(The name in parentheses is the file name above.)

You are done. The next time you open a SAS session on your local machine any of the macros in your macro library are available to use.

Instructions for SAS OnDemand for Academics

Open SAS Studio. In the Navigation Pane, click Server Files and Folders. Create a new folder as shown below. (The new folder icon is circled.)

In this example, I chose Macros as my folder name.

Click the Upload icon (circled in the figure below).

Upload the macros from your local computer to this folder (shown below).

Now it's time to add the appropriate statements to the Autoexec.sas file. Do this by clicking the icon circled in the figure below.

Choose Edit Autoexec File from the drop-down list.

Add the two lines in the box shown in the next figure.

Click SAVE. (You can click RUN first and check the log to see that there are any errors.)

Demonstrating a Macro Call (with the Auto Call facility)

You can now use any of the macros in your macro folder. For example, I have a macro called %Print that will either print an entire data set or the first ā€œnā€ observations and add a title to the listing. The program below was run after opening a new session in SAS OnDemand for Academics.

Here is the result:

If you have never used the Auto Call facility, you now know how to do it. One important final point: you must save your macros using the same name as the macro name. For example, if you have a macro called Print, save it as Print.sas. By doing that, SAS knows where to find and compile the macro that you want to use.

If you are curious about my %Print macro or want to test out the Auto Call facility, here is a listing:

*----------------------------------------------------------------*
| Program Name: Print.sas  in C:\sasdata\macros                  |
| Purpose: Macro that prints out a data set and uses the data    |
|          set name in the title                                 |
| Calling Argument: %PRINT(dsn,obs=)                             |
| Example: %PRINT(test,obs=5)                                    |
| Date: January 13, 1998                                         |
| Updated March 6, 2000 to add obs positional parameter          |
*----------------------------------------------------------------*;
%macro Print(dsn,obs=max);
   proc print data=&dsn (obs=&obs) noobs;
      title "Listing of data set %upcase(&dsn)";
      %if &obs ne max %then title2 "First &obs Observations";;
   run;
%mend print;

 

Share

About Author

Ron Cody

Private Consultant

Dr. Ron Cody was a Professor of Biostatistics at the Rutgers Robert Wood Johnson Medical School in New Jersey for 26 years. During his tenure at the medical school, he taught biostatistics to medical students as well as students in the Rutgers School of Public Health. While on the faculty, he authored or co-authored over a hundred papers in scientific journals. His first book, Applied Statistics and the SAS Programming Language, was first published by Prentice Hall in 1985 and is now in its fifth edition. Since then, he has published over a dozen books on SAS programming and statistical analysis using SAS. His latest book, A Gentle Introduction to Statistics Using SAS Studio was published this year. Ron has presented numerous papers at SAS Global forums, regional conferences, as well as local user groups. He is presently a contract instructor for SAS Institute and continues to write books on SAS and statistical topics.

3 Comments

  1. Hi John.

    The purpose of the blog was mainly to demonstrate how to use your macros without first having to load and run them. There is a data set option called OBS=n where n is the last observation you want to process. So, if you write PROC PRINT DATA=MYDATA(OBS=10); the listing will stop at observation 10. In the macro, the macro variable &Obs is set n the call to the number of observations you want to list. By the way, there is a companion option called FIRSTOBS= that allows you to picking a starting position as well. Remember all data set options are placed in parentheses following the data set name. You can use these options in most procedures. Please contact me directly (ron.cody@gmail.com) if you still have questions. Best,
    Ron

  2. When I posted the question it removed the part that I haven't seen before.
    What is the & amp. We will see if that posts correctly

  3. I am not an expert in SAS Macro but I haven't seen the syntax/option that you are using before. In particular this on the print statement (obs=&obs)
    Could you explain this?

    Thank you
    John Keighley

Leave A Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top