<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic CountW function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/CountW-function/m-p/375297#M89952</link>
    <description>&lt;P&gt;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?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This works:&lt;/P&gt;&lt;P&gt;%let name = type1|type2|type3 ;&lt;/P&gt;&lt;P&gt;%LET word_count = %sysfunc(countw(&amp;amp;name., %str(|) )) ;&lt;/P&gt;&lt;P&gt;%put &amp;amp;word_count. ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This does not work:&lt;/P&gt;&lt;P&gt;%let name = type1,type2,type3 ;&lt;/P&gt;&lt;P&gt;%LET word_count = %sysfunc(countw(&amp;amp;name., %str(,) )) ;&lt;/P&gt;&lt;P&gt;%put &amp;amp;word_count. ;&lt;/P&gt;</description>
    <pubDate>Wed, 12 Jul 2017 13:13:36 GMT</pubDate>
    <dc:creator>CP2</dc:creator>
    <dc:date>2017-07-12T13:13:36Z</dc:date>
    <item>
      <title>CountW function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CountW-function/m-p/375297#M89952</link>
      <description>&lt;P&gt;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?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This works:&lt;/P&gt;&lt;P&gt;%let name = type1|type2|type3 ;&lt;/P&gt;&lt;P&gt;%LET word_count = %sysfunc(countw(&amp;amp;name., %str(|) )) ;&lt;/P&gt;&lt;P&gt;%put &amp;amp;word_count. ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This does not work:&lt;/P&gt;&lt;P&gt;%let name = type1,type2,type3 ;&lt;/P&gt;&lt;P&gt;%LET word_count = %sysfunc(countw(&amp;amp;name., %str(,) )) ;&lt;/P&gt;&lt;P&gt;%put &amp;amp;word_count. ;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2017 13:13:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CountW-function/m-p/375297#M89952</guid>
      <dc:creator>CP2</dc:creator>
      <dc:date>2017-07-12T13:13:36Z</dc:date>
    </item>
    <item>
      <title>Re: CountW function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CountW-function/m-p/375299#M89953</link>
      <description>&lt;P&gt;Since &amp;amp;name has a comma in it, you must use %quote to reference &amp;amp;name, otherwise it thinks the comma are delimiters that are meaningful in the&amp;nbsp;countw function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%LET word_count = %sysfunc(countw(%quote(&amp;amp;name), %str(,) )) ;
&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Jul 2017 13:20:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CountW-function/m-p/375299#M89953</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2017-07-12T13:20:33Z</dc:date>
    </item>
    <item>
      <title>Re: CountW function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CountW-function/m-p/375301#M89954</link>
      <description>&lt;P&gt;thank you.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2017 13:22:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CountW-function/m-p/375301#M89954</guid>
      <dc:creator>CP2</dc:creator>
      <dc:date>2017-07-12T13:22:25Z</dc:date>
    </item>
    <item>
      <title>Re: CountW function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CountW-function/m-p/375302#M89955</link>
      <description>&lt;P&gt;The comma is the separator of arguments for the countw function, and that leads to a "too many arguments" error.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If possible, do it in a data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let name = type1,type2,type3;

data _null_;
call symputx('word_count',put(countw("&amp;amp;name.",','),best.),'g');
run;

%put &amp;amp;word_count. ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Jul 2017 13:25:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CountW-function/m-p/375302#M89955</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-07-12T13:25:03Z</dc:date>
    </item>
    <item>
      <title>Re: CountW function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CountW-function/m-p/375310#M89958</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;The comma is the separator of arguments for the countw function, and that leads to a "too many arguments" error.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If possible, do it in a data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let name = type1,type2,type3;

data _null_;
call symputx('word_count',put(countw("&amp;amp;name.",','),best.),'g');
run;

%put &amp;amp;word_count. ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Asking out of curiosity:&lt;/P&gt;
&lt;P&gt;What is the benefit of doing this in a data step compared to doing it with the %quote() function as I explained above?&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2017 13:37:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CountW-function/m-p/375310#M89958</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2017-07-12T13:37:48Z</dc:date>
    </item>
    <item>
      <title>Re: CountW function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CountW-function/m-p/375314#M89959</link>
      <description>&lt;P&gt;I just don't have to think about what quoting or de-quoting function to use where. It's just simple data step programming.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See it as an application of the KISS principle.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2017 13:43:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CountW-function/m-p/375314#M89959</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-07-12T13:43:29Z</dc:date>
    </item>
    <item>
      <title>Re: CountW function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CountW-function/m-p/375321#M89962</link>
      <description>&lt;P&gt;KISS&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2017 14:14:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CountW-function/m-p/375321#M89962</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2017-07-12T14:14:05Z</dc:date>
    </item>
    <item>
      <title>Re: CountW function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CountW-function/m-p/375329#M89964</link>
      <description>&lt;P&gt;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. &amp;nbsp;SAS normally wants variable lists to be separated by spaces, not commas. &amp;nbsp;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can work around it by quoting the value. &amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let word_count = %sysfunc(countw(%superq(name), %str(,) )) ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In this case with the COUNTW() function you could even just use regular quotes&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let word_count = %sysfunc(countw("&amp;amp;name", %str(,) )) ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;as adding a leading and trailing double quote character will not change the number of words. &amp;nbsp;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2017 14:37:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CountW-function/m-p/375329#M89964</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-07-12T14:37:57Z</dc:date>
    </item>
    <item>
      <title>Re: CountW function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CountW-function/m-p/375517#M90030</link>
      <description>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.&lt;BR /&gt;</description>
      <pubDate>Wed, 12 Jul 2017 21:35:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CountW-function/m-p/375517#M90030</guid>
      <dc:creator>CP2</dc:creator>
      <dc:date>2017-07-12T21:35:39Z</dc:date>
    </item>
  </channel>
</rss>

