<?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 number of the same values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Counting-number-of-the-same-values/m-p/763741#M241867</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/381519"&gt;@Emma2021&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Across all 4 variables because the value can be anything as in the examples&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So you are just checking to see the maximum number of matches anywhere in these 4 variables?&lt;/P&gt;</description>
    <pubDate>Tue, 24 Aug 2021 21:15:24 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2021-08-24T21:15:24Z</dc:date>
    <item>
      <title>Counting number of the same values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-number-of-the-same-values/m-p/763727#M241860</link>
      <description>I have 4 variables and would like to count the same values, for example,&lt;BR /&gt;&lt;BR /&gt;Var1 var2 var3 var4 desired_count&lt;BR /&gt;hi hi hi ho 3&lt;BR /&gt;add add hu hi 2&lt;BR /&gt;h j l o 1&lt;BR /&gt;su su su su 4&lt;BR /&gt;&lt;BR /&gt;How can I do that? Thank you!</description>
      <pubDate>Tue, 24 Aug 2021 20:49:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-number-of-the-same-values/m-p/763727#M241860</guid>
      <dc:creator>Emma2021</dc:creator>
      <dc:date>2021-08-24T20:49:55Z</dc:date>
    </item>
    <item>
      <title>Re: Counting number of the same values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-number-of-the-same-values/m-p/763735#M241862</link>
      <description>&lt;P&gt;are you always comparing to the value in VAR1? Or is there some other logic?&lt;/P&gt;</description>
      <pubDate>Tue, 24 Aug 2021 21:11:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-number-of-the-same-values/m-p/763735#M241862</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-08-24T21:11:53Z</dc:date>
    </item>
    <item>
      <title>Re: Counting number of the same values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-number-of-the-same-values/m-p/763736#M241863</link>
      <description>Across all 4 variables because the value can be anything as in the examples</description>
      <pubDate>Tue, 24 Aug 2021 21:13:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-number-of-the-same-values/m-p/763736#M241863</guid>
      <dc:creator>Emma2021</dc:creator>
      <dc:date>2021-08-24T21:13:01Z</dc:date>
    </item>
    <item>
      <title>Re: Counting number of the same values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-number-of-the-same-values/m-p/763737#M241864</link>
      <description>What would the results be from this data:&lt;BR /&gt;&lt;BR /&gt;Var1 var2 var3 var4 desired_count&lt;BR /&gt;hi hi ho ho &lt;BR /&gt;add add hu hi &lt;BR /&gt;h j l o</description>
      <pubDate>Tue, 24 Aug 2021 21:13:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-number-of-the-same-values/m-p/763737#M241864</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-08-24T21:13:13Z</dc:date>
    </item>
    <item>
      <title>Re: Counting number of the same values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-number-of-the-same-values/m-p/763740#M241866</link>
      <description>2&lt;BR /&gt;2&lt;BR /&gt;1</description>
      <pubDate>Tue, 24 Aug 2021 21:14:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-number-of-the-same-values/m-p/763740#M241866</guid>
      <dc:creator>Emma2021</dc:creator>
      <dc:date>2021-08-24T21:14:19Z</dc:date>
    </item>
    <item>
      <title>Re: Counting number of the same values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-number-of-the-same-values/m-p/763741#M241867</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/381519"&gt;@Emma2021&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Across all 4 variables because the value can be anything as in the examples&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So you are just checking to see the maximum number of matches anywhere in these 4 variables?&lt;/P&gt;</description>
      <pubDate>Tue, 24 Aug 2021 21:15:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-number-of-the-same-values/m-p/763741#M241867</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-08-24T21:15:24Z</dc:date>
    </item>
    <item>
      <title>Re: Counting number of the same values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-number-of-the-same-values/m-p/763742#M241868</link>
      <description>Yes</description>
      <pubDate>Tue, 24 Aug 2021 21:15:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-number-of-the-same-values/m-p/763742#M241868</guid>
      <dc:creator>Emma2021</dc:creator>
      <dc:date>2021-08-24T21:15:53Z</dc:date>
    </item>
    <item>
      <title>Re: Counting number of the same values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-number-of-the-same-values/m-p/763743#M241869</link>
      <description>Transpose your data, take a proc freq getting the most frequent count and merge that back in is the simplest solution IMO.</description>
      <pubDate>Tue, 24 Aug 2021 21:16:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-number-of-the-same-values/m-p/763743#M241869</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-08-24T21:16:10Z</dc:date>
    </item>
    <item>
      <title>Re: Counting number of the same values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-number-of-the-same-values/m-p/763750#M241870</link>
      <description>&lt;P&gt;What &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt; said&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Convert to long&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have2;
    set have;
    n=_n_;
run;
proc transpose data=have2 out=long prefix=values;
    by n;
    var var1-var4;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Count frequencies&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=long;
    by n;
    table values/out=_freqs_ noprint;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Find max frequency for each value of N&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=_freqs_;
    class n;
    var count;
    output out=max_freq max=max_freq;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;From here you can do whatever you want with the max_freq values.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Aug 2021 10:23:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-number-of-the-same-values/m-p/763750#M241870</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-08-25T10:23:46Z</dc:date>
    </item>
    <item>
      <title>Re: Counting number of the same values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-number-of-the-same-values/m-p/763755#M241874</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/381519"&gt;@Emma2021&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternatively, you can add the six Boolean values &lt;FONT face="courier new,courier"&gt;var&lt;EM&gt;i&lt;/EM&gt;=var&lt;EM&gt;j&lt;/EM&gt;&lt;/FONT&gt;&amp;nbsp;(&lt;FONT face="courier new,courier"&gt;&lt;EM&gt;i&lt;/EM&gt;&lt;/FONT&gt;,&lt;FONT face="courier new,courier"&gt;&lt;EM&gt;j&lt;/EM&gt;&lt;/FONT&gt;&amp;nbsp; = 1, 2, 3, 4; &lt;FONT face="courier new,courier"&gt;i&amp;lt;j&lt;/FONT&gt;) and then assign the desired count to the possible sums, which are 0, 1, 2, 3 and 6. That is, 0 → 1, 1 → 2, 2 → &lt;STRONG&gt;2&lt;/STRONG&gt; (unless you want to distinguish cases like "A A B B" [two pairs] from cases like "A A B C" [only one pair], which would make things even easier), 3 → 3, 6 → 4.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
dc=whichn(int((var1=var2)+(var1=var3)+(var1=var4)+(var2=var3)+(var2=var4)+(var3=var4)-1.5),-1,0,1,4);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The INT function and subtraction of 1.5 help to map both sums 1 and 2 to the desired count &lt;FONT face="courier new,courier"&gt;dc=2&lt;/FONT&gt; (as mentioned above [edit: &lt;EM&gt;2&lt;/EM&gt;, not 1]).&lt;/P&gt;</description>
      <pubDate>Tue, 24 Aug 2021 23:00:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-number-of-the-same-values/m-p/763755#M241874</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-08-24T23:00:39Z</dc:date>
    </item>
    <item>
      <title>Re: Counting number of the same values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-number-of-the-same-values/m-p/763791#M241893</link>
      <description>&lt;P&gt;Here are two variants of my first suggestion, using different ways to perform the mapping from the sum of Boolean values to the desired counts (&lt;FONT face="courier new,courier"&gt;dc&lt;/FONT&gt;)&lt;FONT face="helvetica"&gt;:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
dc=choosen((var1=var2)+(var1=var3)+(var1=var4)+(var2=var3)+(var2=var4)+(var3=var4)+1, 1,2,2,3,.,.,4);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
array c[0:6] _temporary_ (1 2 2 3 . . 4);
set have;
dc=c[(var1=var2)+(var1=var3)+(var1=var4)+(var2=var3)+(var2=var4)+(var3=var4)];
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think both are easier to understand and also easier to adapt if you want to implement a different mapping "sum → &lt;FONT face="courier new,courier"&gt;dc&lt;/FONT&gt;" than the one used so far:&lt;/P&gt;
&lt;PRE&gt;sum    dc

 0      1
 1      2
 2      2
 3      3
 6      4&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 Aug 2021 07:26:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-number-of-the-same-values/m-p/763791#M241893</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-08-25T07:26:49Z</dc:date>
    </item>
    <item>
      <title>Re: Counting number of the same values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-number-of-the-same-values/m-p/763827#M241912</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input (var1-var4) ($);
cards;
hi hi hi ho 
add add hu hi 
h j l o 
su su su su
;
data want;
 if _n_=1 then do;
   length k $ 100 count 8;
   declare hash h();
   declare hiter hi('h');
   h.definekey('k');
   h.definedata('count');
   h.definedone();
 end;
set have;
h.clear();
array x{*} $ _character_;
do i=1 to dim(x);
 if h.find(key:x{i})=0 then count=count+1;
  else count=1;
if not missing(x{i}) then  h.replace(key:x{i},data:count);
end;
do while(hi.next()=0);
 desired_count=max(desired_count,count);
end;
drop i count k;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Edited.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Aug 2021 11:48:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-number-of-the-same-values/m-p/763827#M241912</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-08-26T11:48:15Z</dc:date>
    </item>
  </channel>
</rss>

