%DO macro-variable=expression %TO expression <%BY expression>;
%DO %UNTIL(expression);
%DO %WHILE(expression);
%IF expression %THEN action;
For more information about operands and operators in expressions, see Chapter 6,
“Macro Expressions,” on page 73.
Macro Functions
Using Macro Functions
A macro language function processes one or more arguments and produces a result. You
can use all macro functions in both macro definitions and open code. Macro functions
include character functions, evaluation functions, and quoting functions. The macro
language functions are listed in the following table.
Table 12.3 Macro Functions
Function Description
%BQUOTE,
%NRBQUOTE
Mask special characters and mnemonic operators in a resolved
value at macro execution.
%EVAL Evaluates arithmetic and logical expressions using integer
arithmetic.
%INDEX Returns the position of the first character of a string.
%LENGTH Returns the length of a string.
%QUOTE, %NRQUOTE Mask special characters and mnemonic operators in a resolved
value at macro execution. Unmatched quotation marks (“ ”) and
parentheses ( () ) must be marked with a preceding %.
%SCAN, %QSCAN Search for a word specified by its number. %QSCAN masks
special characters and mnemonic operators in its result.
%STR, %NRSTR Mask special characters and mnemonic operators in constant text
at macro compilation. Unmatched quotation marks (“ ”) and
parentheses ( () ) must be marked with a preceding %.
%SUBSTR, %QSUBSTR Produce a substring of a character string. %QSUBSTR masks
special characters and mnemonic operators in its result.
160 Chapter 12 Macro Language Elements
Function Description
%SUPERQ Masks all special characters and mnemonic operators at macro
execution but prevents resolution of the value.
%SYMEXIST Returns an indication as to whether the named macro variable
exists.
%SYMGLOBL Returns an indication as to whether the named macro variable is
global in scope.
%SYMLOCAL Returns an indication as to whether the named macro variable is
local in scope.
%SYSEVALF Evaluates arithmetic and logical expressions using floating-point
arithmetic.
%SYSFUNC,
%QSYSFUNC
Execute SAS functions or user-written functions. %QSYSFUNC
masks special characters and mnemonic operators in its result.
%SYSGET Returns the value of a specified host environment variable.
%SYSMACEXEC Indicates whether a macro is currently executing.
%SYSMACEXIST Indicates whether there is a macro definition in the
Work.SASMacr catalog.
%SYSMEXECDEPTH Returns the depth of nesting from the point of call.
%SYSMEXECNAME Returns the name of the macro executing at a nesting level.
%SYSPROD Reports whether a SAS software product is licensed at the site.
%UNQUOTE Unmasks all special characters and mnemonic operators for a
value.
%UPCASE, %QUPCASE Convert characters to uppercase. %QUPCASE masks special
characters and mnemonic operators in its result.
Macro Character Functions
Character functions change character strings or provide information about them. The
following table lists the macro character functions.
Table 12.4 Macro Character Functions
Function Description
%INDEX Returns the position of the first character of a string.
%LENGTH Returns the length of a string.
Macro Functions 161
Function Description
%SCAN, %QSCAN Search for a word that is specified by a number. %QSCAN masks
special characters and mnemonic operators in its result.
%SUBSTR, %QSUBSTR Produce a substring of a character string. %QSUBSTR masks
special characters and mnemonic operators in its result.
%UPCASE, %QUPCASE Convert characters to uppercase. %QUPCASE masks special
characters and mnemonic operators in its result.
For macro character functions that have a Q form (for example, %SCAN and
%QSCAN), the two functions work alike except that the function beginning with Q
masks special characters and mnemonic operators in its result. Use the function
beginning with Q when an argument has been previously masked with a macro quoting
function or when you want the result to be masked (for example, when the result might
contain an unmatched quotation mark or parenthesis). For more information, see “Macro
Quoting” on page 82.
Many macro character functions have names corresponding to SAS character functions
and perform similar tasks (such as %SUBSTR and SUBSTR). But, macro functions
operate before the DATA step executes. Consider the following DATA step:
data out.%substr(&sysday,1,3); /* macro function */
set in.weekly (keep=name code sales);
length location $4;
location=substr(code,1,4); /* SAS function */
run;
Running the program on Monday creates the data set name Out.Mon:
data out.MON; /* macro function */
set in.weekly (keep=name code sales);
length location $4;
location=substr(code,1,4); /* SAS function */
run;
Suppose that the IN.WEEKLY variable CODE contains the values cary18593 and
apex19624. The SAS function SUBSTR operates during DATA step execution and
assigns these values to the variable LOCATION: cary and apex.
Macro Evaluation Functions
Evaluation functions evaluate arithmetic and logical expressions. They temporarily
convert the operands in the argument to numeric values. Then, they perform the
operation specified by the operand and convert the result to a character value. The macro
processor uses evaluation functions to do the following:
make character comparisons
evaluate logical (Boolean) expressions
assign numeric properties to a token, such as an integer in the argument of a function
For more information, see Chapter 6, “Macro Expressions,” on page 73. The following
table lists the macro evaluation functions.
162 Chapter 12 Macro Language Elements
Table 12.5 Macro Evaluation Functions
Function Description
%EVAL Evaluates arithmetic and logical expressions using integer
arithmetic.
%SYSEVALF Evaluates arithmetic and logical expressions using floating-point
arithmetic.
%EVAL is called automatically by the macro processor to evaluate expressions in the
arguments to the statements that perform evaluation in the following functions:
%QSCAN(argument, n<, delimiters>)
%QSUBSTR(argument, position<, length>)
%SCAN(argument, n<, delimiters>)
%SUBSTR(argument, position<, length>)
Macro Quoting Functions
Macro quoting functions mask special characters and mnemonic operators so that the
macro processor interprets them as text instead of elements of the macro language.
The following table lists the macro quoting functions, and also describes the special
characters that they mask and when they operate. (Although %QSCAN, %QSUBSTR,
and %QUPCASE mask special characters and mnemonic operations in their results, they
are not considered quoting functions. Their purpose is to process a character value and
not simply to quote a value.) For more information, see “Macro Quoting” on page 82.
Table 12.6 Macro Quoting Functions
Function Description
%BQUOTE,
%NRBQUOTE
Mask special characters and mnemonic operators in a resolved
value at macro execution. %BQUOTE and %NRBQUOTE are
the most powerful functions for masking values at execution time
because they do not require that unmatched quotation marks (“ ”)
and parentheses ( () ) be marked.
%QUOTE, %NRQUOTE Mask special characters and mnemonic operators in a resolved
value at macro execution. Unmatched quotation marks (“ ”) and
parentheses ( () ) must be marked with a preceding %.
%STR, %NRSTR Mask special characters and mnemonic operators in constant text
at macro compilation. Unmatched quotation marks (“ ”) and
parentheses ( () ) must be marked with a preceding %.
Macro Functions 163
Function Description
%SUPERQ Masks all special characters and mnemonic operators at macro
execution but prevents resolution of the value.
%UNQUOTE Unmasks all special characters and mnemonic operators for a
value.
Compilation Quoting Functions
%STR and %NRSTR mask special characters and mnemonic operators in values during
compilation of a macro definition or a macro language statement in open code. For
example, the %STR function prevents the following %LET statement from ending
prematurely. It keeps the semicolon in the PROC PRINT statement from being
interpreted as the semicolon for the %LET statement.
%let printit=%str(proc print; run;);
Execution of Macro Quoting Functions
%BQUOTE, %NRBQUOTE, %QUOTE, %NRQUOTE, and %SUPERQ mask special
characters and mnemonic operators in values during execution of a macro or a macro
language statement in open code. Except for %SUPERQ, these functions instruct the
macro processor to resolve a macro expression as far as possible and mask the result.
The other quoting functions issue warning messages for any macro variable references
or macro invocations that they cannot resolve. %SUPERQ protects the value of a macro
variable from any attempt at further resolution.
Of the quoting functions that resolve values during execution, %BQUOTE and
%NRBQUOTE are the most flexible. For example, the %BQUOTE function prevents
the following %IF statement from producing an error if the macro variable STATE
resolves to OR (for Oregon). Without %BQUOTE, the macro processor would interpret
the abbreviation for Oregon as the logical operator OR.
%if %bquote(&state)=nc %then %put North Carolina Dept. of
Revenue;
%SUPERQ fetches the value of a macro variable from the macro symbol table and
masks it immediately, preventing the macro processor from attempting to resolve any
part of the resolved value. For example, %SUPERQ prevents the following %LET
statement from producing an error when it resolves to a value with an ampersand, like
Smith&Jones. Without %SUPERQ, the macro processor would attempt to resolve
&Jones.
%let testvar=%superq(corpname);
/* No ampersand in argument to %superq. */
(%SUPERQ takes as its argument either a macro variable name without an ampersand or
a text expression that yields a macro variable name.)
Quotation Marks and Parentheses without a Match
Syntax errors result if the arguments of %STR, %NRSTR, %QUOTE, and %NRQUOTE
contain a quotation mark or parenthesis that does not have a match. To prevent these
164 Chapter 12 Macro Language Elements
..................Content has been hidden....................

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