![]() Chapter Contents |
![]() Previous |
![]() Next |
| %SUBSTR and %QSUBSTR |
| Type: | Macro functions |
| See also: | %NRBQUOTE |
| Syntax | |
| Details | |
| Comparisons | |
| Examples | |
| Example 1: Limiting a Fileref to Eight Characters | |
| Example 2: Storing a Long Macro Variable Value In Segments | |
| Example 3: Comparing Actions of %SUBSTR and %QSUBSTR | |
Syntax |
| %SUBSTR (argument,position<,length>) |
| %QSUBSTR (argument,position<,length>) |
| 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
| Comparisons |
%QSUBSTR masks the same characters as the %NRBQUOTE function.
| Examples |
%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 statement
FILENAME HUMANRES "/dept/humanresource/report96";
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.));
Executing this program writes these lines 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.
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; %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);
Executing these statements writes these lines to the SAS log:
C: &a &b With SUBSTR: one With QSUBSTR: &a
![]() Chapter Contents |
![]() Previous |
![]() Next |
![]() Top of Page |
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.