You can use the SCAN function in SAS to extract the nth word from a string.
This function uses the following basic syntax:
SCAN(string, count)
where:
- string: The string to analyze
- count: The nth word to extract
Here are the three most common ways to use this function:
Method 1: Extract nth Word from String
data new_data;
set original_data;
second_word = scan(string_variable, 2);
run;
Method 2: Extract Last Word from String
data new_data;
set original_data;
last_word = scan(string_variable, -1);
run;
Method 3: Extract Multiple Words from String
data new_data;
set original_data;
first_word = scan(string_variable, 1);
second_word = scan(string_variable, 2);
third_word = scan(string_variable, 3);
run;
The following examples show how to use each method with the following dataset in SAS:
/*create dataset*/
data original_data;
input name $20. sales;
datalines;
Andy Lincoln Bernard 55
Barren Michael Smith 41
Chad Simpson Arnolds 13
Derrick Parson Henry 29
Eric Miller Johansen 47
Frank Giovanni Goode 61
;
run;
/*view dataset*/
proc print data=original_data;
Example 1: Extract nth Word from String
The following code shows how to extract the second word from each string in the name column:
/*extract second word in each row of name column*/
data new_data;
set original_data;
second_word = scan(name, 2);
run;
/*view results*/
proc print data=new_data;
Notice that the new column called second_word contains the second word from each string in the name column.
Example 2: Extract Last Word from String
The following code shows how to use the value -1 in the scan function to extract the last word from each string in the name column:
/*extract last word in each row of name column*/
data new_data;
set original_data;
last_word = scan(name, -1);
run;
/*view results*/
proc print data=new_data;
Notice that the new column called last_word contains the last word from each string in the name column.
Example 3: Extract Multiple Words from String
The following code shows how to use the scan function to extract every word from each string in the name column:
/*extract each word in each row of name column*/
data new_data;
set original_data;
first_word = scan(name, 1);
second_word = scan(name, 2);
third_word = scan(name, 3);
run;
/*view results*/
proc print data=new_data;
Notice that three new columns have been created that contain the first, second, and third word from each string in the name column.
Additional Resources
The following tutorials explain how to perform other common tasks in SAS:
How to Use the SUBSTR Function in SAS
How to Use the FIND Function in SAS
How to Use the COALESCE Function in SAS