How to use the SAS Auto Call facility

This post was kindly contributed by SAS Users - go there to comment and to read the full post.

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;

 

How to use the SAS Auto Call facility was published on SAS Users.

This post was kindly contributed by SAS Users - go there to comment and to read the full post.