<?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: Calculate mode for each row in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mode-for-each-row/m-p/469001#M119877</link>
    <description>&lt;P&gt;Thank you very much. It works&lt;/P&gt;</description>
    <pubDate>Sun, 10 Jun 2018 12:50:47 GMT</pubDate>
    <dc:creator>mrzlatan91</dc:creator>
    <dc:date>2018-06-10T12:50:47Z</dc:date>
    <item>
      <title>Calculate mode for each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mode-for-each-row/m-p/468952#M119845</link>
      <description>&lt;P&gt;Hey guys,&lt;/P&gt;&lt;P&gt;I have a dataset like as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data work.class1;&lt;/P&gt;&lt;P&gt;input date $ cusip_hld $ value1 $ value2 $ value3$ value4 $;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;01DEC05 A 3 5 3 2 3&lt;/P&gt;&lt;P&gt;01DEC05 B 2 4 2 1 2&amp;nbsp;&lt;/P&gt;&lt;P&gt;01JAN06 A 1 2 3 4 1&lt;/P&gt;&lt;P&gt;01JAN06 C 4 4 4 0 1&amp;nbsp;&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there an opportunity to add a column "classValue" which is calculated as the mode &amp;nbsp;for each row, i.e. the mode for each&amp;nbsp;&lt;/P&gt;&lt;P&gt;date-cusip_hld-combination? (With mode I mean the value which appears most often, for example, the mode of 01JAN06 and Cusip_hld = C would be 4 in my table)&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I found some general&amp;nbsp;solutions here, but I have 2 specific issues which makes it hard for me to reproduce those.&lt;/P&gt;&lt;P&gt;First, it is possible (and often), that there are missing values in the value_i Columns. I only want to calculate the mode of the non-missing values in the row.&lt;/P&gt;&lt;P&gt;Second, the code is in a macro. In each Iteration, one column value_i is added. The number of value_i columns is saved in the macro variable &amp;amp;fundNumber (if that helps).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I´m really thankful of any suggestions or solutions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kind regards,&lt;/P&gt;&lt;P&gt;Zlatan&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 09 Jun 2018 22:37:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-mode-for-each-row/m-p/468952#M119845</guid>
      <dc:creator>mrzlatan91</dc:creator>
      <dc:date>2018-06-09T22:37:13Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate mode for each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mode-for-each-row/m-p/468959#M119851</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/186452"&gt;@mrzlatan91&lt;/a&gt;&amp;nbsp; hello mate!, long time.&amp;nbsp; Please also post a sample of your wanted output for responders to know the exact requirement.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyways, I am going home now. Have a great weekend&lt;/P&gt;</description>
      <pubDate>Sat, 09 Jun 2018 23:17:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-mode-for-each-row/m-p/468959#M119851</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-06-09T23:17:09Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate mode for each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mode-for-each-row/m-p/468973#M119857</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.class1;

input date $ cusip_hld $ value1 $ value2 $ value3$ value4 $ value5 $;

datalines;
01DEC05 A 3 5 3 2 3
01DEC05 B 2 4 2 1 2 
01JAN06 A 1 2 3 4 1
01JAN06 C 4 4 4 0 1 
;

run;

proc transpose data= class1 out=t;
by date cusip_hld notsorted;
var value:;
run;

proc freq data=t order=freq noprint;
by 	date cusip_hld notsorted;
tables col1/out=t1(drop=percent) ;
run;

data want;
set t1;
by date cusip_hld notsorted;
if first.cusip_hld;
mode=col1;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 10 Jun 2018 04:24:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-mode-for-each-row/m-p/468973#M119857</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-06-10T04:24:55Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate mode for each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mode-for-each-row/m-p/468988#M119868</link>
      <description>&lt;P&gt;Hey mate,&amp;nbsp;&lt;/P&gt;&lt;P&gt;first, I want to thank you so much for your solution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sorry that I forgot to post my wanted dataset, but I guess this is exactly what i wanted (if column mode contains the mode of the values)&lt;/P&gt;&lt;P&gt;Unfortunately, as I said, I have a lot of missing values, s.t. in column mode (and col1 as well) are only missing values, s.t. I can´t&amp;nbsp;&lt;/P&gt;&lt;P&gt;proof if the result is correct.&lt;/P&gt;&lt;P&gt;Could you kindly tell me how to fix the problem with the missing values? Unfortunately, I´m an absolute SAS Noob.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To understand it: table T represents the transposed table, _name_ is like a counting variable and col1 contains the values.&lt;/P&gt;&lt;P&gt;Table T1 counts how often a specific value for a cusip-date combination exists.&lt;/P&gt;&lt;P&gt;And Table Want contains all date-cusip combinations, count represents how often the mode value exists for that&amp;nbsp;&lt;/P&gt;&lt;P&gt;combination and mode represents the mode value, but what does column col1 then stand for?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have a nice day mate.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 10 Jun 2018 09:58:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-mode-for-each-row/m-p/468988#M119868</guid>
      <dc:creator>mrzlatan91</dc:creator>
      <dc:date>2018-06-10T09:58:52Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate mode for each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mode-for-each-row/m-p/468994#M119873</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.class1;
input date $ cusip_hld $ value1 $ value2 $ value3$ value4 $;
datalines;
01DEC05 A 3 5 3 2 3
01DEC05 B 2 4 2 1 2 
01JAN06 A 1 2 3 4 1
01JAN06 C 4 4 4 0 1 
;

run;

data fin_mode (drop=k count i max ran);
if _n_ eq 1 then do;
declare hash ha();
declare hiter hi('ha');
ha.definekey('k');
ha.definedata('k','count');
ha.definedone();
declare hash random(ordered:'a');
declare hiter hir('random');
random.definekey('ran');
random.definedata('k','count');
random.definedone();
end;
set class1;
array x{*} value: ;
do i=1 to dim(x);
if not missing(x{i}) then do;
k=x{i};
if ha.find()=0 then do;count+1;ha.replace();end;
else do;count=1;ha.add();end;
end;
end;

do while(hi.next()=0);
ran=ranuni(-1);random.add();
end;
max=0;
do while(hir.next()=0);
if count gt max then do;max=count;mod=k;end;
end;
ha.clear(); random.clear();
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 10 Jun 2018 10:49:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-mode-for-each-row/m-p/468994#M119873</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-06-10T10:49:11Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate mode for each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mode-for-each-row/m-p/469001#M119877</link>
      <description>&lt;P&gt;Thank you very much. It works&lt;/P&gt;</description>
      <pubDate>Sun, 10 Jun 2018 12:50:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-mode-for-each-row/m-p/469001#M119877</guid>
      <dc:creator>mrzlatan91</dc:creator>
      <dc:date>2018-06-10T12:50:47Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate mode for each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mode-for-each-row/m-p/469045#M119901</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/186452"&gt;@mrzlatan91&lt;/a&gt;&amp;nbsp; I'm glad you got a great solution by the &lt;EM&gt;&lt;STRONG&gt;champion&amp;nbsp;&lt;/STRONG&gt;&lt;/EM&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;whose code I tend to copy all the time for my needs. Going forward, please do post a wanted sample like your input sample to make it easy to understand.&lt;/P&gt;</description>
      <pubDate>Sun, 10 Jun 2018 18:27:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-mode-for-each-row/m-p/469045#M119901</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-06-10T18:27:42Z</dc:date>
    </item>
  </channel>
</rss>

