Check if below code helps:
proc sort data=dat1 out=dat1_1;
by id line;
run;
proc sql;
create table ab as
select distinct a.*, coalesce(b.ordering,d.ordering) fromat=mmddyy10. as ordering
from dat1 a
left join dat2 b
on a.ID=b.ID and LOSTART<=Ordering<=LOEND
left join dat1 c
on a.ID=c.ID and a.line = c.line - 1
left join dat2 d
on c.ID=d.ID and a.LOEND<=d.Ordering<=c.LOSTART
order by a.ID;
quit;
Output:
ID
LINE
LOSTART
LOEND
ordering
1
1
12/1/2015
4/1/2016
.
1
2
4/5/2016
8/16/2016
7/16/2016
1
3
8/23/2016
11/14/2016
10/2/2017
1
4
11/27/2017
1/17/2018
.
2
1
6/4/2016
9/18/2016
7/18/2016
2
2
9/19/2016
11/11/2016
.
3
2
5/6/2016
7/17/2016
7/18/2016
3
3
7/18/2016
10/3/2016
7/18/2016
4
1
11/2/2015
5/30/2016
.
4
2
5/31/2016
12/28/2016
7/26/2016
4
3
4/8/2018
1/9/2019
.
5
1
4/3/2015
6/4/2015
.
5
2
6/5/2015
12/27/2015
7/31/2016
5
3
8/5/2016
9/7/2016
.
6
1
10/12/2015
6/2/2016
.
6
2
6/3/2016
8/27/2016
7/31/2016
6
4
11/7/2016
4/20/2017
.
7
1
6/5/2016
6/15/2016
.
... View more