Search code examples
sassas-macro

SAS Function COUNTW - how to count word including NULL value when using customised delimiter?


Trying to use the function COUNTW to get the number of "words" within a string. But this string may sometimes contain NULL value when it is comma delimited;

I found some custom macro that can do that, just wondering if I can achieve the same with in-built function, COUNTW for example.

For example:

%put %wordcount('abc, , ,def,,56j,type',DLM=%str(,));

The above is a custom function, and it returns 7

%put %sysfunc(countw('abc, , ,def,,56j,type',%str(,)));

COUNTW returns 6 because it does not counting the NULL between 2 commas (",,")


Solution

  • Very simple, just look at the online documentation! COUNTW has optional modifiers, one of which is M which tells the function to include consecutive delimiters as a word, not null.

    The following code returns 7, not 6.

    %put %sysfunc(countw('abc, , ,def,,56j,type',%str(,),M));