Functions and CALL Routines |
Category: | Character |
Restriction: | I18N Level 1 |
Tip: | KSUBSTR in SAS National Language Support (NLS): Reference Guide has the same functionality. |
Syntax |
SUBSTRN(string, position <, length>) |
specifies a character or numeric constant, variable, or expression.
If string is numeric, then it is converted to a character value that uses the BEST32. format. Leading and trailing blanks are removed, and no message is sent to the SAS log.
is an integer that specifies the position of the first character in the substring.
is an integer that specifies the length of the substring. If you do not specify length, the SUBSTRN function returns the substring that extends from the position that you specify to the end of the string.
Details |
In a DATA step, if the SUBSTRN function returns a value to a variable that has not previously been assigned a length, then that variable is given the length of the first argument.
The following information applies to the SUBSTRN function:
The SUBSTRN function returns a string with a length of zero if either position or length has a missing value.
If the position that you specify is non-positive, the result is truncated at the beginning, so that the first character of the result is the first character of the string. The length of the result is reduced accordingly.
If the length that you specify extends beyond the end of the string, the result is truncated at the end, so that the last character of the result is the last character of the string.
If you call SUBSTRN by using the %SYSFUNC macro, then the macro processor resolves the first argument (string) to determine whether the argument is character or numeric. If you do not want the first argument to be evaluated as a macro expression, use one of the macro-quoting functions in the first argument.
Comparisons |
The following table lists comparisons between the SUBSTRN and the SUBSTR functions:
Condition |
Function | Result |
---|---|---|
the value of position is nonpositive | SUBSTRN | returns a result beginning at the first character of the string. |
the value of position is nonpositive | SUBSTR | |
the value of length is nonpositive | SUBSTRN | returns a result with a length of zero. |
the value of length is nonpositive | SUBSTR | |
the substring that you specify extends past the end of the string | SUBSTRN | truncates the result. |
the substring that you specify extends past the end of the string | SUBSTR |
Examples |
The following example shows how to manipulate strings with the SUBSTRN function.
options pageno=1 nodate ls=80 ps=60; data test; retain string "abcd"; drop string; do Position = -1 to 6; do Length = max(-1,-position) to 7-position; Result = substrn(string, position, length); output; end; end; datalines; abcd ; proc print noobs data=test; run;
Output from the SUBSTRN Function
The SAS System 1 Position Length Result -1 1 -1 2 -1 3 a -1 4 ab -1 5 abc -1 6 abcd -1 7 abcd -1 8 abcd 0 0 0 1 0 2 a 0 3 ab 0 4 abc 0 5 abcd 0 6 abcd 0 7 abcd 1 -1 1 0 1 1 a 1 2 ab 1 3 abc 1 4 abcd 1 5 abcd 1 6 abcd 2 -1 2 0 2 1 b 2 2 bc 2 3 bcd 2 4 bcd 2 5 bcd 3 -1 3 0 3 1 c 3 2 cd 3 3 cd 3 4 cd 4 -1 4 0 4 1 d 4 2 d 4 3 d 5 -1 5 0 5 1 5 2 6 -1 6 0 6 1
The following example compares the results of using the SUBSTR function and the SUBSTRN function when the first argument is numeric.
data _null_; substr_result = "*" || substr(1234.5678,2,6) || "*"; put substr_result=; substrn_result = "*" || substrn(1234.5678,2,6) || "*"; put substrn_result=; run;
Results from the SUBSTR and SUBSTRN Functions
substr_result=* 1234* substrn_result=*234.56*
See Also |
|
Copyright © 2011 by SAS Institute Inc., Cary, NC, USA. All rights reserved.