This preview shows page 1. Sign up to view the full content.
Unformatted text preview: s they want mountain or track bikes. You could write three different programs--one for each type--or you could write one program with a macro variable. This program uses a %LET statement to create a macro variable named &BIKECLASS. To run the program, you type in the class of bike you want at the beginning of the program, and then SAS echoes that value throughout the program. %LET bikeclass = Mountain; * Use a macro variable to subset; PROC PRINT DATA = models NOOBS; WHERE Class = "&bikeclass"; FORMAT Price DOLLAR6.; TITLE "Current Models of &bikeclass Bicycles"; RUN; Note that the macro variable &BIKECLASS will be global because it is created outside of a macro in open code. When you submit the program with a value of "Mountain", then the macro processor will resolve the macro variable and create this standard SAS code. * Use a macro variable to subset; PROC PRINT DATA = models NOOBS; WHERE Type = "Mountain"; FORMAT Price DOLLAR6.; TITLE "Current Models of Mountain Bicycles"; RUN; Here are the results: Current Models of Mountain Bicycles Model Nor'easter Santa Ana Scirocco Class Mountain Mountain Mountain Price $899 $459 $2,256 Frame Aluminum Aluminum Titanium This was a short program, with only two occurrences of the macro variable. But imagine if you had a long program with dozens of occurrences of the macro variable sprinkled throughout. You could save a lot of time and trouble by changing the macro variable only once at the beginning. CREATING MODULAR CODE WITH MACROS
Anytime you find yourself repeating the same program statements over and over, you might want to consider creating a macro instead. Macros are simply a group of SAS statements that have a name. And, anytime you want that group of statements in your program, you use the name instead of the re-typing all the statements. The general form of a macro is %MACRO macro-name; macro-text %MEND macro-name; 3 SUGI 29 Tutorials The %MACRO statement tells S...
View Full Document
- Spring '11