1. Home
2. STACK procedure

# STACK procedure

Combines several data sets by “stacking” the corresponding vectors (R.W. Payne).

### Option

`DATASET` = factor Factor to indicate the data set to which each unit originally belonged

### Parameters

`STACKEDVECTOR` = variates, factors or texts New vectors combining the corresponding members of the data sets specified by parameter `V1`, or parameters `V1`–`V100` Pointers defining (all) the components to be stacked into each `STACKEDVECTOR`, or contents of the first data set Data sets 2 – 100 How to match the values of factors (`levels`, `labels`, `ordinals`, `renumbered`); default `leve`

### Description

`STACK` allows you to combine vectors (variates, factors or texts) from several data sets into a single data set. Each vector in the new data set is formed by “stacking” the corresponding vectors from the original data sets. So, the new vector first has all the units from the first data set, then those from the second data set, and so on.

The identifiers of the new vectors are specified by the first parameter, `STACKEDVECTOR`. The original vectors of up to 100 data sets can be specified one data set at a time using the subsequent parameters: `V1`, `V2`, … `V100`. Alternatively, `V1` can specify a list of pointers, each one containing all the vectors that are to be stacked together to form the equivalent `STACKEDVECTOR` (allowing vectors from more than 100 data sets to be specified). So, these two statements would be equivalent

`STACK [DATASET=Month] Rainfall,Temperature;\`

`  V1=MarchRain,MarchTemp; V2=AprilRain,AprilTemp`

and

`STACK [DATASET=Month] Rainfall,Temperature;\`

`  V1=!p(MarchRain,AprilRain),!p(MarchTemp,AprilTemp)`

The vectors in each data set must generally all be of the same length. The exception is that you can specify a scalar instead of a variate of identical values (the number of values is then deduced from the lengths of the corresponding vectors of the other data sets). Likewise you can specify a single-valued text instead of a text with duplicates of that value, and either a scalar or a single-valued text instead of a factor with the same level or label duplicated throughout.

The `FREPRESENTATION` option indicates how the levels are to be matched amongst factors. If this is set to `labels` and the levels of the original factors are compatible (that is if each label corresponds to the same level in all the original factors), then the level definitions are transferred to the new factor; if not, the levels are defined to be the default values 1, 2… and a warning is printed by the `APPEND` procedure which is called by `STACK`. Similarly, with the default setting `levels`, the labels are retained if they are compatible, but no warning is printed if they are not. For the `ordinals` setting, the levels of all the factors are taken as the ordinal values 1, 2… (and no labels are defined). Finally, the `renumbered` setting assumes that the original factors all have independent sets of levels, and renumbers these from one upwards for the first factor, from number of levels of the first factor plus one upwards for the second factor, and so on; the new factor will thus have a different level for every level of the original factors.

The `DATASET` option allows a factor to be formed indicating the number if the data set to which each unit of the stacked vectors originally belonged. This factor could be used in the `DATASET` parameter of the `UNSTACK` procedure subsequently to recover the original vectors.

Option: `DATASET`.

Parameter: `STACKEDVECTOR`, `V1`, `V2`, … `V100`, `FREPRESENTATION`.

### Method

The vectors are stacked together using the `APPEND` procedure.

### Action with `RESTRICT`

Any restrictions on the vectors are ignored.

Directive: `EQUATE`.
Procedures: `APPEND`, `JOIN`, `RESHAPE`, `UNSTACK`, `VEQUATE`.
Commands for: Calculations and manipulation.

### Example

```CAPTION  'STACK example'; STYLE=meta
VARIATE  [NVALUES=31] MarchRain,MarchTemp
2.7 11.7  2.9  7.6  1.7  8.0  4.2  9.4  4.1  3.7
0.2 11.4  2.5  6.3  3.0 11.9  0.3  4.5  0.6 10.4
3.0 11.3  0.3  7.3  4.2 12.0  2.5 10.0  3.9  9.0
1.2  4.6  1.4  5.7  3.7 11.8  2.9 11.9  2.7 10.4
0.9  3.0  4.7 10.4  3.5  6.0  3.4  9.2  3.3  5.6
4.8  7.5  1.9 11.7  0.9 10.3  1.1  3.4  0.2  5.7
1.0  4.1  :
VARIATE  [NVALUES=30] AprilRain,AprilTemp
0.1  6.0  3.2 11.0  0.6  4.4  1.2 11.2  1.7  9.1
1.4  3.6  3.1  8.2  3.0  9.7  0.9  7.3  1.8  3.6
3.7 12.5  3.9  7.9  1.4  9.3  4.6 12.2  0.8  4.6
1.9  5.8  3.1  8.8  3.6 11.5  2.8  8.4  2.8 11.7
0.3  6.1  4.5  4.5  4.2  6.2  1.6 12.5  2.7  5.8
2.7  5.5  2.7  9.6  1.8  5.8  0.4  9.1  0.8  3.6 :
STACK    [DATASET=Month] Rainfall,Temperature;\
V1=MarchRain,MarchTemp; V2=AprilRain,AprilTemp
FACTOR   [MODIFY=yes; LABELS=!t(March,April)] Month
PRINT    Month,Rainfall,Temperature; DECIMALS=0,2(1)
```
Updated on January 12, 2022