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
/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.
We may want to compare the sectors our respondents have been working in over years as in the table below.
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
/make sector from sector_2010 to sector_2014
*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).
*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.
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
/make t from t1 to t5
/make a from a1 to a5
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.
We first run basic FREQUENCIES. Note that both questions basically indicate that politicians are not very popular with our respondents.
set tnumbers both.
*2. Frequency tables.
frequencies v1 v2.
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
/make v from v1 v2
*2. Remove incorrect variable label.
variable labels v ''.
*3. Note that result is not correct.
crosstabs v by question/cells columns.
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.