<?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 of specific value across row in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-of-specific-value-across-row/m-p/945825#M370495</link>
    <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/298381"&gt;@mt88&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;And here is an example where the result is incorrect for two-digit value&lt;/P&gt;
&lt;PRE&gt;data test;
input a b c d;
Numzero = count(cats(a,b,c,d),'11');
cards;
1 0 9 1
1 1 0 0
0 9 11 1
0 0 9 1
;
run;&lt;/PRE&gt;
&lt;P&gt;The incorrect result for line 1 is because the CATS result for the line looks like "1100" and so there are two 1's next to each other and the Count function treats them as such.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This problem can be avoided by introducing a delimiter:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set test;
newvar=count(catx('||',.,of a--d,.),'|0|');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(where 0 could be replaced by 11 or any other number whose character representation is to be counted -- or even by a non-numeric character string).&lt;/P&gt;</description>
    <pubDate>Tue, 01 Oct 2024 18:30:07 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2024-10-01T18:30:07Z</dc:date>
    <item>
      <title>Count of specific value across row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-of-specific-value-across-row/m-p/945779#M370488</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd have a dataset where I'd like to create a new variable that gives me a count of a specific value for a set of variables across a row of data. For example, in this test dataset I want to create a new variable that that gives me a count of '0' values across variables &lt;EM&gt;a, b, c,&amp;nbsp;&lt;/EM&gt;and&amp;nbsp;&lt;EM&gt;d.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;input a b c d;&lt;BR /&gt;cards;&lt;BR /&gt;1 0 9 1&lt;BR /&gt;1 1 0 0&lt;BR /&gt;0 9 1 1&lt;BR /&gt;0 0 9 1&lt;BR /&gt;1 0 9 9&lt;BR /&gt;0 1 1 0&lt;BR /&gt;1 9 9 1&lt;BR /&gt;1 9 0 0&lt;BR /&gt;0 0 9 1&lt;BR /&gt;9 1 0 0;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The new variable (newvar) would return the following:&lt;/P&gt;&lt;P&gt;newvar&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;0&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is appreciated. I can provide more information if that is helpful.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks, Matt&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Oct 2024 13:49:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-of-specific-value-across-row/m-p/945779#M370488</guid>
      <dc:creator>mt88</dc:creator>
      <dc:date>2024-10-01T13:49:02Z</dc:date>
    </item>
    <item>
      <title>Re: Count of specific value across row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-of-specific-value-across-row/m-p/945788#M370490</link>
      <description>&lt;P&gt;Your data implies all of the values are one digit, is that in fact the case? I ask because there is a moderately slick solution if it is that may not work if you have some values that are 2 or more digits or non-integer.&lt;/P&gt;
&lt;P&gt;The CATS function creates a string with no spaces between converted numbers into a single long string and uses the Count function to determine how many 0 characters appear.&lt;/P&gt;
&lt;PRE&gt;data test;
input a b c d;
Numzero = count(cats(a,b,c,d),'0');
cards;
1 0 9 1
1 1 0 0
0 9 1 1
0 0 9 1
1 0 9 9
0 1 1 0
1 9 9 1
1 9 0 0
0 0 9 1
9 1 0 0
;
run;&lt;/PRE&gt;
&lt;P&gt;Note: Datalines end on the line with a semicolon or ;;;; if using datalines4; As such you do not want the semicolon on a line with data as it gets ignored.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And here is an example where the result is incorrect for two-digit value&lt;/P&gt;
&lt;PRE&gt;data test;
input a b c d;
Numzero = count(cats(a,b,c,d),'11');
cards;
1 0 9 1
1 1 0 0
0 9 11 1
0 0 9 1
;
run;&lt;/PRE&gt;
&lt;P&gt;The incorrect result for line 1 is because the CATS result for the line looks like "1100" and so there are two 1's next to each other and the Count function treats them as such.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your data is more complex (and possibly contains many more variables or search for more values):&lt;/P&gt;
&lt;PRE&gt;data test;
   input a b c d;
   array v(*) a b c d;
   do i=1 to dim(v);
      numzero = sum(numzero,(v[i]=0));
      numone  = sum(numzero,(v[i]=1));
      numnine = sum(numzero,(v[i]=9));
   end;
cards;
1 0 9 1
1 1 0 0
0 9 1 1
0 0 9 1
1 0 9 9
0 1 1 0
1 9 9 1
1 9 0 0
0 0 9 1
9 1 0 0
;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Oct 2024 14:31:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-of-specific-value-across-row/m-p/945788#M370490</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-10-01T14:31:59Z</dc:date>
    </item>
    <item>
      <title>Re: Count of specific value across row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-of-specific-value-across-row/m-p/945795#M370491</link>
      <description>&lt;P&gt;Let's generalize a bit.&amp;nbsp; Say you have integer values that range from&lt;STRIKE&gt; -3&lt;/STRIKE&gt;&amp;nbsp; 0 to 15, but you only want counts of 0's, 4's, 8's, and 14's.&amp;nbsp; Then you could&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;data test;
input a b c d;
cards;
1 0 9 1
1 1 0 0
0 9 1 1
0 0 9 1
1 0 9 9
0 1 1 0
1 9 9 1
1 9 0 0
0 0 9 1
9 1 0 0
5 6 14 15
8 12 4  0
run;

data want (drop=_:);
  set test;
  array values a -- d;
  array counts {0:15}  count_0  _dum1 - _dum3
                       count_4  _dum5 - _dum7 
                       count_8  _dum9 - _dum13
                       count_14 _dum15 ;

  do _v=1 to dim(values);
    counts{values{_v}}=sum(counts{values{_v}},1);
  end;

  /*convert missing counts to zeroes */
  array cnts count_: ;    /* Was mistakenly array cnts  n_: ; */
  do over cnts;
    cnts=coalesce(cnts,0);
  end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Oct 2024 03:47:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-of-specific-value-across-row/m-p/945795#M370491</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-10-02T03:47:31Z</dc:date>
    </item>
    <item>
      <title>Re: Count of specific value across row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-of-specific-value-across-row/m-p/945814#M370494</link>
      <description>&lt;P&gt;A simple way to count is use the fact that SAS will evaluate boolean expressions to 1 (TRUE) or 0 (FALSE) so that you can just add up the results to find the number of TRUE values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  input a b c d expect;
cards;
1 0 9 1  1
1 1 0 0  2
0 9 1 1  1
0 0 9 1  2
1 0 9 9  1
0 1 1 0  2
1 9 9 1  0
1 9 0 0  2
0 0 9 1  2
9 1 0 0  2
;

data want;
  set test;
  array x a b c d ;
  want=0;
  do over x;
    want+(x=0);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    a    b    c    d    expect    want

  1    1    0    9    1       1        1
  2    1    1    0    0       2        2
  3    0    9    1    1       1        1
  4    0    0    9    1       2        2
  5    1    0    9    9       1        1
  6    0    1    1    0       2        2
  7    1    9    9    1       0        0
  8    1    9    0    0       2        2
  9    0    0    9    1       2        2
 10    9    1    0    0       2        2
&lt;/PRE&gt;</description>
      <pubDate>Tue, 01 Oct 2024 18:15:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-of-specific-value-across-row/m-p/945814#M370494</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-10-01T18:15:07Z</dc:date>
    </item>
    <item>
      <title>Re: Count of specific value across row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-of-specific-value-across-row/m-p/945825#M370495</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/298381"&gt;@mt88&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;And here is an example where the result is incorrect for two-digit value&lt;/P&gt;
&lt;PRE&gt;data test;
input a b c d;
Numzero = count(cats(a,b,c,d),'11');
cards;
1 0 9 1
1 1 0 0
0 9 11 1
0 0 9 1
;
run;&lt;/PRE&gt;
&lt;P&gt;The incorrect result for line 1 is because the CATS result for the line looks like "1100" and so there are two 1's next to each other and the Count function treats them as such.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This problem can be avoided by introducing a delimiter:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set test;
newvar=count(catx('||',.,of a--d,.),'|0|');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(where 0 could be replaced by 11 or any other number whose character representation is to be counted -- or even by a non-numeric character string).&lt;/P&gt;</description>
      <pubDate>Tue, 01 Oct 2024 18:30:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-of-specific-value-across-row/m-p/945825#M370495</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-10-01T18:30:07Z</dc:date>
    </item>
  </channel>
</rss>

