## CountW function

Solved
Frequent Contributor
Posts: 139

# CountW function

Does anyone know why the countw function does not work when the words are separated by a comma but will work when separated by another character?

This works:

%let name = type1|type2|type3 ;

%LET word_count = %sysfunc(countw(&name., %str(|) )) ;

%put &word_count. ;

This does not work:

%let name = type1,type2,type3 ;

%LET word_count = %sysfunc(countw(&name., %str(,) )) ;

%put &word_count. ;

Accepted Solutions
Solution
‎07-12-2017 09:22 AM
Posts: 3,261

## Re: CountW function

Since &name has a comma in it, you must use %quote to reference &name, otherwise it thinks the comma are delimiters that are meaningful in the countw function.

```%LET word_count = %sysfunc(countw(%quote(&name), %str(,) )) ;
```
--
Paige Miller

All Replies
Solution
‎07-12-2017 09:22 AM
Posts: 3,261

## Re: CountW function

Since &name has a comma in it, you must use %quote to reference &name, otherwise it thinks the comma are delimiters that are meaningful in the countw function.

```%LET word_count = %sysfunc(countw(%quote(&name), %str(,) )) ;
```
--
Paige Miller
Frequent Contributor
Posts: 139

thank you.

Super User
Posts: 10,555

## Re: CountW function

The comma is the separator of arguments for the countw function, and that leads to a "too many arguments" error.

If possible, do it in a data step:

``````%let name = type1,type2,type3;

data _null_;
call symputx('word_count',put(countw("&name.",','),best.),'g');
run;

%put &word_count. ;``````
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Posts: 3,261

## Re: CountW function

[ Edited ]

KurtBremser wrote:

The comma is the separator of arguments for the countw function, and that leads to a "too many arguments" error.

If possible, do it in a data step:

``````%let name = type1,type2,type3;

data _null_;
call symputx('word_count',put(countw("&name.",','),best.),'g');
run;

%put &word_count. ;``````

What is the benefit of doing this in a data step compared to doing it with the %quote() function as I explained above?

--
Paige Miller
Super User
Posts: 10,555

## Re: CountW function

I just don't have to think about what quoting or de-quoting function to use where. It's just simple data step programming.

See it as an application of the KISS principle.

Of course, if you want to apply the logic in a place where the data step can't be used (eg for immediate macro logic execution in a call execute), then your solution has to be taken.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Posts: 3,261

## Re: CountW function

KISS

It's in the eye of the beholder I guess, I don't see your CALL SYMPUTX solution as being any simpler than the %QUOTE() solution.

--
Paige Miller
Super User
Posts: 8,272

## Re: CountW function

The comma is the delimiter between arguments in SAS function calls. This is why it is generally not a good idea to use comma as the delimiter in a list of values. Another one is that it makes it difficult to use the list in SAS code.  SAS normally wants variable lists to be separated by spaces, not commas.  You would also have trouble passing that list as the value of a macro parameter since comma is used as the delimiter between parameters in macro calls.

You can work around it by quoting the value.

``%let word_count = %sysfunc(countw(%superq(name), %str(,) )) ;``

In this case with the COUNTW() function you could even just use regular quotes

``%let word_count = %sysfunc(countw("&name", %str(,) )) ;``

as adding a leading and trailing double quote character will not change the number of words.  But if later you want to use the %SCAN() function you would need to use macro quoting since you wouldn't want the extra characters in the result.

Frequent Contributor
Posts: 139

## Re: CountW function

Thank you for your responses - all good ideas and most of all, they work! I need the commas because I'm using the string for the by group variables in proc sql which requires comma separators. I suppose I could have counted them before adding the commas in the first place.
☑ This topic is solved.