SOURCE
SRC
specifies that the source code of the macro will be copied to the output destination. If
the OUTFILE= option is not specified, the source is written to the SAS log.
option1 <option-2 ...>
must be one or more of the following options:
LIBRARY= libref LIB=
specifies the libref of a SAS library that contains a catalog of stored compiled
SAS macros. If no library is specified, the libref specified by the SASMSTORE=
option is used.
Restriction: This libref cannot be Work.
OUTFILE=fileref | 'external file' OUT=
specifies the output destination of the %COPY statement. The value can be a
fileref or an external file.
Example: Using %COPY Statement
In the following example, the %COPY statement writes the stored source code to the
SAS log:
/* commentary */ %macro foobar(arg) /store source
des="This macro does not do much";
%put arg = &arg;
* this is commentary!!!;
%* this is macro commentary;
%mend /* commentary; */; /* Further commentary */
NOTE: The macro FOOBAR completed compilation without errors.
%copy foobar/source;
The following results are written to the SAS log:
%macro foobar(arg) /store source
des="This macro does not do much";
%put arg = &arg;
* this is commentary!!!;
%* this is macro commentary;
%mend /* commentary; */;
%DISPLAY Statement
Displays a macro window.
Type: Macro statement
Restriction: Allowed in macro definitions or open code
See: “%WINDOW Statement” on page 345
Syntax
%DISPLAY window <.group> <NOINPUT> <BLANK>
<BELL> <DELETE>;
%DISPLAY Statement 307
Required Arguments
window <.group>
names the window and group of fields to be displayed. If the window has more than
one group of fields, give the complete window.group specification. If a window
contains a single unnamed group, specify only window.
NOINPUT
specifies that you cannot input values into fields displayed in the window. If you
omit the NOINPUT option, you can input values into unprotected fields displayed in
the window. Use the NOINPUT option when the %DISPLAY statement is inside a
macro definition and you want to merge more than one group of fields into a single
display. Using NOINPUT in a particular %DISPLAY statement causes the group
displayed to remain visible when later groups are displayed.
BLANK
clears the display in the window. Use the BLANK option to prevent fields from a
previous display from appearing in the current display. This option is useful only
when the %DISPLAY statement is inside a macro definition and when it is part of a
window.group specification. When the %DISPLAY statement is outside a macro
definition, the display in the window is cleared automatically after the execution of
each %DISPLAY statement.
BELL
rings your personal computer's bell, if available, when the window is displayed.
DELETE
deletes the display of the window after processing passes from the %DISPLAY
statement on which the option appears. DELETE is useful only when the
%DISPLAY statement is inside a macro definition.
Details
You can display only one group of fields in each execution of a %DISPLAY statement.
If you display a window containing any unprotected fields, enter values into any
required fields and press Enter to remove the display from the window.
If a window contains only protected fields, pressing Enter removes the display from the
window. While a window is displayed, you can use commands and function keys to view
other windows, change the size of the current window, and so on.
%DO Statement
Begins a %DO group.
Type: Macro statement
Restriction: Allowed in macro definitions only
See: “%END Statement” on page 313
Syntax
%DO;
text and macro language statements
%END;
308 Chapter 19 Macro Statements
Details
The %DO statement designates the beginning of a section of a macro definition that is
treated as a unit until a matching %END statement is encountered. This macro section is
called a %DO group. %DO groups can be nested.
A simple %DO statement often appears in conjunction with %IF-%THEN/%ELSE
statements to designate a section of the macro to be processed depending on whether the
%IF condition is true or false.
Example: Producing One of Two Reports
This macro uses two %DO groups with the %IF-%THEN/%ELSE statement to
conditionally print one of two reports.
%macro reportit(request);
%if %upcase(&request)=STAT %then
%do;
proc means;
title "Summary of All Numeric Variables";
run;
%end;
%else %if %upcase(&request)=PRINTIT %then
%do;
proc print;
title "Listing of Data";
run;
%end;
%else %put Incorrect report type. Please try again.;
title;
%mend reportit;
%reportit(stat)
%reportit(printit)
Specifying stat as a value for the macro variable REQUEST generates the PROC
MEANS step. Specifying
printit generates the PROC PRINT step. Specifying any
other value writes a customized error message to the SAS log.
%DO, Iterative Statement
Executes a section of a macro repetitively based on the value of an index variable.
Type: Macro statement
Restriction: Allowed in macro definitions only
See: “%END Statement” on page 313
Syntax
%DO macro-variable=start %TO stop <%BY increment>;
text and macro language statements
%END;
%DO, Iterative Statement 309
Required Arguments
macro-variable
names a macro variable or a text expression that generates a macro variable name. Its
value functions as an index that determines the number of times the %DO loop
iterates. If the macro variable specified as the index does not exist, the macro
processor creates it in the local symbol table.
You can change the value of the index variable during processing. For example,
using conditional processing to set the value of the index variable beyond the stop
value when a certain condition is met ends processing of the loop.
startstop
specify integers or macro expressions that generate integers to control the number of
times the portion of the macro between the iterative %DO and %END statements is
processed.
The first time the %DO group iterates, macro-variable is equal to start. As
processing continues, the value of macro-variable changes by the value of increment
until the value of macro-variable is outside the range of integers included by start
and stop.
increment
specifies an integer (other than 0) or a macro expression that generates an integer to
be added to the value of the index variable in each iteration of the loop. By default,
increment is 1. Increment is evaluated before the first iteration of the loop. Therefore,
you cannot change it as the loop iterates.
Example: Generating a Series of DATA Steps
This example illustrates using an iterative %DO group in a macro definition.
%macro create(howmany);
%do i=1 %to &howmany;
data month&i;
infile in&i;
input product cost date;
run;
%end;
%mend create;
%create(3)
When you execute the macro CREATE, it generates these statements:
DATA MONTH1;
INFILE IN1;
INPUT PRODUCT COST DATE;
RUN;
DATA MONTH2;
INFILE IN2;
INPUT PRODUCT COST DATE;
RUN;
DATA MONTH3;
INFILE IN3;
INPUT PRODUCT COST DATE;
RUN;
310 Chapter 19 Macro Statements
%DO %UNTIL Statement
Executes a section of a macro repetitively until a condition is true.
Type: Macro statement
Restriction: Allowed in macro definitions only
See: “%END Statement” on page 313
Syntax
%DO %UNTIL(expression);
text and macro language statements
%END;
Required Argument
expression
can be any macro expression that resolves to a logical value. The macro processor
evaluates the expression at the bottom of each iteration. The expression is true if it is
an integer other than zero. The expression is false if it has a value of zero. If the
expression resolves to a null value or a value containing nonnumeric characters, the
macro processor issues an error message.
These examples illustrate expressions for the %DO %UNTIL statement:
%do %until(&hold=no);
%do %until(%index(&source,&excerpt)=0);
Details
The %DO %UNTIL statement checks the value of the condition at the bottom of each
iteration. Thus, a %DO %UNTIL loop always iterates at least once.
Example: Validating a Parameter
This example uses the %DO %UNTIL statement to scan an option list to test the validity
of the parameter TYPE.
%macro grph(type);
%let type=%upcase(&type);
%let options=BLOCK HBAR VBAR;
%let i=0;
%do %until (&type=%scan(&options,&i) or (&i>3)) ;
%let i = %eval(&i+1);
%end;
%if &i>3 %then %do;
%put ERROR: &type type not supported;
%end;
%else %do;
proc chart;&type sex / group=dept;
run;
%end;
%DO %UNTIL Statement 311
..................Content has been hidden....................

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