<?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: counting occurrences in a string variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/counting-occurrences-in-a-string-variable/m-p/712731#M219800</link>
    <description>yes, I will go with that option.&lt;BR /&gt;Thank you.</description>
    <pubDate>Wed, 20 Jan 2021 15:08:10 GMT</pubDate>
    <dc:creator>emaneman</dc:creator>
    <dc:date>2021-01-20T15:08:10Z</dc:date>
    <item>
      <title>counting occurrences in a string variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-occurrences-in-a-string-variable/m-p/712695#M219782</link>
      <description>&lt;P&gt;Dear all,&lt;/P&gt;&lt;P&gt;I apologize for asking a similar question to one I asked sometime ago, but it does not work with string variables and that is what i need now.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a variable VAR which includes a series of words. I need to create a new variable multiple new variables which correspond to the frequencies of certain words in VAR.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the example below, starting out with VAR, I need to create NEWRED, NEWBLUE and NEWYELLOW, which would have the values as indicated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;OBS&lt;/TD&gt;&lt;TD&gt;VAR&lt;/TD&gt;&lt;TD&gt;NEWRED&lt;/TD&gt;&lt;TD&gt;NEWBLUE&lt;/TD&gt;&lt;TD&gt;NEWYELLOW&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;red blue red red blue yellow&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;red&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;blue yellow&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance for your attention.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;E&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2021 14:21:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-occurrences-in-a-string-variable/m-p/712695#M219782</guid>
      <dc:creator>emaneman</dc:creator>
      <dc:date>2021-01-20T14:21:54Z</dc:date>
    </item>
    <item>
      <title>Re: counting occurrences in a string variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-occurrences-in-a-string-variable/m-p/712697#M219784</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input OBS	VAR &amp;amp; $50.	;*NEWRED	NEWBLUE	NEWYELLOW;
cards;
1	red blue red red blue yellow	3	2	1
2	red	 1	0	0
3	blue yellow	  0	1	1
;

data want;
 set have;
 NEWRED=count(var,'red');
 NEWBLUE=count(var,'blue');
 newyellow=count(var,'yellow');
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r header" scope="col"&gt;OBS&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;VAR&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;NEWRED&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;NEWBLUE&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;newyellow&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="l data"&gt;red blue red red blue yellow&lt;/TD&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="l data"&gt;red&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="l data"&gt;blue yellow&lt;/TD&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Wed, 20 Jan 2021 14:27:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-occurrences-in-a-string-variable/m-p/712697#M219784</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2021-01-20T14:27:03Z</dc:date>
    </item>
    <item>
      <title>Re: counting occurrences in a string variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-occurrences-in-a-string-variable/m-p/712711#M219789</link>
      <description>&lt;P&gt;Try this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input VAR $50.;
datalines;	
red blue red red blue yellow 
red	                       
blue yellow	                 
;

data want;
   set have;
   newred    = count(VAR, 'red');
   newblue   = count(VAR, 'blue');
   newyellow = count(VAR, 'yellow');
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 Jan 2021 14:36:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-occurrences-in-a-string-variable/m-p/712711#M219789</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-20T14:36:02Z</dc:date>
    </item>
    <item>
      <title>Re: counting occurrences in a string variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-occurrences-in-a-string-variable/m-p/712712#M219790</link>
      <description>&lt;P&gt;Thank you, this works well and it's efficient.&lt;/P&gt;&lt;P&gt;I should have, however, specified a bit further, since what I also need is a NEW variable that counts the occurrence of multiple colors.&lt;/P&gt;&lt;P&gt;See below variable called REDANDBLUE. Of course, i could calculate it in a further step, as a sum of NEWRED and NEWBLUE, but I wonder whether it could be done in one step. I tried&amp;nbsp; NEWRED=count(var,'red' 'blue') and I have tried&amp;nbsp;NEWRED=count(var,'red', 'blue'), but neither works.&lt;/P&gt;&lt;P&gt;Again, sorry for not posting the whole thing at once.&lt;/P&gt;&lt;P&gt;e&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;OBS&lt;/TD&gt;&lt;TD&gt;VAR&lt;/TD&gt;&lt;TD&gt;NEWRED&lt;/TD&gt;&lt;TD&gt;NEWBLUE&lt;/TD&gt;&lt;TD&gt;NEWYELLOW&lt;/TD&gt;&lt;TD&gt;REDANDBLUE&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;red blue red red blue yellow&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;red&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;blue yellow&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Wed, 20 Jan 2021 14:40:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-occurrences-in-a-string-variable/m-p/712712#M219790</guid>
      <dc:creator>emaneman</dc:creator>
      <dc:date>2021-01-20T14:40:11Z</dc:date>
    </item>
    <item>
      <title>Re: counting occurrences in a string variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-occurrences-in-a-string-variable/m-p/712715#M219792</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/208370"&gt;@emaneman&lt;/a&gt;&amp;nbsp; Add another assignment statement in the same datastep-&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set have;
 NEWRED=count(var,'red');
 NEWBLUE=count(var,'blue');
 newyellow=count(var,'yellow');
REDANDBLUE=sum(newred,newblue);
 
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 Jan 2021 14:42:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-occurrences-in-a-string-variable/m-p/712715#M219792</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2021-01-20T14:42:21Z</dc:date>
    </item>
    <item>
      <title>Re: counting occurrences in a string variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-occurrences-in-a-string-variable/m-p/712716#M219793</link>
      <description>&lt;P&gt;It looks like you just need to add.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;REDANDBLUE=NEWRED+NEWBLUE;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 Jan 2021 14:43:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-occurrences-in-a-string-variable/m-p/712716#M219793</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-01-20T14:43:52Z</dc:date>
    </item>
    <item>
      <title>Re: counting occurrences in a string variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-occurrences-in-a-string-variable/m-p/712720#M219796</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/208370"&gt;@emaneman&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you, this works well and it's efficient.&lt;/P&gt;
&lt;P&gt;I should have, however, specified a bit further, since what I also need is a NEW variable that counts the occurrence of multiple colors.&lt;/P&gt;
&lt;P&gt;See below variable called REDANDBLUE. Of course, i could calculate it in a further step, as a sum of NEWRED and NEWBLUE, but I wonder whether it could be done in one step. I tried&amp;nbsp; NEWRED=count(var,'red' 'blue') and I have tried&amp;nbsp;NEWRED=count(var,'red', 'blue'), but neither works.&lt;/P&gt;
&lt;P&gt;Again, sorry for not posting the whole thing at once.&lt;/P&gt;
&lt;P&gt;e&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;OBS&lt;/TD&gt;
&lt;TD&gt;VAR&lt;/TD&gt;
&lt;TD&gt;NEWRED&lt;/TD&gt;
&lt;TD&gt;NEWBLUE&lt;/TD&gt;
&lt;TD&gt;NEWYELLOW&lt;/TD&gt;
&lt;TD&gt;REDANDBLUE&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;red blue red red blue yellow&lt;/TD&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;red&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;blue yellow&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Why is "Red&lt;FONT color="#FF6600"&gt;&lt;STRONG&gt;And&lt;/STRONG&gt;&lt;/FONT&gt;Blue" = 1 in the 2nd and 3rd obs, both don't have "RED BLUE" as value.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2021 14:54:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-occurrences-in-a-string-variable/m-p/712720#M219796</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-01-20T14:54:18Z</dc:date>
    </item>
    <item>
      <title>Re: counting occurrences in a string variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-occurrences-in-a-string-variable/m-p/712723#M219797</link>
      <description>&lt;P&gt;The varname is a bit misleading. I don't mean that both red and blue should appear for REDANDBLUE to have a value or 1 or more, but simply counting the number of either red or blue in VAR. As it has been pointed out, I can of course do the sum of the variables RED and BLUE, which I can create with the COUNT function, but I was wondering whether the COUNT function can count different occurrences (eg. red, blue) in one go. But it does not seem to be the case, since&amp;nbsp;&lt;SPAN&gt;&lt;STRONG&gt;REDBLUE=count(var,'red', 'blue')&lt;/STRONG&gt; does not work.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2021 15:01:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-occurrences-in-a-string-variable/m-p/712723#M219797</guid>
      <dc:creator>emaneman</dc:creator>
      <dc:date>2021-01-20T15:01:20Z</dc:date>
    </item>
    <item>
      <title>Re: counting occurrences in a string variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-occurrences-in-a-string-variable/m-p/712726#M219799</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/208370"&gt;@emaneman&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The varname is a bit misleading. I don't mean that both red and blue should appear for REDANDBLUE to have a value or 1 or more, but simply counting the number of either red or blue in VAR. As it has been pointed out, I can of course do the sum of the variables RED and BLUE, which I can create with the COUNT function, but I was wondering whether the COUNT function can count different occurrences (eg. red, blue) in one go. But it does not seem to be the case, since&amp;nbsp;&lt;SPAN&gt;&lt;STRONG&gt;REDBLUE=count(var,'red', 'blue')&lt;/STRONG&gt; does not work.&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Well, have a look at the docs of the function "count" ... the function is just not working that way. Other have already posted a solution, just adding the counts for blue and red will solve the problem.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2021 15:04:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-occurrences-in-a-string-variable/m-p/712726#M219799</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-01-20T15:04:10Z</dc:date>
    </item>
    <item>
      <title>Re: counting occurrences in a string variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-occurrences-in-a-string-variable/m-p/712731#M219800</link>
      <description>yes, I will go with that option.&lt;BR /&gt;Thank you.</description>
      <pubDate>Wed, 20 Jan 2021 15:08:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-occurrences-in-a-string-variable/m-p/712731#M219800</guid>
      <dc:creator>emaneman</dc:creator>
      <dc:date>2021-01-20T15:08:10Z</dc:date>
    </item>
    <item>
      <title>Re: counting occurrences in a string variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-occurrences-in-a-string-variable/m-p/712732#M219801</link>
      <description>&lt;P&gt;When discussing about occurrence of one string within another you ALWAYS have to include whether this is supposed to include composite words that may contain your target values. So if you are interested in "red" do you count "redfish" or "toured" as an occurence?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I realize that your actual data is likely to be more complex and use different strings but the shorter your target and/or more common the letters in the target the more likely to have that target string appear in a composite word or part of another word.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2021 15:08:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-occurrences-in-a-string-variable/m-p/712732#M219801</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-01-20T15:08:20Z</dc:date>
    </item>
  </channel>
</rss>

