Convert data back to long form after using reshape wide
reshape long
Convert data back to wide form after using reshape long
reshape wide
List problem observations when reshape fails
reshape error
options Description
----------------------------------------------------------------------------------------------------------------------------------------------------
* i(varlist) use varlist as the ID variables
j(varname [values]) long->wide: varname, existing variable
wide->long: varname, new variable
optionally specify values to subset varname
string varname is a string variable (default is numeric)
----------------------------------------------------------------------------------------------------------------------------------------------------
* i(varlist) is required.
where values is #[-#] [...] if varname is numeric
(default)
"string" ["string" ...] if varname is string
and where stubnames are variable names (long->wide), or stubs of variable names (wide->long), and either way, may contain @, denoting where j
appears or is to appear in the name.
In the example above, when we wrote "reshape wide stub", we could have written "reshape wide stub@" because j by default ends up as a suffix.
Had we written stu@b, then the wide variables would have been named stu1b and stu2b.
Advanced syntax
reshape i varlist
reshape j varname [values] [, string]
reshape xij fvarnames [, atwl(chars)]
reshape xi [varlist]
reshape [query]
reshape clear
Menu
Data > Create or change data > Other variable-transformation commands > Convert data between wide and long
Description
reshape converts data from wide to long form and vice versa.
Options
i(varlist) specifies the variables whose unique values denote a logical observation. i() is required.
j(varname [values]) specifies the variable whose unique values denote a subobservation. values lists the unique values to be used from varname,
which typically are not explicitly stated because reshape will determine them automatically from the data.
string specifies that j() may contain string values.
atwl(chars), available only with the advanced syntax and not shown in the dialog box, specifies that chars be substituted for the @ character when
converting the data from wide to long form.
Remarks
Before using reshape, you need to determine whether the data are in long or wide form. You also must determine the logical observation (i) and the
subobservation (j) by which to organize the data. Suppose that you had the following data, which could be organized in wide or long form as follows:
(wide form)
i ....... x_ij ........
id sex inc80 inc81 inc82
-------------------------------
1 0 5000 5500 6000
2 1 2000 2200 3300
3 0 3000 2000 1000
Convert the data from long-long form to wide-wide form (two j variables)
. reshape wide @inc, i(hid year) j(sex) string
. reshape wide minc finc, i(hid) j(year)
List the result
. list
Convert the data back from wide-wide form to long-long form
. reshape long minc finc, i(hid) j(year)
. reshape long @inc, i(hid year) j(sex) string
List the result
. list
-------------------------------------------------------------------------------------------------------------------------------------------------------