SPSS tutorials website header logo SPSS TUTORIALS BASICS ANOVA REGRESSION FACTOR CORRELATION

SPSS VARSTOCASES – What and Why?

SPSS VARSTOCASES - Minimal Example

SPSS VARSTOCASES is short for “variables to cases”. It restructures data by stacking variables on top of each other as illustrated by the figure above. You can try this simple example for yourself by downloading and opening sav_data018 and running the syntax below on it.

SPSS VARSTOCASES Example 1

*Very basic VARSTOCASES example.

varstocases
/make v from v1 v2
/index q (v).

SPSS VARSTOCASES - Why?

One good reason for using VARSTOCASES is that many SPSS graphs can only be generated after stacking the relevant variables on top of each other. On top of that, VARSTOCASES also facilitates generating some tables. For example, let's take a look at some of the data in freelancers.sav.

SPSS VARSTOCASES - Data View

We may want to compare the sectors our respondents have been working in over years as in the table below.

SPSS VARSTOCASES - Overview Table

Makes sense, right? Now, one way for creating this table is CTABLES (custom tables) but this requires a (paid) add-on module. Second, TABLES will do the trick but it's available only in (challenging) syntax and no longer documented.
The third option is VARSTOCASES followed by CROSSTABS as demonstrated below. Note that VARSTOCASES results in an incorrect variable label so we correct that in step 2. We'll discuss this problem a bit later on.

SPSS VARSTOCASES Example 2

*1. Varstocases.

varstocases
/make sector from sector_2010 to sector_2014
/index year(sector).

*2. Correct variable label.

variable labels sector "Sector in which respondent was working".

*3. Extract year from string.

compute year = char.substr(year,index(year,'_') + 1).
execute.

*4. Generate table.

crosstabs sector by year/cells column.

SPSS VARSTOCASES - Creating Multiple Variables

Our first two examples created one variable holding the original values and a second (index) variable holding the original variable names. In some cases, however, you may want to restructure multiple sets of variables in one go. For instance, let's consider sav_data016, holding typical reaction time data.

SPSS VARSTOCASES - Reaction Times

Apparently, respondents took 5 trials, each resulting in an answer and a reaction time. So do wrong answers have shorter or longer average reaction times? One way to figure out is using the VARSTOCASES syntax below, perhaps followed by MEANS.

SPSS VARSTOCASES Example 3

*Varstocases: two sets of variables.

varstocases
/make t from t1 to t5
/make a from a1 to a5
/index trial.

Result

SPSS VARSTOCASES - Result

SPSS VARSTOCASES - Wrong Results

In our second example, we placed two variables on top of each other in one new variable. Both input variables had a variable label but the (single) output variable can have only one variable label. SPSS “solves” the problem by using the first variable label it encounters. In most cases, this will be incorrect but we'll readily see the problem.
The real problem with VARSTOCASES is that the same principle holds for value labels. This may result in nonsensical results. We'll now demonstrate this on sav_data017, part of which are shown below.

SPSS VARSTOCASES - Politics Data View

We first run basic FREQUENCIES. Note that both questions basically indicate that politicians are not very popular with our respondents.

*1. Show values and value labels in output.

set tnumbers both.

*2. Frequency tables.

frequencies v1 v2.

Result

SPSS VARSTOCASES - Correct Frequencies

Importantly, these tables also show that our two variables have inconsistent value labels. We now run VARSTOCASES and replicate both frequency tables with a single contingency table.

SPSS VARSTOCASES Example 4

*1. Varstocases.

varstocases
/make v from v1 v2
/index question(v).

*2. Remove incorrect variable label.

variable labels v ''.

*3. Note that result is not correct.

crosstabs v by question/cells columns.

Result

SPSS VARSTOCASES - Nonsensical Results

Note that VARSTOCASES has applied the value labels of v1 to the values of v1 and v2, resulting in misleading results. Even more disturbing, SPSS didn't throw any error or warning that things were going wrong at some point.
Believe it or not but this is not a bug. VARSTOCASES is supposed to work like this and this behavior is described in the CSR. We wonder, however, how many SPSS users are aware that this may happen. And even those who are aware have no efficient way for circumventing it as SPSS completely lacks any dictionary consistency check.
On a personal note, we feel that VARSTOCASES should perform such a check by default and at least issue a warning if things do go wrong. This suggestion goes for ADD FILES too, which shows similar problematic behavior which is even harder to avoid.

Tell us what you think!

*Required field. Your comment will show up after approval from a moderator.

THIS TUTORIAL HAS 9 COMMENTS: