title: SAS: Most frequent value (Like a MODE) ties solved by recency?
body: <p>I have data like this:</p>
  
  <pre><code>data mydata;
  input ID $ Val $ Date;
  datalines;
  1  A  2010-12-01 
  1  B  2010-12-03 
  1  A  2010-12-04 
  1  B  2010-12-08
  2  X  2009-10-01 
  2  X  2009-10-02 
  2  Z  2009-10-03 
  ;
  run;
  </code></pre>
  
  <p>I would like the mode returned where it exists.  ID 1, however, doesn't have a true mode.  In the case of ties where modes do not exist I would like the most recent Val to break the tie (as in id 1).</p>
  
  <p>Desired OUTPUT:</p>
  
  <pre><code>ID Mode
  1  B  
  2  X  
  </code></pre>
  
  <p>I tried proc univariate (which only handles numeric modes, another problem) but this gives the dataset with mode null; which SAS has correct but is not the desired output.  I would like to do this in a datastep.</p>
  
  <p>CODE:</p>
  
  <pre><code>proc univariate data=mydata noprint;
  class id;
  var val;
  output out=modetable mode=mode;
  run;
  </code></pre>
  
  <p>OUTPUT:</p>
  
  <pre><code>ID Mode
  1  
  2  X
  </code></pre>