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

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

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

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
CP2
Pyrite | Level 9 CP2
Pyrite | Level 9

thank you.

Kurt_Bremser
Super User

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. ;
PaigeMiller
Diamond | Level 26

@Kurt_Bremser 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?

--
Paige Miller
Kurt_Bremser
Super User

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.

PaigeMiller
Diamond | Level 26

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
Tom
Super User Tom
Super User

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.

 

CP2
Pyrite | Level 9 CP2
Pyrite | Level 9
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.

sas-innovate-2024.png

Today is the last day to save with the early bird rate! Register today for just $695 - $100 off the standard rate.

 

Plus, 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
  • 8 replies
  • 12886 views
  • 2 likes
  • 4 in conversation