<?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: How to aggregate columns based on plurality? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-aggregate-columns-based-on-plurality/m-p/377953#M90793</link>
    <description>&lt;P&gt;I've never heard the term plurality...in statistics we call it the mode.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Run a PROC FREQ - sort it descending, there's an option for that.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then use BY group processing to take the top observations per 'group'.&lt;/P&gt;
&lt;P&gt;untested code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=have;
table name*color*food/out=want;
run;

proc sort data=have; by name count;
run;

data want;
set have;
by name;
if last.name;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 20 Jul 2017 22:12:01 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2017-07-20T22:12:01Z</dc:date>
    <item>
      <title>How to aggregate columns based on plurality?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-aggregate-columns-based-on-plurality/m-p/377939#M90786</link>
      <description>&lt;P&gt;I would like to aggreate the columns of a dataset based on the plurality of non-missing values for each column.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Suppose my dataset was&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Name &amp;nbsp; &amp;nbsp; Color &amp;nbsp; &amp;nbsp; Food&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Jane &amp;nbsp; &amp;nbsp; &amp;nbsp; Red &amp;nbsp; &amp;nbsp; &amp;nbsp; Sushi&lt;/P&gt;&lt;P&gt;Jane &amp;nbsp; &amp;nbsp; &amp;nbsp; Blue &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Jane &amp;nbsp; &amp;nbsp; &amp;nbsp; Red &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;John &amp;nbsp; &amp;nbsp; &amp;nbsp; Green &amp;nbsp; &amp;nbsp;Yogurt&lt;/P&gt;&lt;P&gt;John &amp;nbsp; &amp;nbsp; &amp;nbsp; Green &amp;nbsp; &amp;nbsp;Sushi&lt;/P&gt;&lt;P&gt;John &amp;nbsp; &amp;nbsp; &amp;nbsp; Green &amp;nbsp; &amp;nbsp;Yogurt&lt;/P&gt;&lt;P&gt;John &amp;nbsp; &amp;nbsp; &amp;nbsp; Red&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to&amp;nbsp;summarize my dataset using something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sql; 
select Name, plurality(Color) as Color, plurality(Food) as Food
from raw_data
group by Name;&lt;/PRE&gt;&lt;P&gt;The result would be&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Name &amp;nbsp; &amp;nbsp; Color &amp;nbsp; &amp;nbsp; Food&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Jane &amp;nbsp; &amp;nbsp; &amp;nbsp; Red &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Sushi&lt;/P&gt;&lt;P&gt;John &amp;nbsp; &amp;nbsp; &amp;nbsp; Green &amp;nbsp; &amp;nbsp; Yogurt&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The plurality function would return the value that occurs most often after missing values are removed. Ties could be handled using alphabetical order.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What is the best way to accomplish this data transformation in SAS (version 9.3 or 9.4)?&lt;/P&gt;&lt;P&gt;(Is it possible to combine a user defined function with proc sql to accomplish this?)&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jul 2017 21:11:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-aggregate-columns-based-on-plurality/m-p/377939#M90786</guid>
      <dc:creator>Adam_Black</dc:creator>
      <dc:date>2017-07-20T21:11:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to aggregate columns based on plurality?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-aggregate-columns-based-on-plurality/m-p/377953#M90793</link>
      <description>&lt;P&gt;I've never heard the term plurality...in statistics we call it the mode.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Run a PROC FREQ - sort it descending, there's an option for that.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then use BY group processing to take the top observations per 'group'.&lt;/P&gt;
&lt;P&gt;untested code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=have;
table name*color*food/out=want;
run;

proc sort data=have; by name count;
run;

data want;
set have;
by name;
if last.name;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jul 2017 22:12:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-aggregate-columns-based-on-plurality/m-p/377953#M90793</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-07-20T22:12:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to aggregate columns based on plurality?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-aggregate-columns-based-on-plurality/m-p/383536#M91480</link>
      <description>Yes, the mode is what I am after. So I guess I have to do this aggregation one column at a time and then merge the results since there is no MODE aggregate function in PROC SQL. Thanks for the help!</description>
      <pubDate>Fri, 28 Jul 2017 02:31:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-aggregate-columns-based-on-plurality/m-p/383536#M91480</guid>
      <dc:creator>Adam_Black</dc:creator>
      <dc:date>2017-07-28T02:31:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to aggregate columns based on plurality?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-aggregate-columns-based-on-plurality/m-p/383538#M91481</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/25312"&gt;@Adam_Black&lt;/a&gt; wrote:&lt;BR /&gt;So I guess I have to do this aggregation one column at a time and then merge the results since there is no MODE aggregate function in PROC SQL. Thanks for the help!&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can do them all at once, sort it, take top record and then transpose. Probably easier to code and will be fully dynamic in case anything changes in the future.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example of how to do all at once &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; You may need to add a BY statement for yours to work properly.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jul 2017 02:47:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-aggregate-columns-based-on-plurality/m-p/383538#M91481</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-07-28T02:47:54Z</dc:date>
    </item>
  </channel>
</rss>

