![Reshape long stata](https://loka.nahovitsyn.com/110.jpg)
String option to indicate that the suffix is a character. Stata can handle that as long as you use the We would like to make name and inc into long formats but their suffixes are characters (d & m) instead of numbers. use, clear list famid named incd namem incm It also is possible to reshape a wide data file to be long when there are character suffixes. 3 3 2 2.9 31 Example #4: Reshaping data wide to long with character suffixes list famid birth age ht wt famid birth age ht wtġ8. 3 3 2.1 2.9 22 31 reshape long ht wt, i(famid birth) j(age) (note: j = 1 2) List famid birth ht1 ht2 wt1 wt2 famid birth ht1 ht2 wt1 wt2ĩ. reshape long ht wt, i(famid birth) j(age) This is basically the same as the previous command except that Let’s reshape this data into a long format. use, clear list famid birth ht1 ht2 wt1 wt2 famid birth ht1 ht2 wt1 wt2 The file with the kids heights at age 1 and age 2 also contains their weights atĪge 1 and age 2 (called wt1 and wt2). 3 3 2 2.9 Example #3: Reshaping data wide to long list famid birth age ht famid birth age htġ8. 3 3 2.1 2.9 reshape long ht, i(famid birth) j(age) (note: j = 1 2) List famid birth ht1 ht2 famid birth ht1 ht2ĩ. Let’s look at the wide data, and then the data reshaped to be long. Questions, the reshape command will look like this. Q: What do we want to call the variable which contains the suffix of Q: What variable uniquely identifies an observation when it is in theĪ: famid and birth together uniquely identify the Q: What is the stem of the variable going from wide to long. Lets reshape this data into a long format. Use, clear list famid birth ht1 ht2 famid birth ht1 ht2
![reshape long stata reshape long stata](https://www.reed.edu/psychology/stata/gs/tutorials/images/reshapetable2.png)
Year Example #2: Reshaping data wide to longĬonsider the file containing the kids and their heights at 1 year of age (ht1) and at 2 years of age (ht2). J(year) tells reshape that the suffix of faminc (i.e., 96 97 98) should be placed in a variable called I(famid) option tells reshape that famid is the unique identifier for records in their Stata that the stem of the variable to be converted from wide to Long tells reshape that we want to go from wide to long Now let’s look at the pieces of the original reshape command. The reshape long command puts the data back into long format. The reshape wide command puts the data back into wide format. Let’s look at the wide format and contrast it with the long format. Year is represented as its own observation. The list command shows that the data are now in long form, where each Reshape long faminc, i(famid) j(year) (note: j = 96 97 98) The reshape command can accomplish this, as shown below. Long, where each year of data is in a separate observation. This is called a wide format since the years of data are wide. use, clear list famid faminc96 faminc97 faminc98 Example #1: Reshaping data wide to longĬonsider the family income data file below. Show common examples of reshaping data, but do not exhaustively demonstrate theĭifferent kinds of data reshaping that you could encounter. Wide data files and reshape them into long form. View the entire collection of UVA Library StatLab articles.This module illustrates the power (and simplicity) of Stata in its ability to reshape data files. | airline inprog1 i_cnt1 inprog2 i_cnt2 inprog3 i_cnt3 inprog4 i_cnt4 | > reshape wide i_cnt inprog, i(airline) j(time) To convert a dataset from long form to wide, simply use -reshape wide- command instead.Ĭonsider the airacc data, to make a simple example, we only keep 3 variables –
![reshape long stata reshape long stata](https://gtools.readthedocs.io/en/latest/benchmarks/quick.png)
Reshape from long to wide: -reshape wide. > reshape long bp_, i(patient) j(when) string * reshape data, note the string option added at the end * load data and list the first 4 observations
![reshape long stata reshape long stata](https://miro.medium.com/max/1348/0*iyjQxLhaUIJBYcn-.png)
Consider another sample data called reshape2. Sometimes a variable called id does not serve as unique identifier – and that’s one of the reasons we need to be careful when reshaping data. Reshape from wide to long: complex unique identifier * we simply put inc and ue as stubs, then put id and year as i and j as we did in the previous example. In this case, 1) the stub should be inc, which is the variable to be converted from wide to long, 2) i is the id variable, which is the unique identifier of observations in wide form, and 3) j is the year variable that I am going to create – it tells Stata that suffix of inc (i.e., 80, 81, 82) should be put in the variable called year.
![reshape long stata reshape long stata](https://i.stack.imgur.com/3SkW9.png)
The syntax should look like this in general: reshape long stub, i(i) j(j) * let's make the first example simpler by keeping id, sex and the inc variables
![Reshape long stata](https://loka.nahovitsyn.com/110.jpg)