|SAS Macro Language: Reference|
Macros allow you to substitute text in a program and to do many other things. A SAS program can contain any number of macros, and you can invoke a macro any number of times in a single program.
To help you learn how to define your own macros, this section presents a few examples you can model your own macros after. Each of these examples is fairly simple; by mixing and matching the various techniques, you can create advanced, flexible macros that are capable of performing complex tasks.
Each macro you define has a distinct name, which is subject to the standard SAS naming conventions. (See the base SAS language documentation for more information on SAS naming conventions.) A macro definition is placed between a %MACRO statement and a %MEND (macro end) statement, as follows:
The macro-name specified in the %MEND statement must match the macro-name specified in the %MACRO statement.
Note: While specifying the macro-name in the %MEND statement is not
required, it is recommended. It makes matching %MACRO and %MEND statements
while debugging easier.
Here is a simple macro definition:
%macro dsn; Newdata %mend dsn;
This macro is named DSN.
Newdata is the text of the macro. A string inside a macro is called constant text or model text because it is the model, or
pattern, for the text that becomes part of your SAS program.
To call (or invoke) a macro, precede the name of the macro with a percent sign (%), as follows:
Although the call to the macro looks somewhat like a SAS statement, it does not have to end in a semicolon.
For example, here is how you might call the DSN macro:
title "Display of Data Set %dsn";
The macro processor executes the macro DSN, which substitutes the constant text in the macro into the TITLE statement. Thus, the TITLE statement becomes
title "Display of Data Set Newdata";
Note: The title
is enclosed in double quotation marks. In quoted strings in open code, the
macro processor resolves macro invocations within double quotation marks but
not within single quotation marks.
The macro DSN is exactly the same as coding the following:
%let dsn=Newdata; title "Display of Data Set &dsn";
The result is still
title "Display of Data Set Newdata";
So, in this case, the macro approach does not have any advantages over the macro variable approach. However, DSN is an extremely simple macro. As you will see in later examples in this chapter, macros can do much more than the macro DSN does.
|Inserting Comments in Macros|
All code benefits from thorough commenting, and macro code is no exception. There are two forms you can use to add comments to your macro code.
The first form is the same as comments in SAS code, beginning with
/* and ending with
→. The second form begins with
%* and ends with a
;. Here is a program that uses both
types of comments:
%macro comment; /* Here is the type of comment used in other SAS code. */ %let myvar=abc; %* Here is a macro-type comment.; %let myvar2=xyz; %mend comment;
You can use whichever type comment you prefer in your macro code, or
use both types as in the previous example.
You can create macros that contain entire sections of a SAS program:
%macro plot; proc plot; plot income*age; run; %mend plot;
Later in the program you can invoke the macro as follows:
data temp; set in.permdata; if age>=20; run; %plot proc print; run;
Executing these statements produces the following program:
data temp; set in.permdata; if age>=20; run; proc plot; plot income*age; run; proc print; run;
A macro variable defined in parentheses in a %MACRO statement is a macro parameter. Macro parameters allow you to pass information into a macro. Here is a simple example:
%macro plot(yvar= ,xvar= ); proc plot; plot &yvar*&xvar; run; %mend plot;
You invoke the macro by providing values for the parameters, as follows:
When the macro executes, the macro processor matches the values specified in the macro call to the parameters in the macro definition. (This type of parameter is called a keyword parameter.)
Macro execution produces the following code:
proc plot; plot income*age; run; proc plot; plot income*yrs_educ; run;
Using parameters has several advantages. First, you can write fewer
%LET statements. Second, using parameters ensures that the variables never
interfere with parts of your program outside the macro. Macro parameters are
an example of local macro variables, which exist only during
the execution of the macro in which they are defined.
By using the %IF-%THEN-%ELSE macro statements, you can conditionally generate SAS code with a macro. Here is an example:
%macro whatstep(info=,mydata=); %if &info=print %then %do; proc print data=&mydata; run; %end; %else %if &info=report %then %do; options nodate nonumber ps=18 ls=70 fmtsearch=(sasuser); proc report data=&mydata nowd; column manager dept sales; where sector='se'; format manager $mgrfmt. dept $deptfmt. sales dollar11.2; title 'Sales for the Southeast Sector'; run; %end; %mend whatstep;In this example, the macro WHATSTEP uses keyword parameters, which are set to default null values. When you call a macro that uses keyword parameters, specify the parameter name followed by an equal sign and the value you want to assign the parameter. Here, the macro WHATSTEP is called with INFO set to
This produces the following statements:
proc print data=grocery; run;
Because the macro processor is case sensitive, the previous program
does not work if you specify
For more information on macro definitions and macro parameters, see "%MACRO Statement" and "%MEND Statement" in Chapter 13.
Top of Page
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.