Restructure data using Syntax commands in SPSS

SPSS allows you to use Syntax code to reshape data from wide to long format, and vice versa. Below are data sets repeatedly measured in wide and long format. Note that the example data contain two participants measured on two outcome variables (weight and calories), under three different time points.

The wide format uses one row for each observation or participant:

ID    WEIGHT1   WEIGHT2    WEIGHT3    CALORIE1    CALORIE2    CALORIES3
 1      200       190        180         3500         3300         3100
 2      160       150        140         3000         2900         2800

The long format uses multiple rows for each observation or participant:

ID   WEIGHT   CALORIES  TIME
 1     200     3500      1 
 1     190     3300      2
 1     180     3100      3
 2     160     3000      1
 2     150     2900      2
 2     140     2800      3

To restructure data from wide to long, use the VARSTOCASES command:

VARSTOCASES
/make WEIGHT from WEIGHT1 WEIGHT2 WEIGHT3 
/make CALORIES from CALORIES1 CALORIES2 CALORIES3
/index= TIME(3)
/keep=ID.

To restructure data from long to wide, use the CASESTOVARS command:

CASESTOVARS
/id=ID
/index = TIME.

If you have questions about using statistical and mathematical software at Indiana University, contact the UITS Research Applications and Deep Learning team.

This is document bclg in the Knowledge Base.
Last modified on 2023-06-30 12:22:09.