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.
Spss.submit( [“GET FILE = ‘c:/mydata.sav’ .” , “PRINT/ALL.” , “EXECUTE.” ])
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.
Spss.submit (“GET FILE = ‘c:/mydata.sav’.”)
ScaleList = 
For i in range (spss.GetVariableCount ()):
If (spss.MetVariableMeasurementLevel (i) = =’scale’):
ScaleList.append (spss.GetVariableName (i))
Spss.submit (“Descriptives “ + “ “.join(ScaleList) + “.”)
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.