Say I have a datatable with a list of id numbers and date-times. Each id may appear more than once with a different date-time (example data in table 1) I want to add a column to table 1 with a date from a second table matched on id number, which finds the closest date in table 2 occurring after the date in table 1.
Again there may be multiple dates for the same id number in table 2 so I just want to add the nearest next data. I figure I need to write a for loop but can't work out the way to run a match for each id number then select just 1 result to display for the date column. The other condition I need to add is that if there is no date in table 2 before the next date for the same id in table 1 , it should just return NA.
What would be the best way to proceed? Thanks in advance all
Table1
id_code inspection_date
1 600 2019-10-10 18:24:32
2 600 2019-10-10 23:55:13
3 600 2019-08-07 13:42:45
4 601 2019-08-16 15:45:54
5 601 2019-08-17 17:25:34
6 602 2019-08-19 12:34:31
7 603 2019-11-03 16:30:31
8 603 2019-11-03 19:01:01
Table 2
id_code2 confirm_date
1 598 2019-09-09 13:24:45
2 600 2019-10-10 19:35:37
3 600 2019-10-11 01:23:58
4 600 2019-08-07 16:30:01
5 601 2019-08-17 02:30:35
6 601 2019-08-17 22:45:46
7 601 2019-08-19 19:12:18
8 602 2019-12-01 12:12:12
9 602 2019-12-14 23:25:35
10 602 2019-12-29 03:30:31
11 603 2019-12-30 06:35:35
12 603 2019-12-31 01:02:34
13 605 2019-12-31 17:24:46
Blockquote