title: SAS Retain statement - how to retain previous non missing values into new column for comparison
body: <p>I am simply looking to create a new column retaining the value of a specific column within the previous record, so that I can compare the existing column against the new column. Further down the line I want to be able to output records whereby the values in both columns are different and lose the records where the values are the same</p>
  
  <p>Essentially I want my dataset to look like this, where the first idr has the retained date set to null:</p>
  
  <pre><code>Idr Date1           Date2
  1   20/01/2016  .
  1   20/01/2016  20/01/2016
  1   18/10/2016  20/01/2016
  2   07/03/2016  .
  2   18/05/2016  07/03/2016
  2   21/10/2016  18/05/2016
  3   29/01/2016  . 
  3   04/02/2016  29/01/2016
  3   04/02/2016  04/02/2016
  </code></pre>
  
  <p>I have used code along the following lines in the past whereby I have created a temporary variable referencing the data I want to retain:</p>
  
  <pre><code>date_temp=date1;
      data example2;
      set example1;
      by idr date1;
      date_temp=date1;
  retain date_temp ;
      if first.idr then do;
          date_temp=date1;
      end;else do;
          date2=date_temp;
      end;
   run;
  </code></pre>
  
  <p>I have searched the highs and lows of google - Any help would be greatly appreciated</p>