Stata 15 help for stack

[D] stack -- Stack data

Syntax

stack varlist [if] [in] , {into(newvars)|group(#)} [options]

options Description ------------------------------------------------------------------------- Main * into(newvars) identify names of new variables to be created * group(#) stack # groups of variables in varlist clear clear dataset from memory wide keep variables in varlist that are not specified in newvars ------------------------------------------------------------------------- * Either into(newvars) or group(#) is required.

Menu

Data > Create or change data > Other variable-transformation commands > Stack data

Description

stack stacks the variables in varlist vertically, resulting in a dataset with variables newvars and _N*(Nv/Nn) observations, where Nv is the number of variables in varlist and Nn is the number in newvars. stack creates the new variable _stack identifying the groups.

Options

+------+ ----+ Main +-------------------------------------------------------------

into(newvars) identifies the names of the new variables to be created. into() may be specified using variable ranges (for example, into(v1-v3)). Either into() or group(), but not both, must be specified.

group(#) specifies the number of groups of variables in varlist to be stacked. The created variables will be named according to the first group in varlist. Either group() or into(), but not both, must be specified.

clear indicates that it is okay to clear the dataset in memory. If you do not specify this option, you will be asked to confirm your intentions.

wide includes any of the original variables in varlist that are not specified in newvars in the resulting data.

Remarks

This command is best understood by examples. Consider

. webuse stackxmpl . stack a b c d, into(e f) clear

This would create a new dataset containing

. list +----------------+ | _stack e f | |----------------| 1. | 1 1 2 | 2. | 1 5 6 | 3. | 2 3 4 | 4. | 2 7 8 | +----------------+

We formed the new variable e by stacking a and c, and we formed the new variable f by stacking b and d. _stack is automatically created and set equal to 1 for the first (a, b) group and equal to 2 for the second (c, d) group.

The number of variables specified by into() determine the number of groups formed. into() may be specified with variable ranges, such as

. stack a b c d, into(v1-v2)

as, of course, may the varlist

. stack a-d, into(v1-v2)

The new variables formed may have the existing variables' names;

. stack a b c d, into(a b)

makes perfect sense.

When you want the new variables to have the same names as the variables in the first group, rather than specifying into(), you may specify group(). Equivalent to the above command is

. stack a b c d, group(2)

For instance, the latter command creates

. list +----------------+ | _stack a b | |----------------| 1. | 1 1 2 | 2. | 1 5 6 | 3. | 2 3 4 | 4. | 2 7 8 | +----------------+

Examples

--------------------------------------------------------------------------- Setup . webuse stackxmpl

List the original data . list a b c d 1. 1 2 3 4 2. 5 6 7 8

Form e by stacking a and c and form f by stacking b and d . stack a b c d, into(e f) clear

List the results . list _stack e f 1. 1 1 2 2. 1 5 6 3. 2 3 4 4. 2 7 8

_stack is automatically created and set equal to 1 for the first (a,b) group and 2 for the second (c,d) group.

--------------------------------------------------------------------------- Setup . webuse stackxmpl, clear

Stack a on a and call it a and stack b on c and call it bc . stack a b a c, into(a bc) clear

List the results . list

--------------------------------------------------------------------------- Setup . webuse stackxmpl, clear

Form e by stacking a and c, form f by stacking b and d, and keep original variables a, b, c, and d . stack a b c d, into(e f) clear wide

List the results . list

--------------------------------------------------------------------------- Setup . webuse stackxmpl, clear

Stack a on a and call it a, stack b on c and call it bc, and retain original variables (a will contain stacked values because a is specified in into()) . stack a b a c, into(a bc) clear wide

List the results . list ---------------------------------------------------------------------------


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index