<?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: Help counting the most frequent value across multiple columns and return that value to a new col in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-counting-the-most-frequent-value-across-multiple-columns/m-p/931844#M366603</link>
    <description>&lt;P&gt;The most simple way is using PROC IML.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Id (system1 system2 system3 system4) ($);
cards;
1 0001 0001 0001 0002
2 0002 0001 0002 0003
3 0003 0002 0002 0004
4 . 0001 . .
;

proc iml;
use have(keep=system:) nobs n;
read all var _all_ into x;
close;
countofmostfrequent=j(n,1,.);
do i=1 to n;
  call tabulate(level,freq,x[i,]);
  countofmostfrequent[i]=max(freq);
end;
create countofmostfrequent var{countofmostfrequent};
append;
close;
quit;
data want;
 merge have countofmostfrequent;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 12 Jun 2024 01:15:32 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2024-06-12T01:15:32Z</dc:date>
    <item>
      <title>Help counting the most frequent value across multiple columns and return that value to a new column.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-counting-the-most-frequent-value-across-multiple-columns/m-p/931820#M366588</link>
      <description>&lt;P&gt;I have a dataset&amp;nbsp;&lt;/P&gt;&lt;P&gt;Id system1 system2 system3 system4&lt;/P&gt;&lt;P&gt;1 0001 0001 0001 0002&lt;/P&gt;&lt;P&gt;2 0002 0001 0002 0003&lt;/P&gt;&lt;P&gt;3 0003 0002 0002 0004&lt;/P&gt;&lt;P&gt;4 missing 0001 missing missing&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to the new data set to include a new column which is the most frequent value across these systems.&lt;/P&gt;&lt;P&gt;Id system1 system2 system3 system4 mostfrequent countofmostfrequent&lt;/P&gt;&lt;P&gt;1 0001 0001 0001 0002 0001 3&lt;/P&gt;&lt;P&gt;2 0002 0001 0002 0003 0002 2&lt;/P&gt;&lt;P&gt;3 0003 0002 0002 0004 0002 2&amp;nbsp;&lt;/P&gt;&lt;P&gt;4 missing 0001 missing missing 0001 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help you can provide would be greatly appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2024 22:10:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-counting-the-most-frequent-value-across-multiple-columns/m-p/931820#M366588</guid>
      <dc:creator>jaypennington</dc:creator>
      <dc:date>2024-06-11T22:10:27Z</dc:date>
    </item>
    <item>
      <title>Re: Help counting the most frequent value across multiple columns and return that value to a new col</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-counting-the-most-frequent-value-across-multiple-columns/m-p/931827#M366589</link>
      <description>&lt;P&gt;Are these system variables numeric or character?&lt;/P&gt;
&lt;P&gt;By "across these systems" do you mean within that observation (just to make sure I understand).&lt;/P&gt;
&lt;P&gt;Does the ID variable repeat in the data set? If it does repeat does that affect how "most frequent" would be determined?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What are the rules for tie-breaking when the count for more than one value qualifies as 'most frequent': 2 values of 0002 and two values of 0003 or 4 different values out of 4 variables?&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2024 22:30:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-counting-the-most-frequent-value-across-multiple-columns/m-p/931827#M366589</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-06-11T22:30:57Z</dc:date>
    </item>
    <item>
      <title>Re: Help counting the most frequent value across multiple columns and return that value to a new col</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-counting-the-most-frequent-value-across-multiple-columns/m-p/931830#M366592</link>
      <description>&lt;P&gt;Thanks for the quick response!&lt;/P&gt;&lt;P&gt;The system variables are character but can convert them to numeric if it is easier.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The is wide with one id per row and does not repeat. If there a tie breaks then pick the system that is the last value in the chain.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2024 23:04:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-counting-the-most-frequent-value-across-multiple-columns/m-p/931830#M366592</guid>
      <dc:creator>jaypennington</dc:creator>
      <dc:date>2024-06-11T23:04:31Z</dc:date>
    </item>
    <item>
      <title>Re: Help counting the most frequent value across multiple columns and return that value to a new col</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-counting-the-most-frequent-value-across-multiple-columns/m-p/931833#M366595</link>
      <description>&lt;P&gt;If you transpose from wide to tall and skinny, you could do this with PROC FREQ or similar.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Even if you need to deliver wide data for some reason (e.g. Excel users), I would transform to tall and skinny in SAS, then run proc freq (or similar), and transpose back to wide when you're done with the data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One lesson I learned early in my career was just because you are delivered wide data, and asked to return wide data, does not mean you're required to work with the wide data.&amp;nbsp; You're always free to change the data format while processing, to make it easier to work with in SAS (and less likely to make coding errors).&amp;nbsp; I spent a few years excited to learn arrays, multidimensional arrays, etc.&amp;nbsp; Then a brilliant boss pointed out how much simpler (and less error-prone) my code could be if I just normalized the data.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2024 23:18:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-counting-the-most-frequent-value-across-multiple-columns/m-p/931833#M366595</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2024-06-11T23:18:28Z</dc:date>
    </item>
    <item>
      <title>Re: Help counting the most frequent value across multiple columns and return that value to a new col</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-counting-the-most-frequent-value-across-multiple-columns/m-p/931844#M366603</link>
      <description>&lt;P&gt;The most simple way is using PROC IML.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Id (system1 system2 system3 system4) ($);
cards;
1 0001 0001 0001 0002
2 0002 0001 0002 0003
3 0003 0002 0002 0004
4 . 0001 . .
;

proc iml;
use have(keep=system:) nobs n;
read all var _all_ into x;
close;
countofmostfrequent=j(n,1,.);
do i=1 to n;
  call tabulate(level,freq,x[i,]);
  countofmostfrequent[i]=max(freq);
end;
create countofmostfrequent var{countofmostfrequent};
append;
close;
quit;
data want;
 merge have countofmostfrequent;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Jun 2024 01:15:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-counting-the-most-frequent-value-across-multiple-columns/m-p/931844#M366603</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-06-12T01:15:32Z</dc:date>
    </item>
    <item>
      <title>Re: Help counting the most frequent value across multiple columns and return that value to a new col</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-counting-the-most-frequent-value-across-multiple-columns/m-p/931851#M366606</link>
      <description>&lt;P&gt;If you don't have module SAS/IML , could try Hash Table.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Id (system1 system2 system3 system4) ($);
cards;
1 0001 0001 0001 0002
2 0002 0001 0002 0003
3 0003 0002 0002 0004
4 . 0001 . .
;
data want;
 if _n_=1 then do;
   length k $ 100 n countofmostfrequent 8;
   call missing(k,n,countofmostfrequent);
   declare hash h();
   h.definekey('k');
   h.definedata('n');
   h.definedone();
 end;
set have;
h.clear();
array x{*} $ system:;
do i=1 to dim(x);
 if not missing(x{i}) then do;
   k=x{i};
   if h.find()=0 then do;n+1;h.replace(); end;
    else do;n=1;h.replace(); end; 
   countofmostfrequent=max(countofmostfrequent,n);
 end;
end;
drop k n i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Jun 2024 01:47:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-counting-the-most-frequent-value-across-multiple-columns/m-p/931851#M366606</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-06-12T01:47:59Z</dc:date>
    </item>
    <item>
      <title>Re: Help counting the most frequent value across multiple columns and return that value to a new col</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-counting-the-most-frequent-value-across-multiple-columns/m-p/931857#M366610</link>
      <description>&lt;P&gt;The code below uses the ability to sort the SYSTEM values (call sortc or call sortn), and then counts contiguous identical values.&amp;nbsp; If, as you said, the SYSTEM values are character, then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input Id @3 (system1 system2 system3 system4) ($4. +1);
datalines;
1 0001 0001 0001 0002
2 0002 0001 0002 0003
3 0003 0002 0002 0004
run;

data want (drop=_:);
  set have;
  call sortc(of system:);
  length _str $300;    /*Long enough to hold all the SYSTEM values */
  _str=catx(' ',of system:);

 
  mode_val='            '; 
  mode_frq=0;

  do while (_str^=' ');
    do _frq=1 by 1 until(scan(_str,_frq+1)^=scan(_str,1));
    end;
    if _frq&amp;gt;mode_frq then do;
      mode_val=scan(_str,1);
      mode_frq=_frq;
    end;
    _str=tranwrd(_str||' ',scan(_str,1)||' ','');
  end;
  set have;   /*Reread the same obs, so SYSTEM values are in original order*/
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And if the SYSTEM variables are numeric, you only have to change three lines of code&amp;nbsp; (indicated by&amp;nbsp;&lt;EM&gt;&lt;STRONG&gt;/*!!For numeric vals!!*/&lt;/STRONG&gt;&lt;/EM&gt; below):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set have;
  call sortn(of system:);   /*!!For numeric vals!!*/
  length _str $300;  /*Long enough to hold all the SYSTEM values */
  _str=catx(' ',of system:);

 
  mode_val=.;  /*!!For numeric vals!!*/
  mode_frq=0;

  do while (_str^=' ');
    do _frq=1 by 1 until(scan(_str,_frq+1)^=scan(_str,1));
    end;
    if _frq&amp;gt;mode_frq then do;
      mode_val=input(scan(_str,1),best32.); /*!!For numeric vals!!*/
      mode_frq=_frq;
    end;
    _str=tranwrd(_str||' ',scan(_str,1)||' ','');
  end;
  set have;   /*Reread the same obs, so the SYSTEM values are in original order*/
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And note that, while the CALL SORTC (or CALL SORTN) destroys the original order of values for SYSTEM1 through SYSTEM4, at the end of the DATA step is a &lt;EM&gt;&lt;STRONG&gt;second SET HAVE, which rereads the same observation, thereby restoring SYSTEM values to original order&lt;/STRONG&gt;&lt;/EM&gt;.&amp;nbsp; This uses almost no resources, since the reread is retrieving data from cached memory, not the disk storage device.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2024 03:55:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-counting-the-most-frequent-value-across-multiple-columns/m-p/931857#M366610</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-06-12T03:55:48Z</dc:date>
    </item>
    <item>
      <title>Re: Help counting the most frequent value across multiple columns and return that value to a new col</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-counting-the-most-frequent-value-across-multiple-columns/m-p/932576#M366862</link>
      <description>&lt;P&gt;This hash table works great! There is one more column that is needed. I also want to return the value the countofmostfrequent into a new column. Any help you could provide would be greatly appreciated!&lt;/P&gt;</description>
      <pubDate>Sun, 16 Jun 2024 17:20:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-counting-the-most-frequent-value-across-multiple-columns/m-p/932576#M366862</guid>
      <dc:creator>jaypennington</dc:creator>
      <dc:date>2024-06-16T17:20:31Z</dc:date>
    </item>
    <item>
      <title>Re: Help counting the most frequent value across multiple columns and return that value to a new col</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-counting-the-most-frequent-value-across-multiple-columns/m-p/932577#M366863</link>
      <description>&lt;P&gt;Nevermind. I see that it is stored in column k. Thank you!&lt;/P&gt;</description>
      <pubDate>Sun, 16 Jun 2024 17:25:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-counting-the-most-frequent-value-across-multiple-columns/m-p/932577#M366863</guid>
      <dc:creator>jaypennington</dc:creator>
      <dc:date>2024-06-16T17:25:11Z</dc:date>
    </item>
    <item>
      <title>Re: Help counting the most frequent value across multiple columns and return that value to a new col</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-counting-the-most-frequent-value-across-multiple-columns/m-p/932585#M366868</link>
      <description>&lt;P&gt;You mean the value have "countofmostfrequent" ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Id (system1 system2 system3 system4) ($);
cards;
1 0001 0001 0001 0002
2 0002 0001 0002 0003
3 0003 0002 0002 0004
4 . 0001 . .
;
data want;
 if _n_=1 then do;
   length k value $ 100 n  countofmostfrequent 8;
   call missing(k,n,value,countofmostfrequent);
   declare hash h();
   h.definekey('k');
   h.definedata('n');
   h.definedone();
 end;
set have;
h.clear();
array x{*} $ system:;
do i=1 to dim(x);
 if not missing(x{i}) then do;
   k=x{i};
   if h.find()=0 then do;n+1;h.replace(); end;
    else do;n=1;h.replace(); end; 
   if countofmostfrequent&amp;lt;n then do;value=k; countofmostfrequent=n;end;
 end;
end;
drop k n i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 17 Jun 2024 01:02:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-counting-the-most-frequent-value-across-multiple-columns/m-p/932585#M366868</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-06-17T01:02:46Z</dc:date>
    </item>
  </channel>
</rss>

