Using Python with SPSS

by Lucy Fike

We know that using SPSS syntax is an easy way to organize analyses so that you can rerun them in the future without having to go through the menu commands.

Using Python with SPSS makes it much easier to do complicated programming, or even basic programming, that would be difficult to do using SPSS syntax alone. You can use scripting programming in Python to create programs that execute automatically.

Python is a general-purpose language–it was not designed for SPSS alone, and it is very easy to learn.

You will need to install the Python-plug in, as it is not automatically installed when you install SPSS. It can be installed from the CD or downloaded from the IBM web site.

Once you have the Python plug-in installed, you can write programs using Python within your SPSS syntax. For example, you can write programs to execute multiple commands.

BEGIN PROGRAM.
Import spss
Spss.submit( [“GET FILE = ‘c:/mydata.sav’ .” ,  “PRINT/ALL.” , “EXECUTE.” ])
END PROGRAM.

In the program above, Python uses the statement spss.submit to pass through multiple SPSS statements at once. Notice each command is in quotes with a period at the end. The commas separate each command. Without them, Python would join the two commands together, resulting in an error message.

You can also use Python to loop through different SPSS variables.

BEGIN PROGRAM.
Import spss
Spss.submit (“GET FILE = ‘c:/mydata.sav’.”)
ScaleList = []
For i in range (spss.GetVariableCount ()):
If (spss.MetVariableMeasurementLevel (i) = =’scale’):
ScaleList.append (spss.GetVariableName (i))
If (len(ScaleList)):
Spss.submit (“Descriptives  “  + “ “.join(ScaleList) + “.”)
END PROGRAM.

In the program above, an array called ScaleList is created to hold all the variable names of all scale variables found in my data set. Once all scale variables are identified, descriptive statistics are run on the variables and displayed in SPSS output.

If you have never used any of the general programming languages before, I suggest starting with this book – http://www.greenteapress.com/thinkpython/thinkCSpy.pdf – it’s free and it gives a great introduction to the python language.

Happy coding!

 

Getting Started with SPSS
Karen will introduce you to how SPSS is set up, some hidden features to make it easier to use, and some practical tips.

Reader Interactions

Comments

  1. suresh says

    Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.

  2. Nguyen Tran says

    Hi,

    I recently worked in a big dataset (SPSS) with around 20.000 variables, I want to keep some special variables (e.g with pattern “_Product”,”_Shop” at the end of variable name) to process some recoding scripts in my company server hourly. The reason that I would like to use Python instead of hard coding is because the products and shops are changing every month. How can I do it by using python? I have tried to open the SPSS data without “/Keep” and it took 10 minutes or more.


Leave a Reply

Your email address will not be published. Required fields are marked *

Please note that, due to the large number of comments submitted, any questions on problems related to a personal study/project will not be answered. We suggest joining Statistically Speaking, where you have access to a private forum and more resources 24/7.