SUBSTR (right of =) Function

Extracts a substring from an argument.

Category: Character
Restriction: I18N Level 0 functions are designed for use with Single Byte Character Sets (SBCS) only.
Tip: DBCS equivalent functions are KSUBSTR and KSUBSTRB.

Syntax

Required Arguments

string

specifies a character constant, variable, or expression.

position

specifies a numeric constant, variable, or expression that is the beginning character position.

Optional Arguments

variable

specifies a valid SAS variable name.

length

specifies a numeric constant, variable, or expression that is the length of the substring to extract.

Interaction If length is zero, a negative value, or larger than the length of the expression that remains in string after position, SAS extracts the remainder of the expression. SAS also sets _ERROR_ to 1 and prints a note to the log indicating that the length argument is invalid.
Tip If you omit length, SAS extracts the remainder of the expression.

Details

In a DATA step, if the SUBSTR (right of =) 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 SUBSTR function returns a portion of an expression that you specify in string. The portion begins with the character that you specify by position, and is the number of characters that you specify in length.

Example

The following SAS statements produce these results.
SAS Statement
Result
----+----1----+----2
date='06MAY98';
month=substr(date,3,3);
year=substr(date,6,2);
put @1 month @5 year;
 
MAY 98