<?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: Find most frequent value across columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-most-frequent-value-across-columns/m-p/529132#M144519</link>
    <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;Thanks a lot FreelanceReinhard and Reeza. They&amp;nbsp;totally addressed my needs.&amp;nbsp;So the code below worked! Thanks so much.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; input id 1 fy2004 $ 3-4 fy2005 $ 6-7 fy2006 $ 9-10 fy2007 $ 12-13 _Mostfreq $ 17-18 _Count 20 _Total_count 22;
datalines;
1 00 13 13 13   13 3 4 
2 14    14 14   14 3 3 
3 12 12 12 05   12 3 4
4 01 01    02   01 2 3
5 00 00 12 12   00 2 4
;
data want;
	set have;
	length MostFreq $2;
	array list fy:;
	array _t[0:99] _temporary_;  *Set the max possoble value, in this example 99;
	call missing(of _t[*]);
	do i=1 to dim(list);
	  if list[i] ne '' then _t[input(list[i],2.)]+1;
	end;
	Count=max(of _t[*]);
	Total_Count=sum(of _t[*]);
	*MostFreq=put(whichn(Count, of _t[*]),z2.);
	MostFreq=put(whichn(Count, of _t[*])-1,z2.);
	drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/BLOCKQUOTE&gt;</description>
    <pubDate>Tue, 22 Jan 2019 17:22:51 GMT</pubDate>
    <dc:creator>Solph</dc:creator>
    <dc:date>2019-01-22T17:22:51Z</dc:date>
    <item>
      <title>Find most frequent value across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-most-frequent-value-across-columns/m-p/528917#M144433</link>
      <description>&lt;P&gt;I've a data set and would like to find the most frequent value from variables, along with max number of response and the count for the most frequent value.&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 have; input id 1 fy2004 $ 3-4 fy2005 $ 6-7 fy2006 $ 9-10 fy2007 $ 12-13 mostfreq $ 17-18 count 20 total_count 22;
datalines;
1 13 13 13 13   13 4 4 
2 14    14 14   14 3 3 
3 12 12 12 05   12 3 4
4 01 01    02   01 2 4
5 01 01 12 12   02 2 4
;

data want;
	set have;
	length MostFreq $2;
	array list fy:;
	array _t[10] _temporary_;
	call missing(of _t[*]);
	do i=1 to dim(list);
	  if list[i] ne '' then _t[input(list[i],2.)]+1;
	end;
	Count=max(of _t[*]);
	MostFreq=put(whichn(Count, of _t[*]),z2.);
	drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The&amp;nbsp;code came from&amp;nbsp;&lt;A href="https://communities.sas.com/t5/General-SAS-Programming/Find-most-frequent-response-across-multiple-variables/td-p/269774" target="_blank"&gt;https://communities.sas.com/t5/General-SAS-Programming/Find-most-frequent-response-across-multiple-variables/td-p/269774&lt;/A&gt;. I modified it but don't know why it didn't work on my data. I also need a third variable&amp;nbsp; for total count of&amp;nbsp;responses.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Much appeciated for your help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Jan 2019 21:50:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-most-frequent-value-across-columns/m-p/528917#M144433</guid>
      <dc:creator>Solph</dc:creator>
      <dc:date>2019-01-21T21:50:46Z</dc:date>
    </item>
    <item>
      <title>Re: Find most frequent value across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-most-frequent-value-across-columns/m-p/528928#M144436</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19901"&gt;@Solph&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Happy to see that my 2016 solution is still in use. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Back then your possible response values were '01' - '10', which was the reason for dimension 10 of the temporary array &lt;FONT face="courier new,courier"&gt;_t&lt;/FONT&gt;. Now, apparently, you need at least dimension 14. If you like, you can also use 99 to be on the safe side (e.g. for future additional questions). Note that this array contains the count for response '01' in its first element (&lt;FONT face="courier new,courier"&gt;_t[1]&lt;/FONT&gt;), the count for response '02' in the second and so on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the total count you can insert the line&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Total_Count=dim(list)-cmiss(of list[*]);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This will&amp;nbsp;result in the number of non-missing values among the variables in array &lt;FONT face="courier new,courier"&gt;list&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: Of course, a shorter formula for the total count is&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Total_Count=sum(of _t[*]);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, there is one case where the two would differ: If &lt;EM&gt;all&lt;/EM&gt; responses are missing, the first formula gives 0, whereas the second results in a missing value together with the familiar note "&lt;FONT face="courier new,courier"&gt;Missing values were generated&lt;/FONT&gt; ..." in the log (which would be triggered anyway by the formula for &lt;FONT face="courier new,courier"&gt;Count&lt;/FONT&gt;, though).&lt;/P&gt;</description>
      <pubDate>Mon, 21 Jan 2019 22:56:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-most-frequent-value-across-columns/m-p/528928#M144436</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-01-21T22:56:34Z</dc:date>
    </item>
    <item>
      <title>Re: Find most frequent value across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-most-frequent-value-across-columns/m-p/528945#M144444</link>
      <description>&lt;P&gt;Thanks a lot. It worked perfectly.&amp;nbsp;Don't&amp;nbsp;know why I didn't catch it. The code below reflected your input and now worked.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	length MostFreq $2;
	array list fy:;
	array _t[99] _temporary_;  *Set the max possible value to be 99;
	call missing(of _t[*]);
	do i=1 to dim(list);
	  if list[i] ne '' then _t[input(list[i],2.)]+1;
	end;
	Count=max(of _t[*]);
	Total_Count=sum(of _t[*]);
	MostFreq=put(whichn(Count, of _t[*]),z2.);
	drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Jan 2019 00:25:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-most-frequent-value-across-columns/m-p/528945#M144444</guid>
      <dc:creator>Solph</dc:creator>
      <dc:date>2019-01-22T00:25:54Z</dc:date>
    </item>
    <item>
      <title>Re: Find most frequent value across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-most-frequent-value-across-columns/m-p/528946#M144445</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Sorry. I've a follow up question. Hope you can help out. What if my&amp;nbsp;smallest&amp;nbsp;number is 00, not 01. The code wouldn't work when the starting number is 00. Thanks in advance.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; input id 1 fy2004 $ 3-4 fy2005 $ 6-7 fy2006 $ 9-10 fy2007 $ 12-13 _Mostfreq $ 17-18 _Count 20 _Total_count 22;
datalines;
1 00 13 13 13   13 3 4 
2 14    14 14   14 3 3 
3 12 12 12 05   12 3 4
4 01 01    02   01 2 3
5 00 00 12 12   00 2 4
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Jan 2019 00:47:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-most-frequent-value-across-columns/m-p/528946#M144445</guid>
      <dc:creator>Solph</dc:creator>
      <dc:date>2019-01-22T00:47:13Z</dc:date>
    </item>
    <item>
      <title>Re: Find most frequent value across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-most-frequent-value-across-columns/m-p/528971#M144457</link>
      <description>You can add an index to an array to have it go from 0 to 99. &lt;BR /&gt;&lt;BR /&gt;array _t(0:99);&lt;BR /&gt;&lt;BR /&gt;Then it will work.</description>
      <pubDate>Tue, 22 Jan 2019 03:53:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-most-frequent-value-across-columns/m-p/528971#M144457</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-01-22T03:53:24Z</dc:date>
    </item>
    <item>
      <title>Re: Find most frequent value across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-most-frequent-value-across-columns/m-p/528999#M144463</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19901"&gt;@Solph&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;SPAN&gt;Sorry. I've a follow up question. Hope you can help out. What if my&amp;nbsp;smallest&amp;nbsp;number is 00, not 01. The code wouldn't work when the starting number is 00. Thanks in advance.&lt;/SPAN&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;No problem. As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;said, define the temporary array as&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array _t[0:99] _temporary_;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;in order to count the number of '00' responses in &lt;FONT face="courier new,courier"&gt;_t[0]&lt;/FONT&gt;, the number of '01' responses in &lt;FONT face="courier new,courier"&gt;_t[1]&lt;/FONT&gt;, ..., and the number of '99' responses (if any) in &lt;FONT face="courier new,courier"&gt;_t[99]&lt;/FONT&gt;. There is one more change required, though: The WHICHN function used in the definition of &lt;FONT face="courier new,courier"&gt;MostFreq&lt;/FONT&gt; now yields numbers 1, 2, 3, ... if the maximum count is found in &lt;FONT face="courier new,courier"&gt;_t[&lt;STRONG&gt;0&lt;/STRONG&gt;]&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;_t[&lt;STRONG&gt;1&lt;/STRONG&gt;]&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;_t[&lt;STRONG&gt;2&lt;/STRONG&gt;]&lt;/FONT&gt;, ... To compensate for that, we have to subtract 1 from those values. Then they match the responses again.&lt;/P&gt;
&lt;PRE&gt;MostFreq=put(whichn(Count, of _t[*])&lt;FONT size="5" color="#008000"&gt;-1&lt;/FONT&gt;,z2.);&lt;/PRE&gt;</description>
      <pubDate>Tue, 22 Jan 2019 08:56:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-most-frequent-value-across-columns/m-p/528999#M144463</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-01-22T08:56:38Z</dc:date>
    </item>
    <item>
      <title>Re: Find most frequent value across columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-most-frequent-value-across-columns/m-p/529132#M144519</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;Thanks a lot FreelanceReinhard and Reeza. They&amp;nbsp;totally addressed my needs.&amp;nbsp;So the code below worked! Thanks so much.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; input id 1 fy2004 $ 3-4 fy2005 $ 6-7 fy2006 $ 9-10 fy2007 $ 12-13 _Mostfreq $ 17-18 _Count 20 _Total_count 22;
datalines;
1 00 13 13 13   13 3 4 
2 14    14 14   14 3 3 
3 12 12 12 05   12 3 4
4 01 01    02   01 2 3
5 00 00 12 12   00 2 4
;
data want;
	set have;
	length MostFreq $2;
	array list fy:;
	array _t[0:99] _temporary_;  *Set the max possoble value, in this example 99;
	call missing(of _t[*]);
	do i=1 to dim(list);
	  if list[i] ne '' then _t[input(list[i],2.)]+1;
	end;
	Count=max(of _t[*]);
	Total_Count=sum(of _t[*]);
	*MostFreq=put(whichn(Count, of _t[*]),z2.);
	MostFreq=put(whichn(Count, of _t[*])-1,z2.);
	drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Tue, 22 Jan 2019 17:22:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-most-frequent-value-across-columns/m-p/529132#M144519</guid>
      <dc:creator>Solph</dc:creator>
      <dc:date>2019-01-22T17:22:51Z</dc:date>
    </item>
  </channel>
</rss>

