Table of Contents
The SUBSTR function in SAS is a powerful tool that allows users to extract a portion of a string or character variable based on specified starting and ending positions. This function can be used in various scenarios such as data manipulation, text processing, and data cleaning. To use the SUBSTR function, the user must provide the name of the variable, the starting position, and the desired length of the extracted substring. This function can also be combined with other functions to perform more complex tasks. For example, it can be used with the LENGTH function to extract a specific number of characters from the end of a string. Some examples of using the SUBSTR function in SAS include extracting the first name from a full name variable, removing prefixes or suffixes from a text variable, and converting a numerical string to a numeric variable. Overall, the SUBSTR function is a valuable tool for manipulating and extracting data in SAS programming.
Use the SUBSTR Function in SAS (With Examples)
You can use the SUBSTR function in SAS to extract a portion of a string.
This function uses the following basic syntax:
SUBSTR(Source, Position, N)
where:
- Source: The string to analyze
- Position: The starting position to read
- N: The number of characters to read
Here are the four most common ways to use this function:
Method 1: Extract First N Characters from String
data new_data;
set original_data;
first_four = substr(string_variable, 1, 4);
run;
Method 2: Extract Characters in Specific Position Range from String
data new_data;
set original_data;
two_through_five = substr(string_variable, 2, 4);
run;Method 3: Extract Last N Characters from String
data new_data;
set original_data;
last_three = substr(string_variable, length(string_variable)-2, 3);
run;Method 4: Create New Variable if Characters Exist in String
data new_data;
set original_data;
ifsubstr(string_variable, 1, 4) = 'some_string' then new_var = 'Yes';
else new_var = 'No';
run;The following examples show how to use each method with the following dataset in SAS:
/*create dataset*/
data original_data;
input team $1-10;
datalines;
Warriors
Wizards
Rockets
Celtics
Thunder
;
run;
/*view dataset*/
proc printdata=original_data;
Example 1: Extract First N Characters from String
The following code shows how to extract the first 4 characters from the team variable:
/*create new dataset*/
data new_data;
set original_data;
first_four = substr(team, 1, 4);
run;
/*view new dataset*/proc printdata=new_data;

Notice that the first_four variable contains the first four characters of the team variable.
Example 2: Extract Characters in Specific Position Range from String
The following code shows how to extract the characters in positions 2 through 5 from the team variable:
/*create new dataset*/
data new_data;
set original_data;
two_through_five = substr(team, 2, 4);
run;
/*view new dataset*/proc printdata=new_data;

Example 3: Extract Last N Characters from String
The following code shows how to extract the last 3 characters from the team variable:
/*create new dataset*/
data new_data;
set original_data;
last_three = substr(team, length(team)-2, 3);
run;
/*view new dataset*/proc printdata=new_data;
Example 4: Create New Variable if Characters Exist in String
The following code shows how to create a new variable called W_Team that takes a value of ‘yes‘ if the first character in the team name is ‘W’ or a value of ‘no‘ if the first characters is not a ‘W.’
/*create new dataset*/
data new_data;
set original_data;
ifsubstr(team, 1, 1) = 'W' then W_Team = 'Yes';
else W_Team = 'No';
run;
/*view new dataset*/proc printdata=new_data;
Additional Resources
The following tutorials explain how to perform other common tasks in SAS:
Cite this article
stats writer (2024). How can I use the SUBSTR function in SAS?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-the-substr-function-in-sas-can-you-provide-some-examples/
stats writer. "How can I use the SUBSTR function in SAS?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-the-substr-function-in-sas-can-you-provide-some-examples/.
stats writer. "How can I use the SUBSTR function in SAS?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-the-substr-function-in-sas-can-you-provide-some-examples/.
stats writer (2024) 'How can I use the SUBSTR function in SAS?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-the-substr-function-in-sas-can-you-provide-some-examples/.
[1] stats writer, "How can I use the SUBSTR function in SAS?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.
stats writer. How can I use the SUBSTR function in SAS?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
