Scripting Guide > Data Tables > Advanced Data Table Scripting > Stack Values in a Data Table
Publication date: 05/24/2021

Stack Values in a Data Table

Stack() combines values from several columns into one column.

`dt << Stack(`
`	Columns ( columns ),  // the columns to stack together`
`	Source Label Column ( "name" ), // to identify source columns`
`	Stacked Data Column ( "name" ), // name for the new stacked column`
`	Number of Series( n ), // stacks selected columns into two or more columns`
`	Contiguous, // specifies that the series consists of adjacent columns`
`	Keep ( columns ), // the columns to keep in the data table`
`	Drop ( columns ), // the columns to drop in the data table`
`	Output Table ( "name" ), // the name for the new data table`
`	Columns( columns ) ), // specify which columns to include in the stacked table`
`	Copy Formula( Boolean ), // copy the formula into the stacked column`
`	Drop All Other Columns( Boolean ), // omit the non-stacked columns from the new table`
`	"non-stacked columns"n )( Keep( col1, ... ) // the non-stacked columns to keep`
`	"non-stacked columns"n )( Drop( col1, ... ) // the non-stacked columns to omit`
`	Suppress Formula Evaluation( Boolean ) // do not evaluate the formula`

For example, the following script stacks the weight and height columns in Big Class.jmp:

`dt = Open( "\$SAMPLE_DATA/Big Class.jmp" );`
`StackedDt = dt << Stack(`
`    Columns( :weight, :height ),`
`    Source Label Column( "ID" ),`
`    Stacked Data Column( "Y" ),`
`	 "Non-stacked columns"n )( Keep( :age, :sex ) ),`
`    Output Table( "Stacked Table" )`
`);`

The Columns(columns) argument can take a list of columns, or an expression that evaluates to a list.

You can also stack multiple sets of columns into multiple columns.

`dt = Open( "\$SAMPLE_DATA/Blood Pressure.jmp" );`
`dt << Stack(`
`	Columns(`
`		:BP 8M,`
`		:BP 12M,`
`		:BP 6M,`
`		:BP 8W,`
`		:BP 12W,`
`		:BP 6W,`
`		:BP 8F,`
`		:BP 12F,`
`		:BP 6F`
`	),`
`	Stacked Data Column( "BP" ),`
`	Source Label Column( "Day" ),`
`	Number of Series( 3 ), // number of sets in each series`
`	Contiguous, /* If you want to stack vertically. Otherwise, omit the argument. */`
` `
`);`