Here is a completely different approach. It uses the Join capabilities in JMP rather than attempting to do everything with loops and comparisons. I believe it is going what you want, but please verify the results.
Names Default To Here( 1 );
dt_subset = Data Table( "subset table" );
col_names = dt_subset << get column names( string,continuous );
dt_group = Data Table( "dt_groups" );
// Expand dt_group:ProductGroups into separate columns
dt_group << text to columns( delimiter( "," ), columns( :ProductGroups ) );
// Stack the resulting columns
dt_group_col_names = dt_group << get column names( string );
// Remove the first 3 columns from the list
Remove From( dt_group_col_names, 1, 3 );
// Stack data table
// → Data Table( "Stacked dt_groups" )
dt_stack_group = dt_group << Stack(
columns( dt_group_col_names ),
Source Label Column( "Label" ),
Stacked Data Column( "Product" ),
"Non-stacked columns"n( Keep( :Tests, :Limits ) ),
Output Table( "Stacked dt_groups" )
);
// Get rid of the Label column
dt_stack_group << delete columns( :Label );
// Stack the subset table
// Add a RowNum column to the table to preserve the order
dt_subset << New Column( "RowNum", set each value( Row() ) );
dt_stack_subset = dt_subset << Stack(
columns(
col_names
),
Source Label Column( "Tests" ),
Stacked Data Column( "Data" ),
Name("Non-Stacked columns")(Keep({Product, RowNum})),
Output Table( "Stack of subset table" )
);
// Join the 2 stacked tables
dt_joined = dt_stack_subset << Join(
With( dt_stack_group ),
Merge Same Name Columns,
Match Flag( 0 ),
By Matching Columns( :Product = :Product, :Tests = :Tests ),
Drop multiples( 0, 0 ),
Include Nonmatches( 1, 0 ),
Preserve main table order( 1 ),
Output Table( "Joined stack data" )
);
// Split the table back to original form
dt_final = dt_joined << Split(
Split By( :Tests ),
Split( :Data, :Limits ),
Group( :RowNum ),
Output Table( "Final" ),
Sort by Column Property
);
// Cleanup and rearrange the Final data table
dt_final << delete columns( :RowNum );
// Rename the columns
For( i = 2, i <= N Cols( dt_final ), i++,
theName = Column( dt_final, i ) << get name;
If(
Starts With( theName, "Data" ), Column( dt_final, i ) << set name( Substr( theName, 6 ) ),
Starts With( theName, "Limits" ),
Column( dt_final, i ) << set name( Substr( theName, 8 ) || "_LIMITS" )
);
);
// Reorder the columns
For( i = Length( col_names ), i >= 1, i--,
If( Try( Column( dt_final, col_names[i] || "_LIMITS" ) << get name, "" ) != "",
theName = ":\!"" || col_names[i] || "_LIMITS" || "\!"n";
Eval( Parse( "dt_final << Move Selected Columns( " || theName || ",to first);" ) );
//dt_final << Move Selected Columns( :"Test5^0.8_LIMITS"n,to last)
);
theName = ":\!"" || col_names[i] || "\!"n";
Eval( Parse( "dt_final << Move Selected Columns( " || theName || ",to first);" ) );
);
dt_final << move selected columns( :Product, to first );
close( dt_stack_group, nosave);
close( dt_stack_subset, nosave);
close( dt_joined, nosave);
Jim