<?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: How to count number of times a word appears in a series of string variables in each row? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-times-a-word-appears-in-a-series-of/m-p/889274#M351331</link>
    <description>Thanks for pointing it out. It was a typo from my end. The word is specifically "Daughter:" with the ":" and no other variation of it.</description>
    <pubDate>Tue, 15 Aug 2023 02:52:47 GMT</pubDate>
    <dc:creator>Adubhai</dc:creator>
    <dc:date>2023-08-15T02:52:47Z</dc:date>
    <item>
      <title>How to count number of times a word appears in a series of string variables in each row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-times-a-word-appears-in-a-series-of/m-p/889264#M351322</link>
      <description>&lt;P&gt;I have a dataset with 43 string variables named VAR1 till VAR43. There are various string phrases within each value of those variables. I want a new column which counts the number of times the word "Daugther" is written within the 43 variables for each row. Can someone help me with it?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2023 00:45:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-times-a-word-appears-in-a-series-of/m-p/889264#M351322</guid>
      <dc:creator>Adubhai</dc:creator>
      <dc:date>2023-08-15T00:45:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of times a word appears in a series of string variables in each row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-times-a-word-appears-in-a-series-of/m-p/889265#M351323</link>
      <description>&lt;P&gt;One way could be to just concatenate all these variables using catx() and then use countw(). This should work as long as the sum of your strings won't exceed 32767 characters.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What's the length of your variables var1 to var43?&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2023 01:18:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-times-a-word-appears-in-a-series-of/m-p/889265#M351323</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-08-15T01:18:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of times a word appears in a series of string variables in each row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-times-a-word-appears-in-a-series-of/m-p/889267#M351325</link>
      <description>Hi Patrick,&lt;BR /&gt;&lt;BR /&gt;Thanks for your reply. The variables have random and multiple character lengths.&lt;BR /&gt;</description>
      <pubDate>Tue, 15 Aug 2023 01:31:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-times-a-word-appears-in-a-series-of/m-p/889267#M351325</guid>
      <dc:creator>Adubhai</dc:creator>
      <dc:date>2023-08-15T01:31:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of times a word appears in a series of string variables in each row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-times-a-word-appears-in-a-series-of/m-p/889270#M351328</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/300936"&gt;@Adubhai&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hi Patrick,&lt;BR /&gt;&lt;BR /&gt;Thanks for your reply. The variables have random and multiple character lengths.&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So if the longest of the 43 variables is no more than floor(32767/43) in length, you don't need to worry that concatenating the random-length variables will result in a string too long to be captured in a single character variable.&amp;nbsp; &amp;nbsp; In that case&amp;nbsp;the suggestion by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&amp;nbsp;will achieve your goal.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2023 02:33:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-times-a-word-appears-in-a-series-of/m-p/889270#M351328</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-08-15T02:33:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of times a word appears in a series of string variables in each row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-times-a-word-appears-in-a-series-of/m-p/889272#M351329</link>
      <description>&lt;P&gt;Ok so assuming none of the variables are no more than (32767/43) in length and I am able to concatenate the variables. Then how do I count the number of times the word "Daughter" is written in the new concatenated variable?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2023 02:48:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-times-a-word-appears-in-a-series-of/m-p/889272#M351329</guid>
      <dc:creator>Adubhai</dc:creator>
      <dc:date>2023-08-15T02:48:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of times a word appears in a series of string variables in each row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-times-a-word-appears-in-a-series-of/m-p/889273#M351330</link>
      <description>&lt;P&gt;Some incomplete elements in your description. You say count the word "Daugther" (or more likely for English speakers "Daughter").&lt;/P&gt;
&lt;P&gt;Does case matter? Does "daugther", ""DAUGTHER", "dAughTER" or similar differences in capitalization count?&lt;/P&gt;
&lt;P&gt;How about presence in compound words (assuming correct spelling now) such as "Daughter-in-law" or "daughtering"?&lt;/P&gt;
&lt;P&gt;Plurals "Daughters"?&lt;/P&gt;
&lt;P&gt;Plurals with different case?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Provide example data showing the differences I have described above and the expected resulting count for the examples.&lt;/P&gt;
&lt;P&gt;Should only need 4 or 5 variables though instead of all 43. Provide the data as a working data step.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2023 02:50:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-times-a-word-appears-in-a-series-of/m-p/889273#M351330</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-08-15T02:50:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of times a word appears in a series of string variables in each row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-times-a-word-appears-in-a-series-of/m-p/889274#M351331</link>
      <description>Thanks for pointing it out. It was a typo from my end. The word is specifically "Daughter:" with the ":" and no other variation of it.</description>
      <pubDate>Tue, 15 Aug 2023 02:52:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-times-a-word-appears-in-a-series-of/m-p/889274#M351331</guid>
      <dc:creator>Adubhai</dc:creator>
      <dc:date>2023-08-15T02:52:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of times a word appears in a series of string variables in each row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-times-a-word-appears-in-a-series-of/m-p/889292#M351341</link>
      <description>&lt;P&gt;The clarification, especially including the : is very helpful.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Brute force go through each variable and each word. Increment a counter each time the desired word is encountered.&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   array w(*) var1-var43;
   do i=1 to dim(w);
      if not missing(w[i]) then do j=1 to countw(w[i]);
         if scan(w[i],j)='Daughter:' then count=sum(count,1);
      end;
   end;
run;&lt;/PRE&gt;
&lt;P&gt;This will count if the desired word occurs more than once in a single variable, which I have not seen ruled out as a possibility.&lt;/P&gt;
&lt;P&gt;If you have other than default delimiters between words you may have to add them to the SCAN function call.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;DIV class="xis-refDictEntry"&gt;
&lt;DIV class="xis-details"&gt;
&lt;DIV id="n00kvf1f25pei3n16sfnb6yr6nrj" class="xis-subTopic"&gt;
&lt;DIV id="n0ixq9hp8tq11ln1he8d7mojod1n" class="xis-topicContent"&gt;
&lt;DIV id="p0hi4ck62zsyw4n1b1o7g0aydgsh" class="xis-paragraph"&gt;
&lt;DIV class="xis-listUnordered"&gt;
&lt;UL&gt;
&lt;LI&gt;
&lt;DIV id="n0i77e70ukqxmtn11mg730ospe1o" class="xis-item"&gt;
&lt;DIV id="n1ajlezj0cfsg4n1roly22oujbxc" class="xis-paraSimpleFirst"&gt;If your computer uses ASCII characters, the default delimiters are as follows:&lt;/DIV&gt;
&lt;DIV id="n1u3as1m3yp0w2n1dbzznex5n88c" class="xis-paraSimple"&gt;blank ! $ % &amp;amp; ( ) * + , - . / ; &amp;lt; ^ |&lt;/DIV&gt;
&lt;DIV id="p0cxc8lzn89wszn1t9h4dhmtdc33" class="xis-paraSimple"&gt;In ASCII environments that do not contain the ^ character, the SCAN function uses the ~ character instead.&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV id="p1y1fg9pi1h8zfn10le0wfbhhhfd" class="xis-item"&gt;
&lt;DIV id="n0ijv36a49y987n1a3v5wve33z4g" class="xis-paraSimpleFirst"&gt;If your computer uses EBCDIC characters, then the default delimiters are as follows:&lt;/DIV&gt;
&lt;DIV id="p1lpy2762l9ed2n177qyk1ybb208" class="xis-paraSimple"&gt;blank ! $ % &amp;amp; ( ) * + , - . / ; &amp;lt; ¬ | ¢&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So if running in an ASCII environment and you need to find the word delimited with @ characters for example: @Dauthter; then scan wouldn't treat the @ as a delimiter unless you use&lt;/P&gt;
&lt;PRE&gt;if scan(w[i],j,' !$%&amp;amp;()*+,-./;&amp;lt;^|@') ='Daughter:'&lt;/PRE&gt;
&lt;P&gt;to add the @ to the existing list.&lt;/P&gt;
&lt;P&gt;Caution: if you place that string inside double quotes instead of single as shown you will likely some macro processor errors because the % and &amp;amp; characters will be treated as part of the macro language when inside double quotes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you string values are UTF-8 and have unicode characters present then you may have other issues if special characters are adjacent to your word.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2023 06:26:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-times-a-word-appears-in-a-series-of/m-p/889292#M351341</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-08-15T06:26:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of times a word appears in a series of string variables in each row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-times-a-word-appears-in-a-series-of/m-p/889298#M351344</link>
      <description>Thanks ballardw. So did I undestand correctly that the solution you provided does not require concatenating the variables?</description>
      <pubDate>Tue, 15 Aug 2023 06:55:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-times-a-word-appears-in-a-series-of/m-p/889298#M351344</guid>
      <dc:creator>Adubhai</dc:creator>
      <dc:date>2023-08-15T06:55:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of times a word appears in a series of string variables in each row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-times-a-word-appears-in-a-series-of/m-p/889300#M351346</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/300936"&gt;@Adubhai&lt;/a&gt;&amp;nbsp;It would help if you could post some representative sample data. If below data have doesn't create such data then please amend it and show us "reality".&lt;/P&gt;
&lt;P&gt;Given your term includes a column (:) it's eventually possible to use the count() function and just search for substrings in a string. If your actual data supports this then the code could be as simple as below.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover dsd;
  input (var1-var5)(:$30.);
  datalines;
Daughter:,Daughter &amp;amp; Son,Daughter:And:Son:,dAughter:,Son and Daughter:
Daughter:,Daughter: &amp;amp; Son:,,dAughter:,Son and Daughter
;

data want;
  set have;
  Daughter_Cnt_1=count(cats(of var1-var5),'Daughter:','i');
  Daughter_Cnt_2=count(cats(of var1-var5),'Daughter:');
run;

proc print data=want;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;You can of course also create an array over your variables and then loop over it as proposed by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2023 07:03:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-times-a-word-appears-in-a-series-of/m-p/889300#M351346</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-08-15T07:03:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of times a word appears in a series of string variables in each row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-times-a-word-appears-in-a-series-of/m-p/889303#M351348</link>
      <description>Thanks Patrick, but I have gone with ballardw's solution and it works. Maybe yours work too but I didn't need to try it.</description>
      <pubDate>Tue, 15 Aug 2023 08:02:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-times-a-word-appears-in-a-series-of/m-p/889303#M351348</guid>
      <dc:creator>Adubhai</dc:creator>
      <dc:date>2023-08-15T08:02:49Z</dc:date>
    </item>
  </channel>
</rss>

