To count the number of words in a string, we can use the SAS countw() function. We can use countw() in a data step or with the SAS Macro Language.

data data;
	string = "This is a string for an example";
	x = countw(string);  /* x = 7 */
run;

%let string = This is a string of words.;
%let count= %sysfunc(countw(&string));
%put &count; /* Log shows 7 */

When working with character data, being able to find the number of words in a string can be useful. The countw() function gives us an easy to way to find the number of words in a string.

All we need to do is pass the string variable to countw().

data data;
	string = "This is a string for an example";
	x = countw(string);  /* x = 7 */
run;

How to Change Delimiter in with scan() Function in SAS

When working with data, it is common that you will come across strings delimited in different ways. Sometimes you will want to parse sentences, but other times, you make need to parse a comma delimited variable, or a variable with another delimiter.

To change the delimiter in the countw() function, we just pass an additional parameter to it.

That being said, the SAS countw() function, by default, checks for the following common delimiters:

blank ! $ % & ( ) * + , - . / ; < ^ :

For example, let's say we have a string delimited by commas. To find the number of words in the string, we pass it to countw() like in the following SAS code:

data data;
	string = "This,is,a,string,for,an,example";
	x = countw(string);  /* x = 7 */
run;

Something interesting to note is that if there are other common delimiters in the string, you might get some interesting results.

For example, if we take the same string and replace one of the commas with an exclamation point, we still get the same answer.

data data;
	string = "This,is,a!string,for,an,example";
	x = countw(string);  /* x = 7 */
run;

This may or may not be what you want, and so to be sure, if you want to treat a certain delimiter as the delimiter, you can specify as a second parameter.

data data;
	string = "This,is,a!string,for,an,example";
	x = scan(string,",");  /* x = 6 */
run;

Using the SAS countw() function in the SAS Macro Language

With the SAS Macro Language, we can create complex programs which can be dynamic and effective for getting a lot done.

By default, there is no countw() function in the SAS Macro Language, but we can use the SAS sysfunc() function to use countw(). This will find the count of words in a macro string variable.

%let string = This is a string of words.;
%let count = %sysfunc(countw(&string));
%put &count; /* Log shows 7 */

I find the SAS countw() function to be most useful in loops. For example, if I want to loop over a list, I'll get the length of the list with countw(), scan the list with the SAS scan() function, and then do things depending on where I am in the list.

%let string = This is a string of words.;

%macro scan_example;
%do i = 1 to %sysfunc(countw(&string));
    %let current_word = %scan(&string,&i);
    %if ¤t_word = string %then %do;
        /* Do stuff here */
    %end;
%end;
%mend;

Hopefully this article has been beneficial for you to learn how to use the SAS countw() function in your data steps, as well as in the SAS Macro Language.

Categorized in:

SAS,

Last Update: February 26, 2024