UCLA Academic Technology Services HomeServicesClassesContactJobs
Search

Stata Class Notes 3.0
Reshaping Datasets


1.0 Stata commands in this unit

. reshape

2.0 Demonstration and Explanation

There are two major varities of data files; those that are long and those that are wide. An example of a wide file is shown in the table below.

Example Wide File

 id       grp     lrn95     lrn96     lrn97
  1         1         7        12        16 
  2         1        13        14        15 
  3         2        15        14        20 
  4         2        21        27        24 
  5         3         9         9        12 
  6         3        16        17        18 

We would like to change this file to the long format that looks like this:

Example Long File

 id       year       grp       lrn 
  1         95         1         7  
  1         96         1        12  
  1         97         1        16  
  2         95         1        13  
  2         96         1        14  
  2         97         1        15  
  3         95         2        15  
  3         96         2        14  
  3         97         2        20  
  4         95         2        21  
  4         96         2        27  
  4         97         2        24  
  5         95         3         9  
  5         96         3         9  
  5         97         3        12  
  6         95         3        16  
  6         96         3        17  
  6         97         3        18  

2.1 Wide to long example

. use http://www.ats.ucla.edu/stat/stata/notes3/wide, clear
describe
list
reshape long lrn, i(id) j(year)
describe
list
table year, contents(n lrn mean lrn sd lrn)

2.2 Back to wide again

. reshape wide
describe
list
reshape long
describe
list

2.3 Split-plot factorial example

. use spf2, clear
describe
list
reshape long y, i(s) j(b)
describe
list
anova y a / s|a b a*b, repeated(b)

3.0 Try the commands on your own

. use wide,clear
describe
list
reshape long lrn, i(id) j(year)
describe
list

4.0 For More Information


How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California