How do I Use the COUNTW Function in SAS?

The COUNTW function in SAS can be used to count the number of non-missing values in a given dataset. This function is especially useful when analyzing large datasets because it can quickly identify the number of non-missing values in a given group or dataset without having to manually count them. Additionally, the COUNTW function can be used with other SAS functions, such as SUM, MEAN, or MAX, to analyze the values of a given dataset.


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.

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 .

The following tutorials explain how to perform other common tasks in SAS:

x