Previous Page | Next Page

Functions and CALL Routines

SUBSTRN Function



Returns a substring, allowing a result with a length of zero.
Category: Character
Restriction: I18N Level 1
Tip: KSUBSTR in SAS National Language Support (NLS): Reference Guide has the same functionality.

Syntax
Arguments
Details
Length of Returned Variable
The Basics
Using the SUBSTRN Function in a Macro
Comparisons
Examples
Example 1: Manipulating Strings with the SUBSTRN Function
Example 2: Comparison between the SUBSTR and SUBSTRN Functions
See Also

Syntax

SUBSTRN(string, position <, length>)

Arguments

string

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.

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


Length of Returned Variable

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 Basics

The following information applies to the SUBSTRN function:


Using the SUBSTRN Function in a Macro

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:

Comparisons between SUBSTRN and SUBSTR

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
  • 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 nonpositive SUBSTRN returns a result with a length of zero.
the value of length is nonpositive 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


Example 1: Manipulating Strings with the SUBSTRN Function

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            

Example 2: Comparison between the SUBSTR and SUBSTRN Functions

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

Functions:

SUBPAD Function

SUBSTR (left of =) Function

SUBSTR (right of =) Function

Previous Page | Next Page | Top of Page