It was the Column Name() function that was messing the script up. It points to the current active data table. Thus, I added the setting of the Current Data Table back to the original Sample data table.
names default to here(1);
tempStack = {};
GroupNames = {"abc", "def"};
for (i = 1, i <= nitems(GroupNames), i++,
Current data table(data table("Sample"));
for (j = 1, j <= ncols(Data Table("Sample")), j++,
if ((contains(Column Name(j), GroupNames[i])),
Insert Into(tempStack, Column Name(j));
);
);
colNum = ncols(Data Table("Sample"));
tempTable = Data Table("Sample") << Stack(
Columns(tempStack),
Source Label Column("Label"),
Stacked Data Column("Data"),
"Non-stacked columns"n (Keep(:tst_temp, :stdf_file)),
Invisible(1)
);
tempStack = {};
tempTable << set name(char(GroupNames[i]) || " Stacked");
);
Using a variable as a pointer to a data table is sometimes very convenient. In the script below, the variable dt is set to the Current Data table and then used in the script to make sure the code is using the correct table.
Names Default To Here( 1 );
tempStack = {};
GroupNames = {"abc", "def"};
dt = Current Data Table();
For( i = 1, i <= N Items( GroupNames ), i++,
For( j = 1, j <= N Cols( dt ), j++,
If( (Contains( Column( dt, j ) << get name, GroupNames[i] )),
Insert Into( tempStack, Column( dt, j ) << get name )
)
);
colNum = N Cols( dt );
tempTable = dt << Stack(
Columns( tempStack ),
Source Label Column( "Label" ),
Stacked Data Column( "Data" ),
"Non-stacked columns"n( Keep( :tst_temp, :stdf_file ) ),
Invisible( 1 )
);
tempStack = {};
tempTable << set name( Char( GroupNames[i] ) || " Stacked" );
);
Jim