handout_sas_09_SAS_Macro - SUGI 29 Tutorials Paper 243-29...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Paper 243-29 SAS Macro Programming for Beginners Susan J. Slaughter, Avocet Solutions, Davis, CA Lora D. Delwiche, Delwiche Consulting, Winters, CA ABSTRACT Macro programming is generally considered an advanced topic. But, while macros certainly can be challenging, it is also true that the basic concepts are not difficult to learn. This paper is designed for people who know the basics of SAS programming, but know nothing about SAS macro programming. We explain how the macro processor works, and how to use macros and macro variables. Using these techniques you can create flexible, reusable code that can save you time and effort. WHY USE MACROS? Because macro code takes longer to write and debug than standard SAS code, you generally won’t use macros in programs that will be run only a few times. But if you find yourself writing similar code over and over again, then macros may make your job easier. Macros can help in several ways. First, with macros you can make one small change in your program and have SAS echo that change throughout your program. Second, macros can allow you to write a piece of code and use it over and over again in the same program or in different programs. Third, you can make your programs data driven, letting SAS decide what to do based on actual data values. THE MACRO PROCESSOR The most important concept to keep in mind whenever you write macro code is that You are writing a program that writes a program. Here is how it works. When you submit a standard SAS program, SAS compiles and then immediately executes it. But when you write macro code, there is an extra step. Before SAS can compile and execute your program, SAS must pass your macro statements to the macro processor which then “resolves” your macros generating standard SAS code. Because you are writing a program that writes a program, this is sometimes called meta-programming. MACROS VS. MACRO VARIABLES As you construct macro programs, you will use two basic building blocks: macros and macro variables. You can tell them apart because the names of macro variables start with an ampersand (&), while the names of macros start with a percent sign (%). 1 A macro variable is like a standard data variable except that it does not belong to a data set and has only a single value which is always character. The value of a macro variable could be a variable name, a numeral, or any text you want substituted in your program. A macro, however, is a larger piece of a program that can contain complex logic including complete DATA and PROC steps, and macro statements such as %IF-%THEN/%ELSE and %DO-%END. Macros often —but not always —contain macro variables. macro statements standard SAS statements macro processor SUGI 29 Tutorials
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon