You should use N Items(col_names) instead of N Items(dt) in the for loop.
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
col_names = {"age", "sex", "weight"};
for( i = 1, i <= N Items(col_names), i++,
Column( dt, col_names[i] ) << set selected;
);
dt << Invert Column Selection;
Also you can make the code simpler by using Select Columns
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
col_names = {"age", "sex", "weight"};
wait(1); // for demo purposes
dt << Select Columns(col_names);
wait(1); // for demo purposes
dt << Invert Column Selection;
You can also avoid inverting by selecting correct columns directly, but it is most likely more difficult to understand
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
col_names = Associative Array({"age", "sex", "weight"});
all_col_names = Associative Array(dt << Get Column Names("String"));
all_col_names << Remove(col_names);
dt << Select Columns(all_col_names << get keys);
-Jarmo