• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / SAS / countw SAS – Count Number of Words in a String

countw SAS – Count Number of Words in a String

January 20, 2022 Leave a Comment

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 &current_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.

Other Articles You'll Also Like:

  • 1.  SAS max() Function – Find Maximum Value Across Columns in Data Step
  • 2.  SAS yymmdd10. Date Format
  • 3.  SAS Remove Formats from Dataset with PROC DATASETS
  • 4.  Round Number to Nearest Integer in SAS
  • 5.  Remove Specific Character from String in SAS
  • 6.  Get Substring from Right in SAS
  • 7.  SAS Less Than or Equal to with LE or <=
  • 8.  PROC Format – Create User-Defined Formats for Variables in SAS
  • 9.  How to Select First 100 Observations of SAS Dataset
  • 10.  SAS round – Rounding Numbers in a SAS Data Step

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy