# Tag Archives: Stata

## Reshape data in Stata – An easy to understand tutorial

Category:Blog,Uncategorized Tags :

### From wide to long format

Suppose we have the data in the following format

`````` +-------------------------------------------------------+
| id   sex   inc80   inc81   inc82   ue80   ue81   ue82 |
|-------------------------------------------------------|
|  1     0    5000    5500    6000      0      1      0 |
|  2     1    2000    2200    3300      1      0      0 |
|  3     0    3000    2000    1000      0      0      1 |
+-------------------------------------------------------+``````

The above structure is known as the wide format. If we wish to convert it to a long format, such as the one given below,

`` +-----------------------------+  `` | id   year   sex    inc   ue |  `` |-----------------------------|` |  1     80     0   5000    0 | |  1     81     0   5500    1 | |  1     82     0   6000    0 | |  2     80     1   2000    1 | |  2     81     1   2200    0 | |-----------------------------| |  2     82     1   3300    0 | |  3     80     0   3000    0 | |  3     81     0   2000    0 | |  3     82     0   1000    1 |  +-----------------------------+ `

We shall just type

`reshape long inc ue, i(id) j(year)`

### Explanation

Since we need to convert the data from a wide format to a long format, this is why the command that we wrote was reshape long. After that, we have to specify the names of the variables which are in the wide format. In our dataset, there are 2 variables which are INC and UE. Both of these variables have a numeric part. That numeric part is what we call the variable J. We specify this J variable in the option j(new variable). In our dataset, there is no variable with the name year, however, we wrote the option j(year) so that a new variable is created for the numeric values of 80, 81, and 82. We also specified option i(id), where option i needs an existing variable that is a unique panel identifier.

To practice the above yourself, here is the source data and code.

`use http://www.stata-press.com/data/r15/reshape1listreshape long inc ue, i(id) j(year)list`

### Reshape long to wide

Continuing from the previous example, we can reshape the data back to wide format by

`reshape wide inc ue, i(id) j(year)`

email-subscribers-form id=”{form-id}”