Serious SPSS users have probably heard that
using Python in SPSS can dramatically speed up
your daily SPSS tasks. So what is it and how does it work? This brief tutorial quickly walks you through.
- How does Python relate to SPSS?
- What Is Python Known For?
- Why Should I Use Python in SPSS?
- Where Can I Get Python for SPSS?
- Which are Some SPSS Python Examples?
Python is one of the main general programming languages today and was first launched in 1989.
SPSS -short for “statistical package for the social sciences”- is user friendly software for data editing/analysis and statistical procedures. SPSS is much older than Python: it's already been in use since 1968. Originally, Python had nothing whatsoever to do with SPSS. They were simply 2 completely unrelated software packages until roughly 2005.
So How does Python relate to SPSS?
Around 2005, the SPSS developers created software that connects SPSS with Python: the SPSS-Python plugin. This plugin made it possible to
- send Python code to Python from SPSS;
- have Python look up anything in SPSS: variable names or labels, data values, output tables and more;
- use such information to create (large amounts of) custom-made SPSS syntax;
- have Python send such syntax back to SPSS and execute it;
- or, alternatively, have Python directly modify SPSS output tables & charts, syntax or data.
The figure below sketches some of such interactions between SPSS and Python.
What Is Python Known For?
- Python is among the most important programming languages in use today. It is open source software and it may be freely downloaded, used and distributed, even for commercial use.
- Python was deliberately designed to be intuitive and easy to learn, a programming language suitable for non programmers -which probably includes most SPSS users.
- Python easily handles a huge variety of tasks: it can read, write or modify text files, Excel files, MySQL databases, SPSS and much more.
- Python does a couple of things very differently than the bulk of programming languages. Especially its use of indentation for control structures (looping and conditional processing) is original but effective;
- Just like www.spss-tutorials.com, Python was invented and created in Amsterdam, the Netherlands.
Why Should I Use Python in SPSS?
- For some larger SPSS tasks, using Python for SPSS may decimate the amount of time and effort they require.
- Also, Python may drastically decrease the amount of SPSS syntax required by some tasks. Shorter syntax is much easier to read, adjust and correct.
- Some SPSS tasks are not possible at all with basic syntax but are easily accomplished by Python.
- There are no costs associated with using Python in SPSS.
- There's an ever growing number of SPSS extensions freely available that can be used from SPSS’ menu. Most of these require Python to actually run.
Where Can I Get Python for SPSS?
Finally an easy question... Recent SPSS versions are integrated with Python by default. It is located in the Python3 folder in your SPSS installation folder as shown below.
For more details on this, read up on Python for SPSS - How to Use It?
Which are Some SPSS Python Examples?
Some excellent SPSS-Python code is found in many of our SPSS tools:
- SPSS Mean Centering and Interaction Tool is super useful for moderation regression;
- SPSS - Recode with Value Labels Tool recodes values and moves their value labels from the old onto the new values;
- SPSS - Create All Scatterplots Tool runs scatterplots with(out) regression lines and tables for many pairs of variables in one go;
- SPSS - Clean Labels Tool performs text replacements over many value and/or variable labels;
- SPSS Create Dummy Variables Tool creates dummy variables for regression.
Note: when using these tools, you don't immediately see the underlying Python code. However, if you unzip the SPSS extension (.spe) files, you'll find that each of them contains a Python (.py) file that contains the SPSS-Python code being used.
Thanks for reading!
THIS TUTORIAL HAS 19 COMMENTS:
By BELKACEM Rachid on December 22nd, 2016
i want to learn a basic thing about using python from spss
By Ruben Geert van den Berg on December 22nd, 2016
Hi Rachid! Perhaps play with some of the examples. We recently published SPSS Python Loop Examples, see if you like it!
By Anto on January 16th, 2017
By Jon Peck on April 14th, 2017
Of course, I enthusiastically endorse Python with SPSS and could (and do) talk for hours about it. A few points:
Python was introduced into SPSS in version 14. The functionality, though has been increased substantially since then.
The Custom Dialog Builder was introduced in V17, but a bunch of improvements added in V18 make that the practical minimum for many purposes.
Python was indeed begun in the Netherlands, which deserves full credit, but the inventor emigrated to the US many years ago :-)
By Ruben Geert van den Berg on April 14th, 2017
Hi Jon, I'm a great Python for SPSS enthusiast too of course! I'll mention these points -and more- in the next couple of tutorials.
The SPSS Python tutorials are pretty much the first I ever wrote and they became seriously outdated. Since they still attract a lot of readers, I thought it time to update and drastically improve all of them.
I still get most of my freelance assignments from people seeking help with Python for SPSS. So plenty of reasons to rework these tutorials.