Generating SAS Code Using Macros
Defining Macros
Macros enable 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
that 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 that you define has a distinct name. When choosing a name for your macro,
it is recommended that you avoid a name that is a SAS language keyword or call routine
name. The name that you choose is subject to the standard SAS naming conventions.
(For more information about SAS naming conventions, see SAS Language Reference:
Concepts.) A macro name cannot contain double-byte character set (DBCS) characters.
A macro definition is placed between a %MACRO statement and a %MEND (macro
end) statement, as in the following example:
%MACRO macro-name;
%MEND macro-name;
The macro-name specified in the %MEND statement must match the macro-name
specified in the %MACRO statement.
Note: Specifying the macro-name in the %MEND statement is not required, but it is
recommended. It makes matching %MACRO and %MEND statements while
debugging easier.
Here is an example of 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 (%):
%macro-name
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:
title "Display of Data Set Newdata";
Generating SAS Code Using Macros 9
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 the following coding:
%let dsn=Newdata;
title "Display of Data Set &dsn";
The following code is the result:
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, 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 that 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 a %* and ends with a ;. The following program
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 that you prefer in your macro code, or use both
types as in the previous example.
The asterisk-style comment ( * commentary ; ) used in SAS code is not recommended
within a macro definition. The asterisk-style will comment constant text appropriately
and will execute any macro statements contained within the comment. This form of
comment is not recommended because unmatched quotation marks contained within the
comment text are not ignored and can cause unpredictable results.
For more information about commenting, see Usage Note 32684: Using comments
within a macro.
Macro Definition Containing Several SAS Statements
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 that you can invoke the macro:
data temp;
10 Chapter 1 Introduction to the Macro Facility
set in.permdata;
if age>=20;
run;
%plot
proc print;
run;
When these statements execute, the following program is produced:
data temp;
set in.permdata;
if age>=20;
run;
proc plot;
plot income*age;
run;
proc print;
run;
Passing Information into a Macro Using Parameters
A macro variable defined in parentheses in a %MACRO statement is a macro
parameter. Macro parameters enable 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:
%plot(yvar=income,xvar=age)
%plot(yvar=income,xvar=yrs_educ)
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.
Generating SAS Code Using Macros 11
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset