BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Hedegaard
Calcite | Level 5

Hello.

I have been trying to make a macro function that counts the number of words in a given string. I use the function countw, but it seems it doesn't work correctly when applied to an empty string. My program is as follows.

%let words = ;

%macro no_of_words;

     %if &words = " " %then %do;

          amount = 0;

     %end;

     %else %do;

          amount = %sysfunc(nwords(&words));

     %end;

%mend;

data how_many;

%no_of_words´;

put amount;

run;

The error message from the log is "The function COUNTW referenced by the %SYSFUNC or %QSYSFUNC macro function has too few arguments."

The program works, if words contains a non-empty string. Any idea as to what my problem is, and how to avoid it?

Kind regards.

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

You have a couple of problems, beside the code you posted not being that which you ran.  Does the following do what you want?

%let words = this is;

%macro no_of_words;

     %if &words = /*" "*/ %then %do;

          amount = 0;

     %end;

     %else %do;

          amount = %sysfunc(countw(&words));

     %end;

%mend;

data how_many;

%no_of_words

put amount;

run;

View solution in original post

5 REPLIES 5
art297
Opal | Level 21

You have a couple of problems, beside the code you posted not being that which you ran.  Does the following do what you want?

%let words = this is;

%macro no_of_words;

     %if &words = /*" "*/ %then %do;

          amount = 0;

     %end;

     %else %do;

          amount = %sysfunc(countw(&words));

     %end;

%mend;

data how_many;

%no_of_words

put amount;

run;

Astounding
PROC Star

Hedegaard,

The fault lies in your first comparison:

%if &words = " " %then %do;

Macro language does not require quotes.  This statement actually compares &WORDS to a set of 3 characters:  a double quote, followed by a space, followed by another double quote.

This replacement would be acceptable:

%if &words = %then %do;

But this would be safer:

%if %length(&words)=0 %then %do;

There are some text strings that could appear within &WORDS that would cause trouble in the %IF comparison.  This is one method to avoid that trouble.

ballardw
Super User

This is the one I use:

%MACRO WORDS(STRING);

%sysfunc(countw(&string,' ,')) /*does this in 9.1 NOTE there is no ; if included it is returned as part of the value*/

%MEND;

Requires SAS 9.1 or later.

Returns 0 for an empty string. You could modify the delimiter list.

The "need" for this existed for me as I had a prior version dating back to SAS 6 from before the %sysfunc statement.

Hedegaard
Calcite | Level 5

Thank you all.

art297: Yes, that is what I meant to write - sorry I got it mixed up.

Astounding: I now use

%if %length(&words)=0 %then %do

instead,

and it seems to work just fine. Smiley Happy

Astounding
PROC Star

Hedegaard,

See if this makes sense to you as a next step.  Your macro would be much more widely applicable if it did NOT generate amount=, but merely generated the number.  For example:

%macro no_of_words (words);

   %if %length(&words)=0 %then 0;

   %else %sysfunc(countw(&words, %str( )));

%mend no_of_words;

That last piece with a blank inside the parentheses -- %str( ) -- is the right way to use only blanks as delimiters.  You would use the macro wherever you want the number to appear.  For example:

data how_many;

amount = %no_of_words(&words);

put amount;

run;

It looks strange the first time you see it, but it's very practical and effective.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 2333 views
  • 8 likes
  • 4 in conversation