<?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: Finding mode in multiple columns with groupby in cas in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Finding-mode-in-multiple-columns-with-groupby-in-cas/m-p/849099#M335714</link>
    <description>&lt;P&gt;Here is another way to generate the data you want, just using PROC SUMMARY:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=table order=freq;
  class value1 value2;
  by name surname;
  output out=counts;
run;


data want;
  merge 
    counts(where=(_type_=2) drop=value2)
    counts(where=(_type_=1) drop=value1)
    ;
  by name surname;
  if first.surname;
  drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 12 Dec 2022 14:09:53 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2022-12-12T14:09:53Z</dc:date>
    <item>
      <title>Finding mode in multiple columns with groupby in cas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-mode-in-multiple-columns-with-groupby-in-cas/m-p/848396#M335420</link>
      <description>&lt;P&gt;I want to find the mode of columns value1,value2 for every group of name,surname columns. as mentioned in below example.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Meet26_2-1670436275121.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/78172iF309A2B28B4A626B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Meet26_2-1670436275121.png" alt="Meet26_2-1670436275121.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have various approaches but couldn't able to generate any significant output so have not attached my approch. but here is sample data on which it can be tried.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data table;&lt;BR /&gt;infile datalines delimiter=',';&lt;BR /&gt;input name $ surname $ value1 value2;&lt;BR /&gt;datalines;&lt;BR /&gt;N1,S1,90,A&lt;BR /&gt;N1,S1,90,B&lt;BR /&gt;N1,S1,100,B&lt;BR /&gt;N1,S2,70,C&lt;BR /&gt;N1,S2,80,C&lt;BR /&gt;N1,S2,70,C&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Dec 2022 18:05:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-mode-in-multiple-columns-with-groupby-in-cas/m-p/848396#M335420</guid>
      <dc:creator>Meet26</dc:creator>
      <dc:date>2022-12-07T18:05:36Z</dc:date>
    </item>
    <item>
      <title>Re: Finding mode in multiple columns with groupby in cas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-mode-in-multiple-columns-with-groupby-in-cas/m-p/848432#M335441</link>
      <description>&lt;P&gt;First thing is make sure the data step generates the data you expect. The Value2 is missing as shown because it has not been read as character.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't have CAS so can't show code using it, much less with groupby. However the logic below may help. Caution: This approach requires a separate summary data set for each character variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the values were all numeric proc summary would do this easily. But you have included a character value so you need something a bit more.&lt;/P&gt;
&lt;P&gt;One way:&lt;/P&gt;
&lt;PRE&gt;proc sort data=table;
   by name surname;
run;

proc summary data=table;
  by name surname;
  var value1;
  output out=value1 (drop= _:) mode=;
run;

proc freq data=table noprint order=freq;
by name surname;
tables value2/ out=value2 ;
run;

data want;
   merge value1 
         value2 (drop=count percent)
   ;
   by name surname;
   if first.surname;
run;
&lt;/PRE&gt;
&lt;P&gt;The order=freq in proc freq places the highest count at the first of each by group. Otherwise you would sort the data by descending count to accomplish the same thing. So when combined with the proc summary output the first match of name and surname is the one you want. Summary will only have one value for each name surname combination.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I&lt;/P&gt;</description>
      <pubDate>Wed, 07 Dec 2022 21:59:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-mode-in-multiple-columns-with-groupby-in-cas/m-p/848432#M335441</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-12-07T21:59:54Z</dc:date>
    </item>
    <item>
      <title>Re: Finding mode in multiple columns with groupby in cas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-mode-in-multiple-columns-with-groupby-in-cas/m-p/849099#M335714</link>
      <description>&lt;P&gt;Here is another way to generate the data you want, just using PROC SUMMARY:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=table order=freq;
  class value1 value2;
  by name surname;
  output out=counts;
run;


data want;
  merge 
    counts(where=(_type_=2) drop=value2)
    counts(where=(_type_=1) drop=value1)
    ;
  by name surname;
  if first.surname;
  drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Dec 2022 14:09:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-mode-in-multiple-columns-with-groupby-in-cas/m-p/849099#M335714</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2022-12-12T14:09:53Z</dc:date>
    </item>
  </channel>
</rss>

