SAS String Function : SUBSTR( )

<< Go Back To SAS String Fucntions

SAS SUBSTR( ) is mainly used for extracting a part of string. But more interestingly it has got another important use as well.

When we use it on the Left side of assignment statement it can be used to replace the part of string in main string.

So when used on Right side its extracting part of string and when used on left side it can used for replacing the part of string in original string.

This has been demonstrated below;

1. SUBSTR( ) Used on Right Side on Assignment Statement.

SYNTAX:

SUBSTR (char_string, start_position,no_of_chars_to_read );

Example: SAS SUBSTR( ) Function on Right Side

SAS Code:

data temp;

sample_str = "Pin Code 411014";

all_str = SUBSTR(sample_str,1);

wrong_lngth = SUBSTR(sample_str,5,100);

wrong_strt = SUBSTR(sample_str,100,2);

run;

proc print data = temp;

run;

PROC PRINT OUTPUT :

* Please note this code has compiled on SAS 9.3 Platform.

If we see the code more closely;

The first assignment statement all_str = SUBSTR(sample_str,1);

dont have Third argument; and as the corresponding output we get is all the string.

It means third argument is optional; and if its missing SUSTR() returns all the remaining string starting with given position. (its 1 in this case).

the second assignment statement is the standard demonstration of the SUBSTR () function;

mid_str = SUBSTR(sample_str,5,4);

As we can see from the output; SUBSTR() has returned the part of string; starting at 5th position and counting 4 characters then onwards.

The third assignment statements again demonstrates; what happens if provide the invalid length as third argument;

wrong_lngth = SUBSTR(sample_str,5,100);

and the output as we can see; SUBSTR() returned the all remaining string starting from given position. But its worth to note the note displayed in the log.

NOTE: Invalid third argument to function SUBSTR at line 19 column 15.

Similarly in the Fourth assignment statement; we can see what happens when we give the invalied start position; which is actually out of bounds of the original string.

wrong_strt = SUBSTR(sample_str, 100,2);

And we can observe the output to be blank; in addition to following NOTE being displayed in the log.

NOTE: Invalid second argument to function SUBSTR at line 20 column 14.

2. SUBSTR( ) Used on Left Side on Assignment Statement.

As stated above SUBSTR() can be used on left side of assignment statement. In this case the original string used as first argument of the SUBSTR( ) function will get altered.

This can be made simple to understand with the followng example;

Example: SAS SUBSTR( ) Function on Left Side

SAS Code:

data new;

sample_str = "Pin Code 411014";

SUBSTR(sample_str, 4, 5) = ":";

run;

proc print data = new;

run;

OUTPUT:

Obs

1

sample_str

Pin: 411014

* Please note this code has compiled on SAS 9.3 Platform.

One can closely observe that the sample_str has got altered, with the help of SUSBTR ( ) being used on left side of assignment statement. The part of string used as first argument in the SUBSTR( ) has been replaced by the string present on the right hand side of assignment statement strating at given position (as second argument) and for the given length (as third argument).

<< Go Back To SAS String Fucntions