%SUBSTR and %QSUBSTR Functions
Produce a substring of a character string.
Type: Macro function
See: “%NRBQUOTE Function” on page 264
Syntax
%SUBSTR(argument, position<, length>)
%QSUBSTR(argument, position<, length>)
Required Arguments
argument
is a character string or a text expression. If argument might contain a special
character or mnemonic operator, listed below, use %QSUBSTR.
position
is an integer or an expression (text, logical, or arithmetic) that yields an integer,
which specifies the position of the first character in the substring. If position is
greater than the number of characters in the string, %SUBSTR and %QSUBSTR
issue a warning message and return a null value. An automatic call to %EVAL
causes n to be treated as a numeric value.
length
is an optional integer or an expression (text, logical, or arithmetic) that yields an
integer that specifies the number of characters in the substring. If length is greater
than the number of characters following position in argument, %SUBSTR and
%QSUBSTR issue a warning message and return a substring containing the
characters from position to the end of the string. By default, %SUBSTR and
%QSUBSTR produce a string containing the characters from position to the end of
the character string.
Details
The %SUBSTR and %QSUBSTR functions produce a substring of argument, beginning
at position, for length number of characters.
%SUBSTR does not mask special characters or mnemonic operators in its result, even
when the argument was previously masked by a macro quoting function. %QSUBSTR
masks the following special characters and mnemonic operators:
& % ' " ( ) + − * / < > = ¬ ^ ~ ; , # blank
AND OR NOT EQ NE LE LT GE GT IN
Comparisons
%QSUBSTR masks the same characters as the %NRBQUOTE function.
%SUBSTR and %QSUBSTR Functions 275
Examples
Example 1: Limiting a Fileref to Eight Characters
The macro MAKEFREF uses %SUBSTR to assign the first eight characters of a
parameter as a fileref, in case a user assigns one that is longer.
%macro makefref(fileref,file);
%if %length(&fileref) gt 8 %then
%let fileref = %substr(&fileref,1,8);
filename &fileref "&file";
%mend makefref;
%makefref(humanresource,/dept/humanresource/report96)
SAS sees the following statement:
FILENAME HUMANRES "/dept/humanresource/report96";
Example 2: Storing a Long Macro Variable Value in Segments
The macro SEPMSG separates the value of the macro variable MSG into 40-character
units and stores each unit in a separate variable.
%macro sepmsg(msg);
%let i=1;
%let start=1;
%if %length(&msg)>40 %then
%do;
%do %until(%length(&&msg&i)<40);
%let msg&i=%qsubstr(&msg,&start,40);
%put Message &i is: &&msg&i;
%let i=%eval(&i+1);
%let start=%eval(&start+40);
%let msg&i=%qsubstr(&msg,&start);
%end;
%put Message &i is: &&msg&i;
%end;
%else %put No subdivision was needed.;
%mend sepmsg;
%sepmsg(%nrstr(A character operand was found in the %EVAL function
or %IF condition where a numeric operand is required. A character
operand was found in the %EVAL function or %IF condition where a
numeric operand is required.));
When this program executes, these lines are written to the SAS log:
Message 1 is: A character operand was found in the %EV
Message 2 is: AL function or %IF condition where a nu
Message 3 is: meric operand is required. A character
Message 4 is: operand was found in the %EVAL function
Message 5 is: or %IF condition where a numeric operan
Message 6 is: d is required.
Example 3: Comparing Actions of %SUBSTR and %QSUBSTR
Because the value of C is masked by %NRSTR, the value is not resolved at compilation.
%SUBSTR produces a resolved result because it does not mask special characters and
mnemonic operators in C before processing it, even though the value of C had
previously been masked with the %NRSTR function.
%let a=one;
276 Chapter 17 Macro Functions
%let b=two;
%let c=%nrstr(&a &b);
%put C: &c;
%put With SUBSTR: %substr(&c,1,2);
%put With QSUBSTR: %qsubstr(&c,1,2);
When these statements execute, these lines are written to the SAS log:
C: &a &b
With SUBSTR: one
With QSUBSTR: &a
%SUPERQ Function
Masks all special characters and mnemonic operators at macro execution but prevents further resolution of
the value.
Type: Macro quoting function
See: “%NRBQUOTE Function” on page 264 and “%BQUOTE and %NRBQUOTE
Functions” on page 260
Syntax
%SUPERQ(argument)
Required Argument
argument
is the name of a macro variable with no leading ampersand or a text expression that
produces the name of a macro variable with no leading ampersand.
Details
The %SUPERQ function returns the value of a macro variable without attempting to
resolve any macros or macro variable references in the value. %SUPERQ masks the
following special characters and mnemonic operators:
& % ' " ( ) + − * / < > = ¬ ^ ~ ; , # blank
AND OR NOT EQ NE LE LT GE GT IN
%SUPERQ is particularly useful for masking macro variables that might contain an
ampersand or a percent sign when they are used with the %INPUT or %WINDOW
statement, or the SYMPUT routine.
For a description of quoting in SAS macro language, see “Macro Quoting” on page 82.
Note: The maximum level of nesting for the macro quoting functions is 10.
Comparisons
%SUPERQ is the only quoting function that prevents the resolution of macro
variables and macro references in the value of the specified macro variable.
%SUPERQ accepts only the name of a macro variable as its argument, without an
ampersand, and the other quoting functions accept any text expression, including
constant text, as an argument.
%SUPERQ Function 277
%SUPERQ masks the same characters as the %NRBQUOTE function. However,
%SUPERQ does not attempt to resolve anything in the value of a macro variable.
%NRBQUOTE attempts to resolve any macro references or macro variable values in
the argument before masking the result.
Example: Passing Unresolved Macro Variable Values
In this example, %SUPERQ prevents the macro processor from attempting to resolve
macro references in the values of MV1 and MV2 before assigning them to macro
variables TESTMV1 and TESTMV2.
data _null_;
call symput('mv1','Smith&Jones');
call symput('mv2','%macro abc;');
run;
%let testmv1=%superq(mv1);
%let testmv2=%superq(mv2);
%put Macro variable TESTMV1 is &testmv1;
%put Macro variable TESTMV2 is &testmv2;
When this program executes, these lines are written to the SAS log:
Macro variable TESTMV1 is Smith&Jones
Macro variable TESTMV2 is %macro abc;
You might think of the values of TESTMV1 and TESTMV2 as “pictures” of the original
values of MV1 and MV2. The %PUT statement then writes the pictures in its text. The
macro processor does not attempt resolution. It does not issue a warning message for the
unresolved reference &JONES or an error message for beginning a macro definition
inside a %LET statement.
%SYMEXIST Function
Returns an indication of the existence of a macro variable.
Type: Macro function
Syntax
%SYMEXIST(macro-variable-name)
Required Argument
macro-variable-name
is the name of a macro variable or a text expression that yields the name of a macro
variable.
Details
The %SYMEXIST function searches any enclosing local symbol tables and then the
global symbol table for the indicated macro variable and returns one of the following
values:
1 if the macro variable is found
278 Chapter 17 Macro Functions
0 if the macro variable is not found
Example: Using %SYMEXIST Macro Function
The following example uses the %IF %THEN %ELSE macro statement to change the
value of 1 and 0 to TRUE and FALSE respectively:
%global x;
%macro test;
%local y;
%if %symexist(x) %then %put %nrstr(%symexist(x)) = TRUE;
%else %put %nrstr(%symexist(x)) = FALSE;
%if %symexist(y) %then %put %nrstr(%symexist(y)) = TRUE;
%else %put %nrstr(%symexist(y)) = FALSE;
%if %symexist(z) %then %put %nrstr(%symexist(z)) = TRUE;
%else %put %nrstr(%symexist(z)) = FALSE;
%mend test;
%test;
In the previous example, executing the %TEST macro writes the following output to the
SAS log:
%symexist(x) = TRUE
%symexist(y) = TRUE
%symexist(z) = FALSE
%SYMGLOBL Function
Returns an indication as to whether a macro variable is global in scope.
Type: Macro function
Syntax
%SYMGLOBL(macro-variable-name)
Required Argument
macro-variable-name
is a name of a macro variable or a text expression that yields the name of a macro
variable.
Details
The %SYMGLOBL function searches enclosing scopes for the indicated macro variable
and returns a value of 1 if the macro variable is found in the global symbol table,
otherwise it returns a 0. For more information about the global and local symbol tables
and macro variable scopes, see
“Scopes of Macro Variables” on page 49.
%SYMGLOBL Function 279
..................Content has been hidden....................

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