chapter 7: macros in sas macros provide for more flexible programming in sas macros make sas more...

21
Chapter 7: Macros in SAS Chapter 7: Macros in SAS Macros provide for more Macros provide for more flexible programming in SAS flexible programming in SAS Macros make SAS more “object- Macros make SAS more “object- oriented”, like R oriented”, like R 1 © © Fall 2011 Fall 2011 John Grego and the University of South John Grego and the University of South Carolina Carolina

Upload: rosalind-watts

Post on 21-Jan-2016

247 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 7: Macros in SAS  Macros provide for more flexible programming in SAS  Macros make SAS more “object-oriented”, like R 1 © Fall 2011 John Grego

Chapter 7: Macros in SASChapter 7: Macros in SAS

Macros provide for more flexible Macros provide for more flexible programming in SASprogramming in SAS

Macros make SAS more “object-Macros make SAS more “object-oriented”, like Roriented”, like R

11

© © Fall 2011Fall 2011 John Grego and the University of South CarolinaJohn Grego and the University of South Carolina

Page 2: Chapter 7: Macros in SAS  Macros provide for more flexible programming in SAS  Macros make SAS more “object-oriented”, like R 1 © Fall 2011 John Grego

Macros in SAS

In some ways, macros serve similar purposes as functions in R– They can change a piece of a program and

have that change be reflected throughout the program

– They can write repeatable code that can be used in many places efficiently

– They can allow programs to be dependent on data values that may be unknown at the time the program is written

Page 3: Chapter 7: Macros in SAS  Macros provide for more flexible programming in SAS  Macros make SAS more “object-oriented”, like R 1 © Fall 2011 John Grego

Macros in SAS

Macros are initially odd-looking (all Macros are initially odd-looking (all those &’s and %’s) and intimidatingthose &’s and %’s) and intimidating—and can stay that way—and can stay that way

Macro statements consist of special Macro statements consist of special code that SAS interprets and code that SAS interprets and translates to standard SAS code translates to standard SAS code before executing the program (this is before executing the program (this is done internally)done internally)

Page 4: Chapter 7: Macros in SAS  Macros provide for more flexible programming in SAS  Macros make SAS more “object-oriented”, like R 1 © Fall 2011 John Grego

Macros in SAS

MacrosMacros are are subprogramssubprograms that are that are placed in key positions in the main placed in key positions in the main SAS program simply by specifying SAS program simply by specifying the name of the macro in a special the name of the macro in a special wayway

Macro variablesMacro variables are similar to SAS are similar to SAS variables, but do not belong to a data variables, but do not belong to a data set. They are typically substituted set. They are typically substituted into the program in key placesinto the program in key places

Page 5: Chapter 7: Macros in SAS  Macros provide for more flexible programming in SAS  Macros make SAS more “object-oriented”, like R 1 © Fall 2011 John Grego

Macros in SAS

Macros are specified with a % prefix:%macroname;

Macro variables are specified with a & prefix:

&mymacrovar

Page 6: Chapter 7: Macros in SAS  Macros provide for more flexible programming in SAS  Macros make SAS more “object-oriented”, like R 1 © Fall 2011 John Grego

Macros in SAS

If a macro variable is defined in a macro, it is local—it can only be used in that macro

If a macro variable is defined in “open code” (outside any macros), it is global and may be used anywhere

Page 7: Chapter 7: Macros in SAS  Macros provide for more flexible programming in SAS  Macros make SAS more “object-oriented”, like R 1 © Fall 2011 John Grego

Defining Simple Macro Variables

The simplest way to assign a value to a macro variable is using a %LET statement

Double quotes are needed for text, because SAS will not find macros inside single quotes

%LET macrovarname= value;

%LET mypower=3;

%LET powerword=cube;

Y=X**&mypower;

TITLE “Taking the &powerword of each X value” ;

Page 8: Chapter 7: Macros in SAS  Macros provide for more flexible programming in SAS  Macros make SAS more “object-oriented”, like R 1 © Fall 2011 John Grego

Defining Simple Macro Variables

Note that the macro variables are referenced with an ampersand symbol. When SAS sees &macrovarname, it will replace this with the value that was assigned to macrovarname in the &LET statement

Page 9: Chapter 7: Macros in SAS  Macros provide for more flexible programming in SAS  Macros make SAS more “object-oriented”, like R 1 © Fall 2011 John Grego

Defining Simple Macro Variables

This way, if the value needs to be changed, we can change it once (in the %LET statement) rather than everywhere the variable is used

To make a variable global, simply define it (with a %LET statement) outside of a macro

Page 10: Chapter 7: Macros in SAS  Macros provide for more flexible programming in SAS  Macros make SAS more “object-oriented”, like R 1 © Fall 2011 John Grego

Defining Simple Macro Variables

%INCLUDE is another simple macro variable that allows us to insert sections of SAS code into a program; it can improve development and readability of long complex programs

Page 11: Chapter 7: Macros in SAS  Macros provide for more flexible programming in SAS  Macros make SAS more “object-oriented”, like R 1 © Fall 2011 John Grego

Macros as a repeatable set of SAS statements

One important use of macros is packaging a piece of SAS code to be placed in various places in the SAS program.

A macro has the syntax:

%MACRO macroname;

..SAS statements

%MEND macroname;

Page 12: Chapter 7: Macros in SAS  Macros provide for more flexible programming in SAS  Macros make SAS more “object-oriented”, like R 1 © Fall 2011 John Grego

Macros as a repeatable set of SAS statements

This set of SAS statements that lies between the %MACRO and %MEND statements can be placed anywhere in a program by “invoking” the macro (often after the macro has been defined and initial DATA steps and PROC steps have been run:

%macroname

Page 13: Chapter 7: Macros in SAS  Macros provide for more flexible programming in SAS  Macros make SAS more “object-oriented”, like R 1 © Fall 2011 John Grego

Macros as a repeatable set of SAS statements

This serves as a substitute for rewriting all those statements multiple times

Page 14: Chapter 7: Macros in SAS  Macros provide for more flexible programming in SAS  Macros make SAS more “object-oriented”, like R 1 © Fall 2011 John Grego

Macros with parameters

Macros that, when invoked, simply repeat the same statements can be helpful, but not too flexible

Using parameters can add flexibility to macros

Parameters are macro variables whose values are set each time the macro is invoked

Page 15: Chapter 7: Macros in SAS  Macros provide for more flexible programming in SAS  Macros make SAS more “object-oriented”, like R 1 © Fall 2011 John Grego

Macros with parameters

Parameters of macros are similar to arguments of R functions

%MACRO macroname (param1=.., param2=..);

SAS statements..

%MEND macroname;

Page 16: Chapter 7: Macros in SAS  Macros provide for more flexible programming in SAS  Macros make SAS more “object-oriented”, like R 1 © Fall 2011 John Grego

Macros with parameters

When invoking the macro, we also provide the values of the parameters

%macroname(param1=7.5, param2=January)

Within the macro, the parameter name is referred to with an ampersand:

Month=&param2;

Page 17: Chapter 7: Macros in SAS  Macros provide for more flexible programming in SAS  Macros make SAS more “object-oriented”, like R 1 © Fall 2011 John Grego

Conditional Logic in Macros

Macro equivalents of conditions Macro equivalents of conditions statements exist: statements exist: %IF, %THEN, %ELSE, %IF, %THEN, %ELSE, %DO, %END%DO, %END..

These are used in a similar fashion as These are used in a similar fashion as non-macro equivalents, except:non-macro equivalents, except:– They can only be used within a macroThey can only be used within a macro– They can contain entire DATA steps or PROC They can contain entire DATA steps or PROC

steps; this extraordinary flexibility is why steps; this extraordinary flexibility is why we need special notation for these we need special notation for these statementsstatements

Page 18: Chapter 7: Macros in SAS  Macros provide for more flexible programming in SAS  Macros make SAS more “object-oriented”, like R 1 © Fall 2011 John Grego

Conditional Logic in Macros

Automatic macro variables such as&SYSDATE gives current date in character form&SYSDAY gives current day of weekcan be useful (with conditional logic) to run a SAS program that should do different tasks depending on the date or on the day of the week

Page 19: Chapter 7: Macros in SAS  Macros provide for more flexible programming in SAS  Macros make SAS more “object-oriented”, like R 1 © Fall 2011 John Grego

CALL SYMPUT

The CALL SYMPUT statements assigns a value to a macro variable, typically on the basis of data read in a previous DATA stepCALL SYMPUT(“macrovarname”,

value);

Page 20: Chapter 7: Macros in SAS  Macros provide for more flexible programming in SAS  Macros make SAS more “object-oriented”, like R 1 © Fall 2011 John Grego

CALL SYMPUT

1. Read in variables (say, var1, var2, etc.) in a DATA step

2. In a second DATA step, use CALL SYMPUT

Page 21: Chapter 7: Macros in SAS  Macros provide for more flexible programming in SAS  Macros make SAS more “object-oriented”, like R 1 © Fall 2011 John Grego

CALL SYMPUT

3. Invoke the macro variable (&comparison) in a separate step (it cannot be invoked in the same DATA step)

IF var1>var2 THEN CALL SYMPUT(“comparison”,”greater”);

ELSE CALL SYMPUT(“comparison”,”less”);

IF var1>var2 THEN CALL SYMPUT(“comparison”,var3);