<?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 Re: Count words among a string with special characters in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-words-among-a-string-with-special-characters/m-p/645087#M192764</link>
    <description>&lt;P&gt;So you have found a bug (feature?) of %SYSFUNC().&amp;nbsp; If you give it gibberish like | for the format argument it does not generate an error message.&lt;/P&gt;
&lt;P&gt;You essentially ran this code:&lt;/P&gt;
&lt;PRE&gt;940   %put %sysfunc(countw(one two|three four|five),|);
5&lt;/PRE&gt;
&lt;P&gt;So you asked %SYSFUNC() to run the COUNTW() function on the string using the normal set of delimiters and format the result using a pipe character as the format definition.&amp;nbsp; It just ignored the pipe character and simply formatted the resulting integer into the normal character string it would use to display the number 5.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to use | as the delimiter than pass it to the COUNTW() function and not to the %SYSFUNC() macro function.&lt;/P&gt;
&lt;PRE&gt;941   %put %sysfunc(countw(one two|three four|five,|));
3&lt;/PRE&gt;
&lt;P&gt;You should probably leave the quoting on your messy string. Otherwise any commas or unbalanced quotes are going to confuse the macro processor.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You cannot use two character strings as the delimiter with the COUNTW() function.&amp;nbsp; But look at the modifiers, especially the M and Q modifiers.&lt;/P&gt;</description>
    <pubDate>Mon, 04 May 2020 19:52:14 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2020-05-04T19:52:14Z</dc:date>
    <item>
      <title>Count words among a string with special characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-words-among-a-string-with-special-characters/m-p/645043#M192743</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I would like to get the number of items separated by a "|" (or another special character(s) like "||", "//" etc...) in a string macro variable. I tried the following code without success :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let sentences=%unquote(%str(Première phrase d'essai|Deuxième phrase d'essai|Troisième phrase d'essai|Quatrième phrase d'essai));

%let ct_sentences=%sysfunc(countw(&amp;amp;sentences.), %str(|) )) ;

%put ===========&amp;gt; &amp;amp;ct_sentences.;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The macro variable &lt;CODE class=" language-sas"&gt;&amp;amp;ct_sentences.&lt;/CODE&gt;&amp;nbsp;is equal to 12. How to fix this code in order to get the real number of 4 sentences ? Would you know any SAS functions that are more appropriates please ? Would you have any frame of code ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance.&lt;BR /&gt;Best regards&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 May 2020 17:08:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-words-among-a-string-with-special-characters/m-p/645043#M192743</guid>
      <dc:creator>Olscream</dc:creator>
      <dc:date>2020-05-04T17:08:55Z</dc:date>
    </item>
    <item>
      <title>Re: Count words among a string with special characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-words-among-a-string-with-special-characters/m-p/645049#M192744</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/270412"&gt;@Olscream&lt;/a&gt;&amp;nbsp; Not sure why you want to macro quote that in my opinion is not necessary.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

%let sentences=Première phrase d'essai|Deuxième phrase d'essai|Troisième phrase d'essai|Quatrième phrase d'essai;

%let ct_sentences=%sysfunc(countw(&amp;amp;sentences, %str(|)) ) ;

%put &amp;amp;=ct_sentences.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;174 %put &amp;amp;=ct_sentences.;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;CT_SENTENCES=4&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Alternatively, a %bquote usage would give you a clean log:&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;183  %let sentences=%bquote(Première phrase d'essai|Deuxième phrase d'essai|Troisième phrase
183! d'essai|Quatrième phrase d'essai);
184
185  %let ct_sentences=%sysfunc(countw(&amp;amp;sentences, %str(|)) ) ;
186
187  %put &amp;amp;=ct_sentences.;
CT_SENTENCES=4
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 May 2020 17:19:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-words-among-a-string-with-special-characters/m-p/645049#M192744</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-04T17:19:36Z</dc:date>
    </item>
    <item>
      <title>Re: Count words among a string with special characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-words-among-a-string-with-special-characters/m-p/645051#M192745</link>
      <description>&lt;PRE&gt;%let ct_sentences=%sysfunc(countw(&amp;amp;sentences.,|)) ;

%put ===========&amp;gt; &amp;amp;ct_sentences.;&lt;/PRE&gt;
&lt;P&gt;you have the %str outside of the Countw function parameters and for a single character like this %str is not needed.&lt;/P&gt;</description>
      <pubDate>Mon, 04 May 2020 17:20:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-words-among-a-string-with-special-characters/m-p/645051#M192745</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-05-04T17:20:53Z</dc:date>
    </item>
    <item>
      <title>Re: Count words among a string with special characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-words-among-a-string-with-special-characters/m-p/645073#M192754</link>
      <description>I would say the macro quoting makes sense, as the only reason the code works without it is there happen to be an even number of single quote marks in the string.</description>
      <pubDate>Mon, 04 May 2020 18:34:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-words-among-a-string-with-special-characters/m-p/645073#M192754</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2020-05-04T18:34:56Z</dc:date>
    </item>
    <item>
      <title>Re: Count words among a string with special characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-words-among-a-string-with-special-characters/m-p/645075#M192756</link>
      <description>&lt;P&gt;That's right. I should have mentioned that at least in this case tokenisation doesn't get impacted. Also, %bquote(') is cleaner than %str(%') in my opinion.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 May 2020 18:43:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-words-among-a-string-with-special-characters/m-p/645075#M192756</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-04T18:43:23Z</dc:date>
    </item>
    <item>
      <title>Re: Count words among a string with special characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-words-among-a-string-with-special-characters/m-p/645087#M192764</link>
      <description>&lt;P&gt;So you have found a bug (feature?) of %SYSFUNC().&amp;nbsp; If you give it gibberish like | for the format argument it does not generate an error message.&lt;/P&gt;
&lt;P&gt;You essentially ran this code:&lt;/P&gt;
&lt;PRE&gt;940   %put %sysfunc(countw(one two|three four|five),|);
5&lt;/PRE&gt;
&lt;P&gt;So you asked %SYSFUNC() to run the COUNTW() function on the string using the normal set of delimiters and format the result using a pipe character as the format definition.&amp;nbsp; It just ignored the pipe character and simply formatted the resulting integer into the normal character string it would use to display the number 5.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to use | as the delimiter than pass it to the COUNTW() function and not to the %SYSFUNC() macro function.&lt;/P&gt;
&lt;PRE&gt;941   %put %sysfunc(countw(one two|three four|five,|));
3&lt;/PRE&gt;
&lt;P&gt;You should probably leave the quoting on your messy string. Otherwise any commas or unbalanced quotes are going to confuse the macro processor.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You cannot use two character strings as the delimiter with the COUNTW() function.&amp;nbsp; But look at the modifiers, especially the M and Q modifiers.&lt;/P&gt;</description>
      <pubDate>Mon, 04 May 2020 19:52:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-words-among-a-string-with-special-characters/m-p/645087#M192764</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-05-04T19:52:14Z</dc:date>
    </item>
  </channel>
</rss>

