%mend grph;
When you invoke the GRPH macro with a value of HBAR, the macro generates these
statements:
PROC CHART;
HBAR SEX / GROUP=DEPT;
RUN;
If you invoke the GRPH macro with a value of PIE, then the %PUT statement writes this
line to the SAS log:
ERROR: PIE type not supported
%DO %WHILE Statement
Executes a section of a macro repetitively while a condition is true.
Type: Macro statement
Restriction: Allowed in macro definitions only
See: “%END Statement” on page 313
Syntax
%DO %WHILE (expression);
text and macro program statements
%END;
Required Argument
expression
can be any macro expression that resolves to a logical value. The macro processor
evaluates the expression at the top 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 to a value containing nonnumeric characters,
the macro processor issues an error message.
These examples illustrate expressions for the %DO %WHILE statement:
%do %while(&a<&b);
%do %while(%length(&name)>20);
Details
The %DO %WHILE statement tests the condition at the top of the loop. If the condition
is false the first time the macro processor tests it, the %DO %WHILE loop does not
iterate.
Example: Removing Markup Tags from a Title
This example demonstrates using the %DO %WHILE to strip markup (SGML) tags
from text to create a TITLE statement:
312 Chapter 19 Macro Statements
%macro untag(title);
%let stbk=%str(<);
%let etbk=%str(>);
/* Do loop while tags exist */
%do %while (%index(&title,&stbk)>0) ;
%let pretag=;
%let posttag=;
%let pos_et=%index(&title,&etbk);
%let len_ti=%length(&title);
/* Is < first character? */
%if (%qsubstr(&title,1,1)=&stbk) %then %do;
%if (&pos_et ne &len_ti) %then
%let posttag=%qsubstr(&title,&pos_et+1);
%end;
%else %do;
%let pretag=%qsubstr(&title,1,(%index(&title,&stbk)-1));
/* More characters beyond end of tag (>) ? */
%if (&pos_et ne &len_ti) %then
%let posttag=%qsubstr(&title,&pos_et+1);
%end;
/* Build title with text before and after tag */
%let title=&pretag&posttag;
%end;
title "&title";
%mend untag;
You can invoke the macro UNTAG as
%untag(<title>Total <emph>Overdue </emph>Accounts</title>)
The macro then generates this TITLE statement:
TITLE "Total Overdue Accounts";
If the title text contained special characters such as commas, you could invoke it with the
%NRSTR function.
%untag(
%nrstr(<title>Accounts: Baltimore, Chicago, and Los Angeles</title>))
%END Statement
Ends a %DO group.
Type: Macro statement
Restriction: Allowed in macro definitions only
Syntax
%END;
Example: Ending a %DO Group
This macro definition contains a %DO %WHILE loop that ends, as required, with a
%END statement:
%macro test(finish);
%END Statement 313
%let i=1;
%do %while (&i<&finish);
%put the value of i is &i;
%let i=%eval(&i+1);
%end;
%mend test;
%test(5)
Invoking the TEST macro with 5 as the value of finish writes these lines to the SAS log:
The value of i is 1
The value of i is 2
The value of i is 3
The value of i is 4
%GLOBAL Statement
Creates macro variables that are available during the execution of an entire SAS session.
Type: Macro statement
Restriction: Allowed in macro definitions or open code
See: “%LOCAL Statement” on page 323
Syntax
%GLOBAL macro-variable(s);
Or
%GLOBAL / READONLY macro-variable=value;
Required Argument
macro-variable(s)
is the name of one or more macro variables or a text expression that generates one or
more macro variable names. You cannot use a SAS variable list or a macro
expression that generates a SAS variable list in a %GLOBAL statement.
Optional Argument
READONLY macro-variable=value
creates a new read only global macro variable.
Note: The READONLY option can be used to create a single new macro variable
(local or global).
macro-variable
is the name of a macro variable or a text expression that produces a macro
variable name. The name must be a new macro variable name.
value
is a character string or a text expression.
T I P Omitting value produces a null value (0 characters).
314 Chapter 19 Macro Statements
T I P Leading and trailing blanks in the value are ignored. To have leading and
trailing blanks contained in the value, you must enclose the value within the
%STR function.
Details
The %GLOBAL statement creates one or more global macro variables and assigns null
values to the variables. Global macro variables are variables that are available during the
entire execution of the SAS session or job.
A macro variable created with a %GLOBAL statement has a null value until you assign
it some other value. If a global macro variable already exists and you specify that
variable in a %GLOBAL statement, the existing value remains unchanged.
%GLOBAL statements that use the READONLY option create a new global macro
variable and assign the specified value. Existing macro variables cannot be made read-
only. The value of the global macro variable cannot be changed and the variable cannot
be deleted. A macro variable that is declared with the READONLY option cannot be re-
declared in the same scope or any enclosed scope. All read-only macro variables persist
until the scope in which they exist is deleted.
Comparisons
Both the %GLOBAL statement and the %LOCAL statement create macro variables
with a specific scope. However, the %GLOBAL statement creates global macro
variables that exist for the duration of the session or job. The %LOCAL statement
creates local macro variables that exist only during the execution of the macro that
defines the variable.
If you define both a global macro variable and a local macro variable with the same
name, the macro processor uses the value of the local variable during the execution
of the macro that contains the local variable. When the macro that contains the local
variable is not executing, the macro processor uses the value of the global variable.
Example: Creating Global Variables in a Macro Definition
%macro vars(first=1,last=);
%global gfirst glast;
%let gfirst=&first;
%let glast=&last;
var test&first-test&last;
%mend vars;
When you submit the following program, the macro VARS generates the VAR statement
and the values for the macro variables used in the title statement.
proc print;
%vars(last=50)
title "Analysis of Tests &gfirst-&glast";
run;
SAS sees the following:
PROC PRINT;
VAR TEST1-TEST50;
TITLE "Analysis of Tests 1-50";
RUN;
%GLOBAL Statement 315
%GOTO Statement
Branches macro processing to the specified label.
Type: Macro statement
Alias: %GO TO
Restriction: Allowed in macro definitions only
See: “%label Statement” on page 321
Syntax
%GOTO label;
Required Argument
label
is either the name of the label that you want execution to branch to or a text
expression that generates the label. A text expression that generates a label in a
%GOTO statement is called a
computed %GOTO destination.
1
The following examples illustrate how to use label:
%goto findit; /* branch to the label FINDIT */
%goto &home; /* branch to the label that is */
/* the value of the macro variable HOME */
CAUTION:
No percent sign (%) precedes the label name in the %GOTO statement. The
syntax of the %GOTO statement does not include a % in front of the label name.
If you use a %, the macro processor attempts to call a macro by that name to
generate the label.
Details
Branching with the %GOTO statement has two restrictions. First, the label that is the
target of the %GOTO statement must exist in the current macro; you cannot branch to a
label in another macro with a %GOTO statement. Second, a %GOTO statement cannot
cause execution to branch to a point inside an iterative %DO, %DO %UNTIL, or %DO
%WHILE loop that is not currently executing.
Example: Providing Exits in a Large Macro
The %GOTO statement is useful in large macros when you want to provide an exit if an
error occurs.
%macro check(parm);
%local status;
%if &parm= %then %do;
%put ERROR: You must supply a parameter to macro CHECK.;
%goto exit;
1
A computed %GOTO contains % or & and resolves to a label.
316 Chapter 19 Macro Statements
..................Content has been hidden....................

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