Nine Steps to Get Started using SAS® Macros

77 downloads 123 Views 360KB Size Report
In the nine easy steps outlined in this paper, you follow ... You can write a macro definition to execute code conditionally in ... You still have to look at the report */.
SUGI 28

Beginning Tutorials Paper 56-28

Nine Steps to Get Started using SAS® Macros Jane Stroupe, SAS Institute, Chicago, IL When you run the program on June 1, the following report is generated.

ABSTRACT Have you ever heard your coworkers rave about macros? If so, you've probably wondered what all the fuss was about. This introductory tutorial unravels the mystery of why and how you use the SAS macro facility. In the nine easy steps outlined in this paper, you follow the process of testing your code, substituting macro variables into the code, and turning your SAS program into a macro definition with conditional processing.

Lowest Priced Hotels in the EXPENSES Data Set Obs 1 2 3 4 5 7 10

INTRODUCTION The macro facility is comprised of two components: • macro variables • macro definitions. Simply put, the purpose of both of these is text substitution. Utilizing the macro facility can make your code • easy to maintain • flexible • dynamic. You can write a macro definition to execute code conditionally in nine steps. The macro definition developed in this tutorial prints hotel information from a SAS data set named EXPENSES that is updated with resorts' costs and amenities as they change. You need to run the program often in order to monitor any changes. In this example, if you submit the program in June, July, or August, you want to print a list of the cheapest hotels (those with room rates less than the average room rate for the entire data set.) Otherwise, you want to print a list of all the hotels in the data.

STEP 1: WRITE YOUR PROGRAM AND MAKE SURE IT WORKS. One of the macro tricks is to make sure that your code executes correctly and efficiently before you begin the process of 'macroizing' the program. It is much easier to debug your code when it is not in a macro or does not reference macro variables. So hard code your program and check it out first. proc means data=expenses mean; var RoomRate; run; Analysis Variable : RoomRate Mean -----------221.1090000 ------------

ResortName

RoomRate

Joe's Pretty Good Resort & Bait Shop Larry, Curly, and Motel Sand And Shores Array of Sun Hotel Report Resort Dew Drop Inn Come On Inn

165.89 178.9 215.32 210.78 189.87 197.12 187.98

Food 45.5 64 76 54 49 45 36

. . .

On June 1, 2003

REASONS TO USE THE MACRO FACILITY 1. The macro facility can automatically change this program every time you access the data set or the EXPENSES file updates.

STEP 2: USE MACRO VARIABLES TO FACILITATE TEXT SUBSTITUTION. One technique for making a change to the program is to replace text by utilizing the REPLACE feature of your operating environment; however, there are downsides of that technique. For example, you might accidentally change a word that you do not want changed, or you might have to avoid that problem by using FIND Æ REPLACE repetitively. Furthermore, you must repeat the process every time you want to change the program. Instead of replacing values manually, let SAS macro variables do it for you. Macro variables provide text substitution that you typically use for individual words or phrases, rather than for blocks of code. There are • automatic macro variables such as the date on which you invoked SAS • user-defined macro variables created with the %LET statement • user-defined macro variables created with the CALL SYMPUT routine in a DATA step or with the SQL procedure. Regardless of how you create the macro variables, to reference them in your program, preface the name of the macro variable with an &. options symbolgen;



%let dsn=expenses; ② %let varlist=ResortName RoomRate Food;

After looking at the output report to determine the average value, submit the PRINT procedure step with a WHERE statement. proc print data=expenses; title 'Lowest Priced Hotels in the EXPENSES Data Set'; footnote 'On June 1, 2003'; var ResortName RoomRate Food; where RoomRate