<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Need help to find the nearest/closest datetime in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-help-to-find-the-nearest-closest-datetime/m-p/500684#M133368</link>
    <description>&lt;P&gt;Depending on the whole story you may think of matching by date, neglecting the time,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Otherwise, should the 2nd table datetime always be later than the master table datetime?&lt;/P&gt;
&lt;P&gt;If positive you may think of merging data on 1st step and later combine data of two observations.&lt;/P&gt;</description>
    <pubDate>Tue, 02 Oct 2018 11:02:36 GMT</pubDate>
    <dc:creator>Shmuel</dc:creator>
    <dc:date>2018-10-02T11:02:36Z</dc:date>
    <item>
      <title>Need help to find the nearest/closest datetime</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-to-find-the-nearest-closest-datetime/m-p/500662#M133361</link>
      <description>&lt;P&gt;Hi, I am trying to think of a way to match the closest/nearest datetime in 2 different tables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First, I will have a master table where it has a datetime column. In my second table, it has a datetime column aswell. However, datetime in Master table and 2nd table NOT NECESSARILY have the same value of datetime that tie to the same ID.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kindly refer to the code below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;	data master;	
		
	informat master_datetime datetime19.;	
		
	format master_date datetime19.;	
		
	input ID master_datetime age ;	
		
	cards;	
		
1	1/11/11 12:00	11
		
2	12/15/11 13:00	11
		
3	12/16/11 3:30	33
		
4	1/1/12 0:00	55
		
5	1/16/12 4:50	77










data second;		
		
	informat secondary_datetime datetime19.;	
		
  	format secondary_datetime datetime19.;	
		
	input ID secondary_datetime yearsintown ;	
		
	cards;	
		
1	1/11/11 12:30	11
		
1	11/1/11 13:00	22
		
2	12/15/11 13:05	33
		
2	12/15/11 12:51	55
		
2	12/15/11 13:03	77

&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want to achieve is that, with the same ID match in 2 tables, only produce the result set that has the closest "secondary_datetime" to "master_datetime".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;By looking at the data above, I am only expecting to get the following result:&lt;/P&gt;&lt;PRE&gt;ID    master_datetime  Age       secondary_datetime yearsintown
1	1/11/11 12:00	    11		1/11/11 12:30	          11
2      12/15/11 13:00	    11        12/15/11 13:03	          77

 &lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If i were to get the exact datetime match, it has no issue at all. But for now, i need to find the datetime in 2nd table that is closest to the master table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone help me with that?&lt;/P&gt;</description>
      <pubDate>Tue, 02 Oct 2018 10:37:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-to-find-the-nearest-closest-datetime/m-p/500662#M133361</guid>
      <dc:creator>WorkingMan</dc:creator>
      <dc:date>2018-10-02T10:37:30Z</dc:date>
    </item>
    <item>
      <title>Re: Need help to find the nearest/closest datetime</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-to-find-the-nearest-closest-datetime/m-p/500681#M133367</link>
      <description>&lt;P&gt;I would merge the data into one dataset, by ID, so that you have all the records in one place, then do date-date to get a difference in datetimes, ensuring to abs() the resulting diff so previous dates are considered.&amp;nbsp; Then its simply a matter of getting the min(diff).&amp;nbsp; I will try to do some code, but I have a meeting shortly, the logic above should get you there though.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Oct 2018 10:55:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-to-find-the-nearest-closest-datetime/m-p/500681#M133367</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-10-02T10:55:11Z</dc:date>
    </item>
    <item>
      <title>Re: Need help to find the nearest/closest datetime</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-to-find-the-nearest-closest-datetime/m-p/500684#M133368</link>
      <description>&lt;P&gt;Depending on the whole story you may think of matching by date, neglecting the time,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Otherwise, should the 2nd table datetime always be later than the master table datetime?&lt;/P&gt;
&lt;P&gt;If positive you may think of merging data on 1st step and later combine data of two observations.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Oct 2018 11:02:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-to-find-the-nearest-closest-datetime/m-p/500684#M133368</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2018-10-02T11:02:36Z</dc:date>
    </item>
    <item>
      <title>Re: Need help to find the nearest/closest datetime</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-to-find-the-nearest-closest-datetime/m-p/500765#M133393</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;I would like to see the sample code when you are convenient sir. Good luck to your meeting.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;The secondary_date may be earlier or may be later than the datetime in table A.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Oct 2018 14:31:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-to-find-the-nearest-closest-datetime/m-p/500765#M133393</guid>
      <dc:creator>WorkingMan</dc:creator>
      <dc:date>2018-10-02T14:31:29Z</dc:date>
    </item>
    <item>
      <title>Re: Need help to find the nearest/closest datetime</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-to-find-the-nearest-closest-datetime/m-p/500782#M133395</link>
      <description>&lt;P&gt;Here is an example, please test and post working test data in future:&lt;/P&gt;
&lt;PRE&gt;data master;  
  informat master_datetime datetime19.;        
  format master_datetime datetime19.;         
  id=1 ;
  master_datetime="01NOV2011 12:00"dt;
  age=11;
run;

data second;            
  informat secondary_datetime datetime19.;   
  format secondary_datetime datetime19.;         
  id=1;
  secondary_datetime="01NOV2011 12:30"dt;
  age=11;
  output;
  id=1;
  secondary_datetime="11NOV2011 13:00"dt;
  age=22;
  output;
run;

proc sql;
  create table tot as
  select a.id,
         a.master_datetime,
         a.age,
         b.secondary_datetime,
         abs(b.secondary_datetime - a.master_datetime) as diff
  from   master a
  left join second b
  on     a.id=b.id;
  create table want as
  select *
  from   tot
  group by id
  having diff=min(diff);
quit;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Oct 2018 14:52:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-to-find-the-nearest-closest-datetime/m-p/500782#M133395</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-10-02T14:52:01Z</dc:date>
    </item>
  </channel>
</rss>

