You can use the COUNTW function in SAS to count the number of words in a character string.
This function uses the following syntax:
COUNTW(string, <character>, <modifier>)
where:
- string: The string that contains the words to be counted
- character: Optional character constant that initializes a list of characters
- modifier: Optional codes that specify characters or symbols to count as separators between words
The following example shows how to use this function in practice.
Example: How to Use the COUNTW Function in SAS
Suppose we have the following dataset in SAS:
/*create dataset*/
data my_data;
input phrase $char50.;
datalines;
Hey_everyone
What's going on today
Wow, what a great day
Let's have fun
We should play basketball
This weather is so so awesome
;
run;
/*view dataset*/
proc print data=my_data;
The following code shows how to use the COUNTW function to create a new column that shows the word count in each row of the phrase column:
/*create new dataset that shows number of words in each row*/
data new_data;
set my_data;
word_count = countw(phrase);
run;
/*view new dataset*/
proc print data=new_data;
By default, the COUNTW function only considers spaces to be the separators between words.
Thus:
- In the first phrase there are no spaces, so the COUNTW function counts a total of only 1 word.
- In the second phrase there are three spaces, so the COUNTW function counts a total of 4 words.
- In the third phrase there are four spaces, so the COUNTW function counts a total of 5 words.
And so on.
However, we can specify a list of modifiers to also count as separators between words.
For example, we can use the following syntax to specify that a space and an underscore should both be considered separators between words:
/*create new dataset that shows number of words in each row*/
data new_data;
set my_data;
word_count = countw(phrase, ' _');
run;
/*view new dataset*/
proc print data=new_data;
The new word_count column now accurately counts the number of words in the first phrase since we specified that an underscore should also be considered to be a separator between words.
Note: You can find the complete documentation for the SAS COUNTW function here.
Additional Resources
The following tutorials explain how to perform other common tasks in SAS:
How to Extract Numbers from String in SAS
How to Use the SUBSTR Function in SAS
How to Remove Special Characters from Strings in SAS