Table of Contents
The COUNTW function in SAS is used to count the number of words in a given string. It can be used in various scenarios such as data cleaning, data analysis, and data manipulation. This function is particularly useful when dealing with large datasets and allows for efficient and accurate counting of words.
For example, if we have a dataset containing customer reviews of a product, we can use the COUNTW function to count the number of words in each review and analyze the average length of reviews for different products. This can provide valuable insights into customer preferences and help businesses improve their products and services. Additionally, the COUNTW function can also be used in data cleaning to identify and remove any unnecessary or irrelevant words from a string. Overall, the COUNTW function is a powerful tool in SAS that can aid in various data-related tasks.
Use the COUNTW Function in SAS (With Example)
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 printdata=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 printdata=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.
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 printdata=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 .
The following tutorials explain how to perform other common tasks in SAS:
Cite this article
stats writer (2024). How can the COUNTW function be used in SAS, and can you provide an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-countw-function-be-used-in-sas-and-can-you-provide-an-example/
stats writer. "How can the COUNTW function be used in SAS, and can you provide an example?." PSYCHOLOGICAL SCALES, 23 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-the-countw-function-be-used-in-sas-and-can-you-provide-an-example/.
stats writer. "How can the COUNTW function be used in SAS, and can you provide an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-countw-function-be-used-in-sas-and-can-you-provide-an-example/.
stats writer (2024) 'How can the COUNTW function be used in SAS, and can you provide an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-countw-function-be-used-in-sas-and-can-you-provide-an-example/.
[1] stats writer, "How can the COUNTW function be used in SAS, and can you provide an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can the COUNTW function be used in SAS, and can you provide an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
