My read of the initial input is that more than 1 column on a given row may have a value of 1. If that is not the case, the following 2 scripts are not the method to use.
Here is one way to do this
Names Default To Here( 1 );
// Create a sample data table
dt = New Table( "Example",
Add Rows( 2 ),
New Column( "IMCO", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC1", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IIMC2", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC3", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC4", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 0] ) ),
New Column( "IMC5", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC6", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 1] ), Set Display Width( 46 ) ),
New Column( "IMC7", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC8", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC9", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC10", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC11", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "RESIDUAL", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [32.3201037, 20.54947] ) ),
New Column( "TEST", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [., .] ) )
);
// Generate a list of columns to be processed
colNamesList = dt << get column names(string);
remove from (colNamesList,13,2);
// Find the maximum( the ones ) for the columns in question
dtSum = dt<< Summary(
Max(colNamesList),
Freq( "None" ),
Weight( "None" ),
statistics column name format( "column" ),
Link to original data table( 0 )
);
// Stack all of the summarized columns
dtStack = dtSum << Stack(
columns(
colNamesList
),
Source Label Column( "IMC" ),
Stacked Data Column( "Data" )
);
close( dtSum, nosave );
// Delete all data that do not have 1
dtStack << select where(:Data !=1 );
dtStack << delete rows;
// Delete the unwanted columns from the summary table
dtStack << delete columns( {N Rows, data} );
// If there are more rows than in the original table, add rows
if( n rows( dt ) < N Rows( dtStack ),
dt << add rows( N Rows( dtStack ) - N Rows( dt ) )
);
// Add the column names for the columns found with 1's in their data
dt << Update( With( dtStack ) );
close( dtStack, nosave );
Here is a second way to do it: In this example more than one column on a given row has a value of 1
Names Default To Here( 1 );
// Create a sample data table
dt = New Table( "Example",
Add Rows( 2 ),
New Column( "IMCO", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC1", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 0] ) ),
New Column( "IIMC2", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC3", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 1] ) ),
New Column( "IMC4", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 0] ) ),
New Column( "IMC5", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC6", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 1] ), Set Display Width( 46 ) ),
New Column( "IMC7", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC8", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC9", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC10", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "IMC11", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 0] ) ),
New Column( "RESIDUAL", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [32.3201037, 20.54947] ) ),
New Column( "TEST", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [., .] ) ),
New Column( "IMC", Character, "Nominal", Set Values( {"IMC1", "IMC3"} ) )
);
// Generate a list of columns to be processed
colNamesList = dt << get column names( string );
Remove From( colNamesList, 13, 2 );
foundNamesList = {};
For( i = 1, i <= N Items( colNamesList ), i++,
theRows = dt << get rows where( As Column( colNamesList[i] ) == 1 );
If( N Rows( theRows ) > 0,
Insert Into( foundNamesList, colNamesList[i] )
);
);
dt << New Column( "IMC", character );
// If there are more rows than in the original table, add rows
If( N Rows( dt ) < N Items( foundNamesList ),
dt << add rows( N Items( foundNamesList ) - N Rows( dt ) )
);
If( N Items( foundNamesList ) > 0,
dt:IMC << set values( foundNamesList )
);
Jim