= Stata Reshape = '''`-reshape-`''' transforms a dataset between long and wide formats. See [[Stata/AggregatingData|here]] for other similar commands. <> ---- == 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`. ---- == Internals == If `-reshape-` fails, try running `reshape errors` to describe the issue. A dataset that was transformed by `-reshape-` can be re-transformed to the original format with a subsequent `-reshape-`, as long as both occur in the same session. ---- == 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 == [[https://www.stata.com/manuals/dreshape.pdf|Stata manual for -reshape-]] ---- CategoryRicottone