title: Calculate a date difference within a group variable, over rows of missing observations
body: <p>In SAS, I have ID, Date1, and Date2 sorted by ascending ID, Date1, and Date2. The sort causes the missing Date2 values to be where they are, as desired. How can I calculate the Date2 difference between rows with valid dates and obtain the results displayed in D_Date2? </p>
  
  <p>In words it is: BY ID, skip a missing date value in Date2, read the next valid date under it, subtract the earlier date from the later, and write the difference as D_Date2 to the row that has the valid Date2 value. Thanks.</p>
  
  <pre><code>Obs ID  Date1      Date2        D_Date2
  1   1   20090815   20090818       .
  2   1   20090815   20090818       0
  3   1   20090816   20090820       2
  4   1   20090816          .       .
  5   1   20090816   20090820       0
  6   2   20090101          .       .
  7   2   20090105   20090105       .
  8   2   20090105          .       .
  9   2   20090105   20090106       1
  10  2   20090105   20090110       4
  11  3   20080720          .       .
  12  3   20080720   20080917       .
  13  3   20080720   20080918       1
  14  3   20081010          .       .
  15  3   20081010   20080925       7
  16  3   20081010   20080925       0
  </code></pre>