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. ;
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(,) )) ;
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(,) )) ;
thank you.
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. ;
@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?
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.
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.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: