SPSS tutorials website header logo

SPSS tutorials



SPSS VARSTOCASES with labels tool

Running VARSTOCASES is often necessary for generating nice charts in SPSS. One of the many examples is a stacked bar chart for comparing multiple variables. Sadly, we lose our variable labels when running VARSTOCASES and we really do need those.
The solution is a simple tool that generates our VARSTOCASES for us and applies the variable labels of the input variables as value labels to the newly created index variable.

SPSS VARSTOCASES Problem - Example

Before proposing our solution, let's first take a look at the problem. We'll use course_evaluation.sav, part of which is shown below.


Now let's say we'd like to create the overview table shown below, perhaps followed up by a nice chart. The way to go here is VARSTOCASES as we'll demonstrate in a minute.

Desired End Result


Quick Data Check before VARSTOCASES

Whenever running VARSTOCASES, we always need to make sure our input variables have consistent value labels as explained in VARSTOCASES - wrong results. We'll check that by running the syntax below.

Inspecting Consistency of Value Labels Syntax

*1. Show values and value labels in output.

set tnumbers both.

*2. Check if value labels are consistent over variables.

frequencies q1 to q6.

*Result: consistent labels, good to go.

Right, we now run a basic VARSTOCASES command followed by CROSSTABS for comparing the scores on our 6 input variables in a single table.

Creating an Overview Table Syntax


varstocases/make q from q1 to q6/index question (q).

*2. Show only labels in output for reporting.

set tnumbers labels tvars labels.

*3. Create table.

crosstabs question by q/cells row.


SPSS VARSTOCASES - Lousy Crosstabs

Right. Now, the numbers in this table are correct but we find it very annoying that we lost the variable labels for q2 to q6 in the process. By default, the variable label for q1 is -incorrectly- applied to our newly created variable that now holds the values of q1 to q6. It's precisely these two problems that our tool takes care of.

VARSTOCASES with Labels Tool - How to Use It?

VARSTOCASES with Labels Tool - Just Do It

If you already ran VARSTOCASES on course_evaluation.sav, then you'll need to reopen the original data. After installing the tool, you'll find it under Utilities in the menu. Fill it out as shown below.

SPSS VARSTOCASES with labels tool

Clicking Paste results in the syntax below.

VARSTOCASES Syntax Generated by Tool

*1. Close data without saving and reopen it.

dataset close all.
new file.
get file 'course_evaluation.sav'.

*2. SPSS Python syntax pasted from tool.

begin program.
varSpec = 'q1 to q6'
import spssaux,spss
vallabcmd = 'value labels aspect'
sDict = spssaux.VariableDict(caseless = True)
varList = sDict.expand(varSpec)
for var in varList:
    vallabcmd += "\n'%s' '%s'"%(var.lower(),sDict[var].VariableLabel)
varstocases/make score '' from q1 to q6/index aspect(score).
compute aspect = lower(aspect).
spss.Submit(vallabcmd + '.')
end program.

Generating a Nice Overview Table

After running the previous example, we're good to go. We'll now create a nice overview table of q1 to q6 by running the syntax below.

*1. Add variable label.

variable labels q "Answer Category".

*2. Generate table.

crosstabs question by q/cells row.



Final Notes

Our final table can also be created with CTABLES but not all SPSS users have a license for it. Alternatively, TABLES can do the trick but it's available only from (rather challenging) syntax which is no longer documented.
In our daily work, we routinely use this tool for creating charts rather than tables. One example, the stacked bar chart will be discussed in next week’s tutorial.

Previous tutorial: SPSS VARSTOCASES – What and Why?

Next tutorial: SPSS AGGREGATE Command

Let me know what you think!

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

This tutorial has 4 comments