proc sql left merge on the most recent date for each row

23 views Asked by At

I am trying to make this code work but for some reason it gives me a lot of duplicates. Table1 has 75k records and I would like to get data from Table2 against each row. Please help me:

proc sql;
create table table3 as
select a.*, b.* 
from table1 as a
left join table2 as b on a.username=b.username and datepart(a.date)>=datepart(b.date)
group by a.username, a.id, b.date
having b.date = max(b.date);
quit;
1

There are 1 answers

0
siddop3 On

If I add this code below the above code then I get the desired result but its not an efficient way of doing this. Ideally the desired results should come from the merge code.

proc sort data=table3 out=table4;
by username id descending a_date descending b_date;
run;
proc sort data = table4 nodupkey out=table5;
by username id a_date b_date;
run;