DATA Step, Macro, Functions and more

CountW function

Accepted Solution Solved
Reply
Frequent Contributor
Frequent Contributor
Posts: 134
Accepted Solution

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
Trusted Advisor
Posts: 1,933

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(,) )) ;

View solution in original post


All Replies
Solution
‎07-12-2017 09:22 AM
Trusted Advisor
Posts: 1,933

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(,) )) ;
Frequent Contributor
Frequent Contributor
Posts: 134

Re: CountW function

Posted in reply to PaigeMiller

thank you.

Super User
Posts: 7,866

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
Trusted Advisor
Posts: 1,933

Re: CountW function

[ Edited ]
Posted in reply to KurtBremser

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. ;

Asking out of curiosity:

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

Super User
Posts: 7,866

Re: CountW function

Posted in reply to PaigeMiller

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
Trusted Advisor
Posts: 1,933

Re: CountW function

Posted in reply to KurtBremser

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.

Super User
Super User
Posts: 7,078

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
Frequent Contributor
Posts: 134

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 263 views
  • 2 likes
  • 4 in conversation