Hi Jim,
Thank you for the script; I will likely use it in my implementation. I added a couple of lines to account for cases where the values in dt1 are outside the values from dt2.
Names Default To Here( 1 );
// Open a sample data table
dt = Open( "$SAMPLE_DATA/semiconductor capability.jmp" );
// Create a couple of data tables to show how to do lookups
// to multiple tables
dt1 = dt << subset( selected rows( 0 ), columns( npn1 ) );
dt2 = dt << subset( selected rows( 0 ), columns( npn2 ) );
Close( dt, nosave );
// Sort the table to find the closest values from
dt2 << sort( by( NPN2 ), replace table( 1 ) );
// Create a column in the first table to save the row number
// from the second table that is closest to it
dt1 << New Column( "Row Number Closest" );
// Create a column that has the row numbers in the second
// data table, so it can be used to join with the first table
dt2 << New Column( "Row Number", values( 1 :: N Rows( dt2 ) ) );
// Find the value from the second table that is the closest to the
// value from the first data table
For( i = 1, i <= N Rows( dt1 ), i++,
TheSelected = dt2 << get rows where( dt2:NPN2 > dt1:NPN1[i] );
If( N Rows( TheSelected ) == 0,
dt1:Row Number Closest[i] = N Rows( dt2 ), // Added for case: TheSelected is empty
If( TheSelected[1] > 1,
If( dt2:NPN2[Theselected[1]] - dt1:NPN1[i] > dt1:NPN1[i] - dt2:NPN2[Theselected[1] - 1],
dt1:Row Number Closest[i] = Theselected[1],
dt1:Row Number Closest[i] = Theselected[1] - 1
),
dt1:Row Number Closest[i] = Theselected[1]; // Added for case: TheSelected[1] == 1
)
);
);
// Join the tables based upon the closest value found
dtjoined = dt1 << Join(
With( dt2 ),
OUtput Table Name( "Closest Values Joined" ),
By Matching Columns( :Row Number Closest = :Row Number ),
Drop multiples( 0, 0 ),
Include Nonmatches( 1, 0 ),
Preserve main table order( 1 )
);