To get a substring of a character variable in a SAS data step, you can use the SAS substr() function. The substr() function takes arguments which define the starting position of the substring and how many characters you want to get from the string.

data k;
        var = "string";
        first_two_chars = substr(var, 1, 2); 
        put substr_from_right=;
run;

/* Output */
first_two_chars=st

When working with character variables in SAS, the ability to manipulate and get information from these variables is valuable.

One such piece of information are substrings of a character variable.

The SAS substr() function allows us to easily get substrings from our variables in data steps.

substr() takes in 3 arguments. The first argument is the string you want to extract a substring from. The second argument is the starting position of your substring. For the third argument, you provide the number of characters you want to read.

You can use substr() to extract substrings from strings, and also change the value of strings by changing a specific substring.

Below are some simple examples of extracting substrings from character variables with the SAS substr() variable.

data k;
        var = "this is a string";
        first_two_chars = substr(var, 1, 2); 
        first_five_chars = substr(var, 1, 5); 
        second_five_chars = substr(var, 6, 5); 
        put first_two_chars=;
        put first_five_chars=;
        put second_five_chars=;
run;

/* Output */
first_two_chars=th
first_five_chars=this 
second_five_chars=is a 

Changing Substrings of Character Variables in SAS

With the substr() function, you can also set substrings in strings to different values. If you put substr() on the left side of the equation, you can change the original string.

Below are some examples of how you can change pieces of strings with the SAS substr() function.

data k;
        var = "this is a string";
        substr(var, 1, 4) = "that";
        substr(var, 11, 6) = "puzzle";
        put var=;
run;

/* Output */
var=that is a puzzle

Getting Substrings from the Right of a Character Variable in SAS

You can also use the SAS substr() function to get a substring from the right of a character variable.

If you want to get a substring of a string and start from the end of the string, we can use substr() and use length() to pass different second and third arguments.

For example, if you want to get all characters except the last two characters of a string, you can use the length() function and subtract two from it as shown below.

data k;
        var = "string";
        all_but_last_two = substr(var, 1, length(var) - 2); 
        put substr_from_right=;
run;

/* Output */
all_but_last_two=stri

If you instead want to get just the last two characters from a string, you can start your substring at the length of the variable minus two position and go for two characters as shown below.

data k;
        var = "string";
        last_two_chars = substr(var, length(var) - 2,2); 
        put substr_from_right=;
run;

/* Output */
last_two_chars=ng

Hopefully this article has been useful for you to learn how to use the SAS substr() function to get substrings from character variables in SAS.

Categorized in:

SAS,

Last Update: February 26, 2024