
SPSS users who want to speed up their work by using Python will encounter some surprises. This tutorial walks you through the 5 major pitfalls and shows how to avoid them.
1. Python is Fully Case Sensitive
SPSS is mostly case insensitive; if we have a variable “gender”, we can address it in syntax as gender
or GENDER
or anything in between. On top of that, we can't have two variables gender
and GENDER
in SPSS because they'd be seen as the same variable.
In Python, none of the above holds. As it's fully case sensitive, we must always use the exact right casing for all objects. This is especially tricky when importing modules or using methods: they don't seem to exist if we don't use the correct casing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
*Wrong casing for module. begin program. import spss,spssclient # ImportError: No module named spssclient end program. *Correct casing. begin program. import spss,SpssClient end program. *Wrong casing for attribute. begin program. import spssaux sDict = spssaux.Variabledict() # AttributeError: 'module' object has no attribute 'Variabledict' end program. *Correct casing. begin program. import spssaux sDict = spssaux.VariableDict() end program. |
Result

2. Indentation Matters in Python
In many computer languages -SPSS syntax, Javascript, CSS, PhP, HTML and more- indentation is optional and mainly used for making code more readable. In Python, however, indentation indicates where Python loops and Python-if clauses end. The very simple examples below illustrate how it works.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
*Print "hello" and "bye" 5 times in loop. begin program. for i in range(5): print "hello" print "bye" #Indented so still in loop end program. *Print "hello" " 5 times in loop and "bye just once. begin program. for i in range(5): print "hello" print "bye" #Not indented so loop has ended end program. |
Python Indentation in SPSS
We could indent lines with 1 or 2 spaces but for some reason, 4 spaces is most common. We can set this in SPSS by navigating to
and setting it to 5.SPSS seems to insert the number of spaces - 1, which we think may be a minor bug.
Pressing tab in the syntax editor window now results in 4 spaces.

Note that Notepad++ -recommended for writing larger block of Python code- has a similar setting and some very handy shortkeys for indenting or outdenting entire sections.
3. Comment Your Code
For SPSS syntax as well as Python, adding comments to your code is a great idea. In Python, all code between a hash tag (#) and the end of a line is seen as a comment. By default, Notepad++ shows Python comments in green as shown below.

4. Print Objects and their Types
If you create Python-objects such as strings or Python lists yourself, you'll probably know their object types and what they contain. This allows you to work with them in a goal directed manner.
However, if we retrieve objects from SPSS such as value labels or data values, we're not always sure how they end up in Python. The solution is to run print object
and print type(object)
. The example below -using employees.sav- illustrates how this helps us out.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
*Look up value labels for job satisfaction. begin program. import spssaux sDict = spssaux.VariableDict() vallabs = sDict['job_satisfaction'].ValueLabels print type(vallabs) # <type 'dict'> end program. *Since vallabs = Python dict object, we can retrieve key-value pairs with iteritems() method. begin program. for key,val in vallabs.iteritems(): print key,val end program. |
5. Be Careful with Backslashes
Python uses the backslash (\) as an escape character in strings. This may yield unexpected results if we're not aware of it. For example, we can't specify the path to an SPSS file as somepath = 'c:\newdata\data.sav' because “\n” inserts a line break into our string rather than “\n”. One solution is to prefix the entire string with “r”, which is short for raw string. Therefore, somepath = r'c:\newdata\data.sav' will work as we intended.

The examples below show some wrong and correct uses of backslashes in Python.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
*Wrong way: \n indicates new line. begin program. somepath = 'c:\newdata\data.sav' print somepath end program. *Right way: \n in raw string is just \n. begin program. somepath = r'c:\newdata\data.sav' print somepath end program. *Wrong way: second quote ends string prematurely. begin program. print 'I don't know!' end program. *Right way: \ escapes second quote. begin program. print 'I don\'t know!' end program. |
Right, I guess these are the major pitfalls in Python. If you agree or disagree or if I forgot to mention something, please let me know by dropping a comment below or otherwise, ok?
Thanks for reading!
This tutorial has 5 comments
By Ruben Geert van den Berg on January 18th, 2019
Hi Aravinda, thanks for your comment!
Good point but I think it'll be over the heads of most of my audience.
Thanks!
SPSS tutorials
By Aravinda Thiagalingam on January 18th, 2019
Hi Ruben,
Thanks for tutorial! One other thing that is worth mentioning with python is that many objects - particularly lists are passed by reference and hence assigning a new variable to an old one and then changing the new variable will result in change the old one (as it is not a copy of the old list but just another pointer to it).
By Ruben Geert van den Berg on July 31st, 2016
Hi Omkar!
When you install a recent SPSS version, you'll be asked during the installation process whether you'd like to have the Python Essentials too. It seems you declined that so you have SPSS without the Python Essentials -like many other users by the way. I think the easiest fix is to uninstall and reinstall SPSS completely and make sure you do opt in for the Python Essentials.
Other users have reported that they added the Python Essentials to SPSS without reinstalling but I'm not entirely sure how they did it and how much effort it took them.
By omkar on July 31st, 2016
In our company we are using SPSS v23 on windows 7.When i am trying to run the python code as you suggested in the previous section.I got the bellow error.
>Error # 6887. Command name: begin program
>External program failed during initialization.
>Execution of this command stops.
Please make sure that Essentials for Python has been successfully installed.
As per knowledge SPSS v23 dosen't require any additional download of it(defaulty installed). if so what are the ways to perfectly install Python Essentials in my PC.(before than how can i make sure that i have python essentials in my system( i didn't see any python essential installation file in my SPSS program files)).Secondly just to let you know i didn't install Python in my PC.Is it required to install it.
please help on this.
By Linda Martell on July 15th, 2015
Really good!