<?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 Applying countw to an empty string in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Applying-countw-to-an-empty-string/m-p/15285#M1999</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have been trying to make a macro function that counts the number of words in a given string. I use the function countw, but it seems it doesn't work correctly when applied to an empty string. My program is as follows. &lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;%let words = ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;&lt;STRONG style="font-size: 10pt; font-family: Courier New;"&gt; &lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;%&lt;SPAN style="color: #ff0000;"&gt;macro&lt;/SPAN&gt; no_of_words;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %if &amp;amp;words = " " %then %do;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; amount = 0;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %else %do;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; amount = %sysfunc(nwords(&amp;amp;words));&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;%mend;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;&lt;SPAN style="color: #ff0000;"&gt;data&lt;/SPAN&gt; how_many;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;%no_of_words´;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;put amount;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000;"&gt;The error message from the log is "The function COUNTW referenced by the %SYSFUNC or %QSYSFUNC macro function has too few arguments."&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000;"&gt;The program works, if words contains a non-empty string. Any idea as to what my problem is, and how to avoid it?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000;"&gt;Kind regards.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 27 Feb 2012 15:56:19 GMT</pubDate>
    <dc:creator>Hedegaard</dc:creator>
    <dc:date>2012-02-27T15:56:19Z</dc:date>
    <item>
      <title>Applying countw to an empty string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Applying-countw-to-an-empty-string/m-p/15285#M1999</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have been trying to make a macro function that counts the number of words in a given string. I use the function countw, but it seems it doesn't work correctly when applied to an empty string. My program is as follows. &lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;%let words = ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;&lt;STRONG style="font-size: 10pt; font-family: Courier New;"&gt; &lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;%&lt;SPAN style="color: #ff0000;"&gt;macro&lt;/SPAN&gt; no_of_words;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %if &amp;amp;words = " " %then %do;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; amount = 0;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %else %do;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; amount = %sysfunc(nwords(&amp;amp;words));&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;%mend;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;&lt;SPAN style="color: #ff0000;"&gt;data&lt;/SPAN&gt; how_many;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;%no_of_words´;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;put amount;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000;"&gt;The error message from the log is "The function COUNTW referenced by the %SYSFUNC or %QSYSFUNC macro function has too few arguments."&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000;"&gt;The program works, if words contains a non-empty string. Any idea as to what my problem is, and how to avoid it?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000;"&gt;Kind regards.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 27 Feb 2012 15:56:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Applying-countw-to-an-empty-string/m-p/15285#M1999</guid>
      <dc:creator>Hedegaard</dc:creator>
      <dc:date>2012-02-27T15:56:19Z</dc:date>
    </item>
    <item>
      <title>Applying countw to an empty string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Applying-countw-to-an-empty-string/m-p/15286#M2000</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You have a couple of problems, beside the code you posted not being that which you ran.&amp;nbsp; Does the following do what you want?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%let words = this is;&lt;/P&gt;&lt;P&gt;%macro no_of_words;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %if &amp;amp;words = /*" "*/ %then %do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; amount = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %else %do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; amount = %sysfunc(countw(&amp;amp;words));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %end;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data how_many;&lt;/P&gt;&lt;P&gt;%no_of_words&lt;/P&gt;&lt;P&gt;put amount;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 27 Feb 2012 16:14:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Applying-countw-to-an-empty-string/m-p/15286#M2000</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2012-02-27T16:14:31Z</dc:date>
    </item>
    <item>
      <title>Applying countw to an empty string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Applying-countw-to-an-empty-string/m-p/15287#M2001</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hedegaard,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The fault lies in your first comparison:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%if &amp;amp;words = " " %then %do;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Macro language does not require quotes.&amp;nbsp; This statement actually compares &amp;amp;WORDS to a set of 3 characters:&amp;nbsp; a double quote, followed by a space, followed by another double quote.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This replacement would be acceptable:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%if &amp;amp;words = %then %do;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But this would be safer:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%if %length(&amp;amp;words)=0 %then %do;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;There are some text strings that could appear within &amp;amp;WORDS that would cause trouble in the %IF comparison.&amp;nbsp; This is one method to avoid that trouble.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 27 Feb 2012 16:14:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Applying-countw-to-an-empty-string/m-p/15287#M2001</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2012-02-27T16:14:38Z</dc:date>
    </item>
    <item>
      <title>Re: Applying countw to an empty string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Applying-countw-to-an-empty-string/m-p/15288#M2002</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This is the one I use:&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #800080; font-size: 10pt; font-family: SAS Monospace;"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 12pt;"&gt;&lt;SPAN lang="EN"&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;%MACRO WORDS(STRING);&lt;/P&gt;&lt;P&gt;%sysfunc(countw(&amp;amp;string,' ,')) /*does this in 9.1 NOTE there is no ; if included it is returned as part of the value*/&lt;/P&gt;&lt;P&gt;%MEND;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Requires SAS 9.1 or later.&lt;/P&gt;&lt;P&gt;Returns 0 for an empty string. You could modify the delimiter list.&lt;/P&gt;&lt;P&gt;The "need" for this existed for me as I had a prior version dating back to SAS 6 from before the %sysfunc statement.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 27 Feb 2012 18:38:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Applying-countw-to-an-empty-string/m-p/15288#M2002</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2012-02-27T18:38:48Z</dc:date>
    </item>
    <item>
      <title>Re: Applying countw to an empty string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Applying-countw-to-an-empty-string/m-p/15289#M2003</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you all.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;art297: Yes, that is what I meant to write - sorry I got it mixed up.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Astounding: I now use&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%if %length(&amp;amp;words)=0 %then %do&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;instead,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;and it seems to work just fine. &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 28 Feb 2012 09:37:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Applying-countw-to-an-empty-string/m-p/15289#M2003</guid>
      <dc:creator>Hedegaard</dc:creator>
      <dc:date>2012-02-28T09:37:22Z</dc:date>
    </item>
    <item>
      <title>Re: Applying countw to an empty string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Applying-countw-to-an-empty-string/m-p/15290#M2004</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hedegaard,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;See if this makes sense to you as a next step.&amp;nbsp; Your macro would be much more widely applicable if it did NOT generate amount=, but merely generated the number.&amp;nbsp; For example:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%macro no_of_words (words);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; %if %length(&amp;amp;words)=0 %then 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; %else %sysfunc(countw(&amp;amp;words, %str( )));&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%mend no_of_words;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;That last piece with a blank inside the parentheses -- %str( ) -- is the right way to use only blanks as delimiters.&amp;nbsp; You would use the macro wherever you want the number to appear.&amp;nbsp; For example:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data how_many;&lt;/P&gt;&lt;P&gt;amount = %no_of_words(&amp;amp;words);&lt;/P&gt;&lt;P&gt;put amount;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It looks strange the first time you see it, but it's very practical and effective.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 28 Feb 2012 14:00:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Applying-countw-to-an-empty-string/m-p/15290#M2004</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2012-02-28T14:00:58Z</dc:date>
    </item>
  </channel>
</rss>

