Command reference
reshape — switch between wide and long data
Reorganize repeated measurements between columns and rows. In wide data, one row can contain math1 and math2; in long data, separate rows hold math for each term.
Syntax
reshape long stubs, i(idvars) j(indexvar) [string]
reshape wide stubs, i(idvars) j(indexvar) [string]
stubs are the shared variable-name parts, such as math and read in math1 math2 read1 read2. i() identifies the unit, such as a student. j() identifies the repeated measurement, such as term 1 or 2.
Directions and identifiers
Example
The sample contains four students, a school name, and math and reading scores for two terms. It converts both subjects to long form and sorts the result by student and term.
clear
import delimited "data/student_scores_wide.csv"
reshape long math read, i(id) j(term)
sort id term
list
Sample data included. Opens in a new tab; click Run.
What to look for
The result has eight observations: two terms for each of four students. The subject columns are now math and read, and term identifies the measurement. Student 1 has math scores 48 and 51 in terms 1 and 2; the school name repeats on both rows.