Returns a substring, allowing a result with a length of zero
Category: Character
SUBSTRN ( string , position <, length >)
string
specifies a character string.
position
is an integer that specifies the position of the first character in the substring.
length
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.
If the SUBSTRN function returns a value to a variable that has not yet been assigned a length, by default the variable length is determined by 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.
The following table lists comparisons between the SUBSTRN and the SUBSTR functions.
If | in the function | the function |
---|---|---|
the value of position is non-positive | SUBSTRN | returns a result beginning at the first character of the string. |
the value of position is non-positive | SUBSTR |
|
the value of length is non-positive | SUBSTRN | returns a result with a length of zero. |
the value of length is non-positive | 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 |
|
The following example shows how the SUBSTRN function works with strings.
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;
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
Functions:
'SUBPAD Function' on page 836
'SUBSTR (left of =) Function' on page 837
'SUBSTR (right of =) Function' on page 838