SUBSTRN Function


SUBSTRN Function

Returns a substring, allowing a result with a length of zero

Category: Character

Syntax

SUBSTRN ( string , position <, length >)

Arguments

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.

Details

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.

Comparisons

The following table lists comparisons between the SUBSTRN and the SUBSTR functions.

Table 4.5: Comparisons between SUBSTRN and SUBSTR

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

  • writes a note to the log stating that the second argument is invalid

  • sets_ERROR_ =1

  • returns the substring that extends from the position that you specified to the end of the string.

the value of length is non-positive

SUBSTRN

returns a result with a length of zero.

the value of length is non-positive

SUBSTR

  • writes a note to the log stating that the third argument is invalid

  • sets_ERROR_ =1

  • returns the substring that extends from the position that you specified to the end of the string.

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

  • writes a note to the log stating that the third argument is invalid

  • sets_ERROR_=1

  • returns the substring that extends from the position that you specified to the end of the string.

Examples

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; 
Output 4.52: Results from the SUBSTRN Function
start example
 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 
end example
 

See Also

Functions:

  • 'SUBPAD Function' on page 836

  • 'SUBSTR (left of =) Function' on page 837

  • 'SUBSTR (right of =) Function' on page 838




SAS 9.1 Language Reference Dictionary, Volumes 1, 2 and 3
SAS 9.1 Language Reference Dictionary, Volumes 1, 2 and 3
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 704

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net