proc sql noprint;
create table result
as select * from (
select distinct a.id,b.id as id_match,a.old_value,b.old_value as new_value
from test as a,test as b
where b.old_value ne .
group by a.id
having a.id=b.id or (abs(a.id-b.id)=min(abs(a.id-b.id)) ))
...