SPSS Tutorials

BASICS REGRESSION T-TEST ANOVA CORRELATION

Apply Dictionary Information from Excel

Question

“I have an Excel workbook whose three sheets contain data values, variable labels and value labels. How can I apply the dictionary information from these last two sheets to the SPSS dataset after importing the data values?”

Option A: Python

A nice and clean option is to have Python read the dictionary information from the Excel sheets. The cell contents can then be inserted into standard VARIABLE LABELS and ADD VALUE LABELS commands. Running these commands applies the variable labels and value labels to the data values. We'll use data_and_labels.xls for demonstrating this approach.

1. Read the Data Values

Reading Excel data values into SPSS is straightforward. We usually paste the required syntax from File SPSS Menu Arrow Open SPSS Menu ArrowData. The screenshot below shows which options to select.

SPSS Import Excel Data Importing Excel Data into SPSS

SPSS Syntax for Reading Excel Data

*1. Read data values (pasted syntax from GUI).

GET DATA
/TYPE=XLS
/FILE='D:\Downloaded\data_and_labels.xls'
/SHEET=name 'data'
/CELLRANGE=full
/READNAMES=on
/ASSUMEDSTRWIDTH=32767.

2. Create Variable Labels Command

Let's first open our workbook and take a look at how the second sheet is structured. As shown in the screenshot below, the first column holds variable names and the second variable labels.

SPSS Variable Labels in Excel SPSS Variable Labels in Excel

Now we'll read this second sheet with Python instead of SPSS. Note that you need to have the SPSS Python Essentials as well as the xlrd module installed first. The syntax below shows how to create the VARIABLE LABELS commands as a single (multi line) string. For now we'll just print it for inspection.

SPSS Python Syntax Example

*2. Create and inspect VARIABLE LABELS commands.

begin program.
xlsPath = r'D:\Downloaded\data_and_labels.xls'
import xlrd
varLabCmd = ''
wb = xlrd.open_workbook(xlsPath)
varLabs = wb.sheets()[1]
for rowCnt in range(varLabs.nrows):
    rowVals = varLabs.row_values(rowCnt)
    varLabCmd += "variable labels %s '%s'.\n"%(rowVals[0],rowVals[1].replace("'","''"))
print varLabCmd
end program.

3. Create Value Labels Command

SPSS Value Labels in Excel SPSS Value Labels in Excel

Remember that Python objects persist over program blocks. We can therefore leave out the first lines of syntax from the previous example. The Excel sheet holding value labels has the same basic structure as the one with variable labels (see screenshot). The main difference is that we'll now insert three pieces of information (variable name, value, value label) into each line. We'll generate our ADD VALUE LABELS commands as shown below.

SPSS Python Syntax Example

*3. Create and inspect ADD VALUE LABELS commands.

begin program.
valLabCmd = ''
valLabs = wb.sheets()[2]
for rowCnt in range(valLabs.nrows):
    rowVals = valLabs.row_values(rowCnt)
    valLabCmd += "add value labels %s %d '%s'.\n"%(rowVals[0],rowVals[1],rowVals[2].replace("'","''"))
print valLabCmd
end program.

Running the Python Generated Syntax

If neither of the generated commands require any further tweaking, the only thing left to do is just run them by using spss.Submit. The syntax below does so and thus finishes this job.

*4. Run both commands.

begin program.
import spss
spss.Submit(varLabCmd)
spss.Submit(valLabCmd)
end program.

Option B: Syntax Generating Syntax

Before Python was introduced to SPSS, a different approach was needed for this situation. It comes down to declaring a new (long) string variable and using CONCAT to create lines of syntax as string values. Next, we save the contents of this string variable as a .txt file with an .sps extension and INSERT it.
We don't usually recommend taking this approach but we'll present it anyway for the sake of the demonstration. Some of the commands used by the syntax below are explained in SPSS Datasets Tutorial 1 - Basics and SPSS String Variables Tutorial.

SPSS Syntax Generating Syntax

*1. Set working directory.

cd 'd:/downloaded'. /*or wherever Excel file is located.

*2. Read data values (pasted syntax from GUI).

GET DATA
/TYPE=XLS
/FILE='data_and_labels.xls'
/SHEET=name 'data'
/CELLRANGE=full
/READNAMES=on
/ASSUMEDSTRWIDTH=32767.

dataset name values.

*3. Read variable labels.

GET DATA
/TYPE=XLS
/FILE='data_and_labels.xls'
/SHEET=name 'variablelabels'
/CELLRANGE=full
/READNAMES=off
/ASSUMEDSTRWIDTH=32767.

dataset name varlabs.
dataset activate varlabs.

string syntax(a1000).

*4. Create syntax in data window.

compute syntax = concat("variable labels ",rtrim(v1),"'",rtrim(replace(v2,"'","''")),"'.").
exe.

*5. Save variable holding syntax as .sps file.

write outfile 'insert_varlabs.sps'/syntax.
exe.

dataset close varlabs.

*6. Import value labels sheet.

GET DATA
/TYPE=XLS
/FILE='data_and_labels.xls'
/SHEET=name 'valuelabels'
/CELLRANGE=full
/READNAMES=off
/ASSUMEDSTRWIDTH=32767.

dataset name vallabs.
dataset activate vallabs.

string syntax(a1000).

*7. Create syntax in data window.

compute syntax = concat("add value labels ",rtrim(v1)," ",ltrim(str(v2,f3)),"'",rtrim(replace(v3,"'","''")),"'.").
exe.

*8. Save syntax variable as .sps file.

write outfile 'insert_vallabs.sps'/syntax.
exe.

dataset close vallabs.
dataset activate values.

*9. Run both syntax files.

insert file = 'insert_varlabs.sps'.
insert file = 'insert_vallabs.sps'.

*10 Optionally, delete both syntax files.

erase file = 'insert_varlabs.sps'.
erase file = 'insert_vallabs.sps'.

Previous tutorial: Merge Many Data Files

Next tutorial: Suffix All Variable Names

Let me know what you think!

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

This tutorial has 9 comments