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:
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 (firstname.lastname@example.org) if you still have questions. Best,
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
Sorry I didn't see this earlier. Oh, I never noticed the error. It should have read "proc print data=&dsn (Obs=&obs); Thanks for pointing this out to me.
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?