title: In SAS how to create a 3rd table from other two, based upon certain conditions
body: <p><strong>This is my dataset 1 (t1):</strong></p>
  
  <pre><code>data sample1;
    infile cards truncover expandtabs;
    input MACH $ EVENT $ DATE :date. TIME :time. PMACH $ PXMACH $ SITE $ RAD MILVAL SF;
    format date date8. time HHMM.;
  cards;
  US0001 Lat 02JAN13 19:24 . . . 876 2660403.00000 1
  US0001 Cre 29MAY13 0:00 7611 I1124P 1 . 2734440.00000 1
  US0001 Cre 31JAN14 0:00 7402 I1015P 2 . 2735017.00000 1
  US0001 Lat 12JAN15 7:00 . . . . 2900334.00000 1
  US000323 Lat 13OCT12 19:37 . . . 852.2 1332753.00000 1
  US000323 WI 25OCT12 0:00 . . . . 1342148.00000 1
  US000323 Rem 31OCT12 0:00 7416 I1079P 3 . 1346049.00000 1
  US000323 Lat 31OCT12 14:03 . . . 890.5 1346049.00000 1
  US000323 Inst 11JAN13 0:00 7408 I1034P 3 . 1346049.00000 1
  US000323 Lat 16.marras.13 19:52 . . . 888.7 1417443.00000 1
  US000323 Lat 12OCT13 13:49 . . . 886.7 1606899.00000 1
  US000323 Lat 12OCT13 14:17 . . . 886.7 1606899.00000 1
  US000323 Rem 24MAY14 0:00 7408 I1034P 3 . 1812537.00000 1
  US000328 Lat 31JAN13 23:41    853.2 2040610.00000 1
  US000328 Cre 03FEB13 0:00 7209 I1115P 3 . 2040610.00000 1
  US000328 Lat 17MAY13 12:43 . . . 847.2 2134438.00000 1
  US000328 Lat 17MAY13 12:46 . . . 847.2 2134438.00000 1
  US000328 Lat 02SEP13 15:03 . . . 846.3 2134438.00000 1
  US000328 Cre 18JUL14 0:00 7218 I1152P 3 . 2134438.00000 1
  US000328 Lat 25SEP14 8:54 . . . 842.4 2180863.00000 1
  US000328 Inst 15FEB15 0:00 7508 I1098P 3 . 2180863.00000 1
  US000328 Cre 21MAY15 0:00 7212 I1098P 3 . 2232830.00000 1
  US000328 WI 24JUN15 0:00 . . . . 2232830.00000 1
  US000328 Lat 13JUL15 0:00 . . . 890.2 2232830.00000 1
  ;
  </code></pre>
  
  <p><strong>And this is my dataset 2 (t2):</strong></p>
  
  <pre><code>data sample2;
  infile cards truncover expandtabs;
  input Mach $ Date : date9. Data $;
  format date : date9.;
  cards;
  US0001 28Dec2012 dassssd
  US0001 . 322234
  US0001 30Dec2012 sadasd
  US0001 . 1213
  US0001 01Jan2013 asdsds
  US0001 02Jan2013 zxcxzc
  US0001 03Jan2013 asdad
  US0001 04Jan2013 zxczc
  US0001 . zxcc
  US0001 06Jan2013 zxc
  US0001 07Jan2013 czxcxzc
  US0001 24May2013 zxcwef
  US0001 . x
  US0001 26May2013 dassssd
  US0001 . 322234
  US0001 28May2013 sadasd
  US0001 29May2013 1213
  US0001 30May2013 asdsds
  US0001 . zxcxzc
  US0001 01Jun2013 asdad
  US0001 02Jun2013 zxczc
  US0001 03Jun2013 zxcc
  US0001 . zxc
  US0001 27Jan2014 czxcxzc
  US0001 . zxcwef
  US0001 29Jan2014 x
  US0001 30Jan2014 dassssd
  US0001 31Jan2014 322234
  US0001 01Feb2014 sadasd
  US0001 02Feb2014 1213
  US0001 . asdsds
  US0001 04Feb2014 zxcxzc
  US0001 05Feb2014 asdad
  US0001 07Jan2015 zxczc
  US0001 . zxcc
  US0001 09Jan2015 zxc
  US0001 10Jan2015 czxcxzc
  US0001 11Jan2015 zxcwef
  US0001 12Jan2015 x
  US0001 . dassssd
  US0001 14Jan2015 322234
  US0001 15Jan2015 sadasd
  US0001 . 1213
  US0001 17Jan2015 asdsds
  US000323 08Oct2012 zxcxzc
  US000323 09Oct2012 asdad
  US000323 10Oct2012 zxczc
  US000323 . zxcc
  US000323 12Oct2012 zxc
  US000323 13Oct2012 czxcxzc
  US000323 . zxcwef
  US000323 15Oct2012 x
  US000323 16Oct2012 dassssd
  US000323 17Oct2012 322234
  US000323 18Oct2012 sadasd
  US000323 . 1213
  US000323 21Oct2012 asdsds
  US000323 22Oct2012 zxcxzc
  US000323 . asdad
  US000323 24Oct2012 zxczc
  US000323 25Oct2012 zxcc
  US000323 26Oct2012 zxc
  US000323 27Oct2012 czxcxzc
  US000323 28Oct2012 zxcwef
  US000323 29Oct2012 x
  US000323 30Oct2012 dassssd
  US000323 26Oct2012 322234
  US000323 . sadasd
  US000323 28Oct2012 1213
  US000323 29Oct2012 asdsds
  US000323 30Oct2012 zxcxzc
  US000323 . asdad
  US000323 01Nov2012 zxczc
  US000323 02Nov2012 zxcc
  US000323 03Nov2012 zxc
  US000323 04Nov2012 czxcxzc
  US000323 05Nov2012 zxcwef
  US000323 26Oct2012 x
  US000323 . dassssd
  US000323 28Oct2012 322234
  US000323 29Oct2012 sadasd
  US000323 30Oct2012 1213
  US000323 31Oct2012 asdsds
  US000323 01Nov2012 zxcxzc
  US000323 02Nov2012 asdad
  US000323 03Nov2012 zxczc
  US000323 04Nov2012 zxcc
  US000323 . zxc
  US000323 06Jan2013 czxcxzc
  US000323 07Jan2013 zxcwef
  US000323 08Jan2013 x
  US000323 09Jan2013 dassssd
  US000323 . 322234
  US000323 11Jan2013 sadasd
  US000323 12Jan2013 1213
  US000323 13Jan2013 asdsds
  US000323 14Jan2013 zxcxzc
  US000323 15Jan2013 asdad
  US000323 16Jan2013 zxczc
  US000323 11Mar2013 zxcc
  US000323 12Mar2013 zxc
  US000323 13Mar2013 czxcxzc
  US000323 14Mar2013 zxcwef
  US000323 15Mar2013 x
  US000323 16Mar2013 dassssd
  US000323 17Mar2013 322234
  US000323 18Mar2013 sadasd
  US000323 19Mar2013 1213
  US000323 20Mar2013 asdsds
  US000323 21Mar2013 zxcxzc
  US000323 07Oct2013 asdad
  US000323 08Oct2013 zxczc
  US000323 09Oct2013 zxcc
  US000323 10Oct2013 zxc
  US000323 11Oct2013 czxcxzc
  US000323 12Oct2013 zxcwef
  US000323 13Oct2013 x
  US000323 14Oct2013 dassssd
  US000323 15Oct2013 322234
  US000323 16Oct2013 sadasd
  US000323 17Oct2013 1213
  US000323 07Oct2013 asdsds
  US000323 08Oct2013 zxcxzc
  US000323 09Oct2013 asdad
  US000323 10Oct2013 zxczc
  US000323 11Oct2013 zxcc
  US000323 12Oct2013 zxc
  US000323 13Oct2013 czxcxzc
  US000323 14Oct2013 zxcwef
  US000323 15Oct2013 x
  US000323 16Oct2013 dassssd
  US000323 17Oct2013 322234
  US000323 19May2014 sadasd
  US000323 20May2014 1213
  US000323 21May2014 asdsds
  US000323 22May2014 zxcxzc
  US000323 23May2014 asdad
  US000323 24May2014 zxczc
  US000323 25May2014 zxcc
  US000323 26May2014 zxc
  US000323 27May2014 czxcxzc
  US000323 28May2014 zxcwef
  US000323 29May2014 x
  US000328 26Jan2013 dassssd
  US000328 27Jan2013 322234
  US000328 28Jan2013 sadasd
  US000328 . 1213
  US000328 30Jan2013 asdsds
  US000328 31Jan2013 zxcxzc
  US000328 01Feb2013 asdad
  US000328 02Feb2013 zxczc
  US000328 . zxcc
  US000328 04Feb2013 zxc
  US000328 05Feb2013 czxcxzc
  US000328 29Jan2013 zxcwef
  US000328 30Jan2013 x
  US000328 . dassssd
  US000328 01Feb2013 322234
  US000328 02Feb2013 sadasd
  US000328 03Feb2013 1213
  US000328 04Feb2013 asdsds
  US000328 . zxcxzc
  US000328 06Feb2013 asdad
  US000328 07Feb2013 zxczc
  US000328 08Feb2013 zxcc
  US000328 . zxc
  US000328 13May2013 czxcxzc
  US000328 14May2013 zxcwef
  US000328 15May2013 x
  US000328 16May2013 dassssd
  US000328 . 322234
  US000328 18May2013 sadasd
  US000328 19May2013 1213
  US000328 20May2013 asdsds
  US000328 21May2013 zxcxzc
  US000328 . asdad
  US000328 12May2013 zxczc
  US000328 13May2013 zxcc
  US000328 14May2013 zxc
  US000328 . czxcxzc
  US000328 16May2013 zxcwef
  US000328 17May2013 x
  US000328 18May2013 dassssd
  US000328 19May2013 322234
  US000328 20May2013 sadasd
  US000328 21May2013 1213
  US000328 22May2013 asdsds
  US000328 28Aug2013 zxcxzc
  US000328 29Aug2013 asdad
  US000328 . zxczc
  US000328 31Aug2013 zxcc
  US000328 01Sep2013 zxc
  US000328 02Sep2013 czxcxzc
  US000328 03Sep2013 zxcwef
  US000328 04Sep2013 x
  US000328 . dassssd
  US000328 06Sep2013 322234
  US000328 07Sep2013 sadasd
  US000328 13Jul2014 1213
  US000328 14Jul2014 asdsds
  US000328 15Jul2014 zxcxzc
  US000328 16Jul2014 asdad
  US000328 17Jul2014 zxczc
  US000328 18Jul2014 zxcc
  US000328 19Jul2014 zxc
  US000328 20Jul2014 czxcxzc
  US000328 21Jul2014 zxcwef
  US000328 . sdsfsfqeww
  US000328 23Jul2014 zxcc
  US000328 20Sep2014 zxc
  US000328 21Sep2014 czxcxzc
  US000328 22Sep2014 zxcwef
  US000328 23Sep2014 x
  US000328 24Sep2014 dassssd
  US000328 . 322234
  US000328 26Sep2014 sadasd
  US000328 27Sep2014 1213
  US000328 28Sep2014 asdsds
  US000328 29Sep2014 zxcxzc
  US000328 30Sep2014 asdad
  US000328 . zxczc
  US000328 11Feb2015 zxcc
  US000328 12Feb2015 zxc
  US000328 . czxcxzc
  US000328 14Feb2015 zxcwef
  US000328 15Feb2015 x
  US000328 16Feb2015 dassssd
  US000328 17Feb2015 322234
  US000328 18Feb2015 sadasd
  US000328 19Feb2015 1213
  US000328 . asdsds
  US000328 16May2015 zxcxzc
  US000328 17May2015 asdad
  US000328 18May2015 zxczc
  US000328 19May2015 zxcc
  US000328 20May2015 zxc
  US000328 21May2015 czxcxzc
  US000328 22May2015 zxcwef
  US000328 . x
  US000328 24May2015 dassssd
  US000328 25May2015 322234
  US000328 26May2015 sadasd
  US000328 19Jun2015 1213
  US000328 20Jun2015 asdsds
  US000328 21Jun2015 zxcxzc
  US000328 22Jun2015 asdad
  US000328 23Jun2015 zxczc
  US000328 24Jun2015 zxcc
  US000328 25Jun2015 zxc
  US000328 26Jun2015 czxcxzc
  US000328 . zxcwef
  US000328 28Jun2015 x
  US000328 29Jun2015 dassssd
  US000328 08Jul2015 322234
  US000328 . sadasd
  US000328 10Jul2015 1213
  US000328 11Jul2015 asdsds
  US000328 12Jul2015 zxcxzc
  US000328 . asdad
  US000328 14Jul2015 zxczc
  US000328 15Jul2015 zxcc
  US000328 16Jul2015 zxc
  US000328 17Jul2015 czxcxzc
  US000328 . zxcwef
  ;
  </code></pre>
  
  <p><strong>What I want is:</strong></p>
  
  <ol>
  <li><p>Where   t1.Event = "Lat"    and     t1.Mach = t2.Mach    and      t1.Date = t2.Date</p></li>
  <li><p>Get the  Dates from t2 which are immediate previous and immediate after (when t1.Date = t2.Date as in above point 1). If date in <strong>t2</strong> is BLANK then chose 2nd previous or 2nd after the matched date. (pl refer note below).</p></li>
  <li><p>Combine t1 and t2 into t3 such that the result looks like something as in below:</p></li>
  </ol>
  
  <p>Sample t3:</p>
  
  <pre><code>MACH    EVENT   DATE    TIME    PMACH   PXMACH  SITE    RAD MILVAL  SF  DATA
  US0001   .  01Jan2013    .       .       .       .       .    .     .  asdsds
  US0001  Lat 02Jan2013   19:24    .       .       .      876 2660403 1   . 
  US0001   .  03Jan2013   .        .       .       .       .    .     .   asdad
  US0001   .  07Jan2013    .       .       .       .       .    .     . czxcxzc
  US0001  Lat 12Jan2015   7:00     .       .       .       .  2900334 1   . 
  US0001   .  24May2013    .       .       .       .       .    .     .  zxcwef
  US000323 .  12Oct2012    .       .       .       .       .    .     .   zxc
  US000323 Lat 13Oct2012  19:37    .       .       .      852.2 1332753 1  .   
  US000323 .   15Oct2012   .       .       .       .       .    .     . .  x
  </code></pre>
  
  <p><strong>NOTE:</strong>  for US000323 after 13Oct2012 in t1, the selected date from t2 is 15Oct2012, which is 2nd next date in t2 because immediately after t1.Date=t2.Date (=13Oct2016) in t2 is a BLANK date, so we do not select that one but select the 2nd next date which is 15Oct2016.</p>