<?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: Most frequent observation by row &amp;amp; number of distinct values by row in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Most-frequent-observation-by-row-amp-number-of-distinct-values/m-p/364423#M23894</link>
    <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;, I believe your codes can help a lot of people tackle&amp;nbsp;similar problems. We don't have IML however.&lt;/P&gt;</description>
    <pubDate>Tue, 06 Jun 2017 00:44:59 GMT</pubDate>
    <dc:creator>ayin</dc:creator>
    <dc:date>2017-06-06T00:44:59Z</dc:date>
    <item>
      <title>Most frequent observation by row &amp; number of distinct values by row</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Most-frequent-observation-by-row-amp-number-of-distinct-values/m-p/364177#M23887</link>
      <description>&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 date1:date9. date2:date9. date3:date9.;
  format date1 date9. date2 date9. date3 date9.;
  cards;
1 01JUN2001 01JUN2001 01JUN2001
2 01JUN2001 01JUN2001 05MAY1990
3 05MAY1990 05MAY1990 01JUN2001
4 01JUN2001 05MAY1990 .
;
run;

data want;
  input id date1:date9. date2:date9. date3:date9. date_adj:date9. date_count:best8.;
  format date1 date9. date2 date9. date3 date9. date_adj date9. date_count best8.;
  cards;
1 01JUN2001 01JUN2001 01JUN2001 01JUN2001 1
2 01JUN2001 01JUN2001 05MAY1990 01JUN2001 2
3 05MAY1990 05MAY1990 01JUN2001 05MAY1990 2
4 01JUN2001 05MAY1990 . . 2
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;From data have to data want:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Create a new column named 'date_adj', it would be the most frequent observation in that row; if same frequency, then output blank, or first/last appeared non-blank value (not really fussy about this output).&lt;/LI&gt;&lt;LI&gt;Create a new column named 'date_count', count the number of distinct values by that row.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;I understand SAS can do this by column, not sure how to achieve it by rows.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Jun 2017 00:23:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Most-frequent-observation-by-row-amp-number-of-distinct-values/m-p/364177#M23887</guid>
      <dc:creator>ayin</dc:creator>
      <dc:date>2017-06-05T00:23:59Z</dc:date>
    </item>
    <item>
      <title>Re: Most frequent observation by row &amp; number of distinct values by row</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Most-frequent-observation-by-row-amp-number-of-distinct-values/m-p/364182#M23888</link>
      <description>&lt;P&gt;Transpose to a row structure via PROC TRANSPOSE, obtain solution, and merge it back into main dataset.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Jun 2017 00:39:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Most-frequent-observation-by-row-amp-number-of-distinct-values/m-p/364182#M23888</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-06-05T00:39:20Z</dc:date>
    </item>
    <item>
      <title>Re: Most frequent observation by row &amp; number of distinct values by row</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Most-frequent-observation-by-row-amp-number-of-distinct-values/m-p/364183#M23889</link>
      <description>&lt;P&gt;By using case statements but we careful in case statement if below query has missed any scenario. But you get idea by seeing this query.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
create table want as
select date1, date2, date3,
 case when date1=date2 and date1 =date3 then date1
      when date1 =date2 and date1&amp;lt;&amp;gt;date3 then date1
      when date1 =date3 and date1 &amp;lt;&amp;gt;date2 then date1
      when date2= date3 and date1&amp;lt;&amp;gt;date3 then date2
      when date1&amp;lt;&amp;gt;date2 and date2&amp;lt;&amp;gt;date3 then .
 end as date_adj format=date9.,
 case when date1=date2 and date1 =date3 then 1
      when date1 =date2 and date1&amp;lt;&amp;gt;date3 then 2
      when date1 =date3 and date1 &amp;lt;&amp;gt;date2 then 2
       when date2= date3 and date1&amp;lt;&amp;gt;date3 then 2
      when date1&amp;lt;&amp;gt;date2 and date2&amp;lt;&amp;gt;date3 then 2
 end as date_count
 from have ;
 quit;&lt;/PRE&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Jun 2017 01:02:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Most-frequent-observation-by-row-amp-number-of-distinct-values/m-p/364183#M23889</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2017-06-05T01:02:46Z</dc:date>
    </item>
    <item>
      <title>Re: Most frequent observation by row &amp; number of distinct values by row</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Most-frequent-observation-by-row-amp-number-of-distinct-values/m-p/364273#M23892</link>
      <description>&lt;P&gt;How about IML code ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id date1:date9. date2:date9. date3:date9.;
  format date1 date9. date2 date9. date3 date9.;
  cards;
1 01JUN2001 01JUN2001 01JUN2001
2 01JUN2001 01JUN2001 05MAY1990
3 05MAY1990 05MAY1990 01JUN2001
4 01JUN2001 05MAY1990 .
;
run;
proc iml;
use have(keep=date:) nobs nobs;
read all var _all_ into x;
close;
date_adj=j(nobs,1,.);
date_count=j(nobs,1,.);
do i=1 to nobs;
 temp=x[i,];
 call tabulate(level,freq,temp);
 if sum(freq=max(freq)) ^= 1 then date_adj[i]=.;
  else date_adj[i]=level[ freq[&amp;lt;:&amp;gt;] ];
 date_count[i]=ncol(level);
end;
create temp var{date_adj date_count};
append;
close;
run;

data want;
 merge have temp;
 format date_adj date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 05 Jun 2017 14:02:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Most-frequent-observation-by-row-amp-number-of-distinct-values/m-p/364273#M23892</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-06-05T14:02:16Z</dc:date>
    </item>
    <item>
      <title>Re: Most frequent observation by row &amp; number of distinct values by row</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Most-frequent-observation-by-row-amp-number-of-distinct-values/m-p/364421#M23893</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37783"&gt;@kiranv_&lt;/a&gt;,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your approach. Because in my real data I have 8 columns, it might be too hard to build the logic by using 'Case'.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Jun 2017 00:43:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Most-frequent-observation-by-row-amp-number-of-distinct-values/m-p/364421#M23893</guid>
      <dc:creator>ayin</dc:creator>
      <dc:date>2017-06-06T00:43:15Z</dc:date>
    </item>
    <item>
      <title>Re: Most frequent observation by row &amp; number of distinct values by row</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Most-frequent-observation-by-row-amp-number-of-distinct-values/m-p/364423#M23894</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;, I believe your codes can help a lot of people tackle&amp;nbsp;similar problems. We don't have IML however.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Jun 2017 00:44:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Most-frequent-observation-by-row-amp-number-of-distinct-values/m-p/364423#M23894</guid>
      <dc:creator>ayin</dc:creator>
      <dc:date>2017-06-06T00:44:59Z</dc:date>
    </item>
    <item>
      <title>Re: Most frequent observation by row &amp; number of distinct values by row</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Most-frequent-observation-by-row-amp-number-of-distinct-values/m-p/364424#M23895</link>
      <description>&lt;P&gt;Yes.&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;approach is best to follow&lt;/P&gt;</description>
      <pubDate>Tue, 06 Jun 2017 00:45:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Most-frequent-observation-by-row-amp-number-of-distinct-values/m-p/364424#M23895</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2017-06-06T00:45:26Z</dc:date>
    </item>
    <item>
      <title>Re: Most frequent observation by row &amp; number of distinct values by row</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Most-frequent-observation-by-row-amp-number-of-distinct-values/m-p/364433#M23896</link>
      <description>&lt;P&gt;The following codes proved to be working:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro checkUnique(fileName=,col=,ID=);
%global colList;

/* get all date columns' names */
proc sql noprint;
select name into: colList separated by ' '
  from dictionary.columns
  where libname = 'WORK' and memname contains upcase("&amp;amp;fileName") and name contains upcase("&amp;amp;col");
quit;

/* transpose back to flat table */
data temp1(keep= ID_adj frequent position);
  set work.&amp;amp;fileName.(rename= (&amp;amp;ID.=ID));
  array &amp;amp;col._array{*} &amp;amp;colList.;
  do i = 1 to dim(&amp;amp;col._array);
    ID_adj = ID;
    frequent = &amp;amp;col._array{i};
    position = i;
    output;
  end;
  format frequent date9.;
run;

/* get the frequency of each date value */
proc sql noprint;
  create table temp2 as select
      ID_adj,
      frequent,
      count(frequent) as count
      /* count(distinct(frequent)) as num_unique */
          from temp1
          group by ID_adj, frequent;
quit;

proc sort data= temp2(where=(frequent ne .));
  by ID_adj descending count descending frequent;
run;

/* get the most frequent date */
data output.&amp;amp;fileName.(keep= &amp;amp;ID. &amp;amp;col._adj);
  set temp2;
  by ID_adj descending count;
  if first.ID_adj;
  format frequent date9.;
  rename ID_adj=&amp;amp;ID.;
  rename frequent=&amp;amp;col._adj;
run;

%mend checkUnique;

%checkUnique(fileName=test,col=date,ID=id);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Jun 2017 01:19:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Most-frequent-observation-by-row-amp-number-of-distinct-values/m-p/364433#M23896</guid>
      <dc:creator>ayin</dc:creator>
      <dc:date>2017-06-06T01:19:01Z</dc:date>
    </item>
  </channel>
</rss>

