title: Conditional Merging in SAS
body: <p><strong><em>Hi all i have the below two datasets in which i need to map Date1 with Date2 in a range of (+/- 7)days within an ID.</em></strong></p>
  
  <pre><code>    Data set1;
      input ID  Date1 ddmmyy8.;
      Format Date1 Date11.;
      Datalines;
      001 02-08-15
      001 04-08-15
      001 06-08-15
      002 11-09-15
      002 14-09-15
      002 17-09-15
      ;
      run;
  
      Data set2;
      input ID TYPE $ Date2 ddmmyy8.;
      Format Date2 Date11.;
      Datalines;
      001 TYPE1 02-08-15
      001 TYPE2 11-08-15
      001 TYPE3 06-08-15
      002 TYPE1 07-09-15
      002 TYPE2 04-09-15
      002 TYPE3 09-08-15
      ;
      run;
  
  
      Proc sql;
  create table out as select a.ID, a.Date1, b.Date2,
  intck('days', Date1, Date2) as Diff
  from set1 as a full join set2 as b
  on a.ID = b.ID and (Date1 + 7 &gt;= Date2 &gt;= Date1 - 7)
  group by a.ID, Date1 having diff = min(diff);
  quit;
  </code></pre>
  
  <p><em>I get the below output</em>
  <a href="https://i.stack.imgur.com/c8pbd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/c8pbd.png" alt="enter image description here"></a></p>
  
  <p><em>i need output</em>
  <a href="https://i.stack.imgur.com/jsRRp.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jsRRp.jpg" alt="enter image description here"></a></p>
  
  <p><em>Expected Output</em></p>
  
  <p><a href="https://i.stack.imgur.com/AGFNm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AGFNm.png" alt="enter image description here"></a></p>
  
  <p>***The output i get is highlighted in yellow when i map using min of Diff.
  but the output i need is highlighted in green it is because i have to maintain the values in Date2 as distinct and does not repeat.</p>
  
  <p>(i.e) Because 02-Aug-2015 is already mapped with 02-Aug-2015 as well as 09-Aug-2015 mapped with 09-Aug-2015 of Date1 i need the 04-Aug-2015 of Date1 to be mapped with the remaining 11-Aug-2015***</p>