Stata Reshape

The reshape command is used to translate a dataset between long and wide formats.


Usage

Long to Wide

reshape wide VARSTUB, i(KEYVAR) j(GROUPVAR)

A series of variables named like VARSTUB* will be created for each group of GROUPVAR.

If GROUPVAR is a string variable, the string option is mandatory.

Wide to Long

To translate into a long format, try:

reshape long VARSTUB, i(KEYVAR) j(GROUPVAR)

The variable VARSTUB will be created from the variable list VARSTUB*, and the variable GROUPVAR will be created to indicate the source of VARSTUB. If the string option is specified, GROUPVAR will be a created as a string variable.

If the variable list does not follow the simple pattern of VARSTUB*, it may be possible to specify the location of the group indicator with an at sign (@). For example, if the target variables are inc1r, inc2r, and inc3r, then the command reshape long inc@r, i(KEYVAR) j(GROUPVAR) would correctly create the variable incr.


Reshape Internals

If a reshape failed, the reshape errors command can be used to describe what caused the issue.

A dataset can be re-translated back to the original format with a subsequent reshape long or reshape wide. The specifications used in the first reshape command are remembered and re-applied.


Created Variable Names

When translating data from long to wide formats, variables are created with the group identifier appended to the original variable's name.

If the group indicator should be placed otherwise, use a single at sign (@) to indicate the placement. For example, if the groups are 1, 2, and 3, then the command reshape wide inc@r, i(KEYVAR) j(GROUPVAR) would create variables inc1r, inc2r, and inc3r.

It is often more reliable to separately program the renaming logic.


See also

Stata manual for reshape


CategoryRicottone

Stata/Reshape (last edited 2023-06-08 01:14:18 by DominicRicottone)