SPSS Python Examples

Learn Python for SPSS the right way by running some of the examples below. They show precisely how to get things done in SPSS the fastest way possible and are easily modified to suit your needs. Enjoy!

SPSS for Python - Shorter Syntax

Latest SPSS Python Examples

SPSS – Batch Process Files with Python

Running syntax on several SPSS data files in one go is fairly easy with Python. This tutorial walks you through. Read More

SPSS – Create Several Excel Files with Python

We'll look up SPSS data values with the Python spssdata module. We'll then have Python insert the values into SPSS syntax and create several Excel files. Read More

SPSS – Change Value Labels with Python

A local supermarket held a small survey, the data of which are in minisurvey.sav. Unfortunately, the software for downloading the data in SPSS format prefixes all variable and value labels with the variable names. The screenshot below shows part of the data. Undesired Prefixes in Value Labels Clicking on some value labels in variable view ... (Read More) Read More

SPSS – Process Variables Based on Names

We held a reaction time experiment in which people had to resolve 20 puzzles as fast as possible. The 20 answers and reactions times are in an SPSS data file which we named trials.sav. Part of if is shown below. In first instance, we'd just like to inspect the histograms of our reaction time variables. ... (Read More) Read More

SPSS with Python – Looping over Scatterplots

For running SPSS tables or charts over many variables, an SPSS Python loop is the right way to go. We'll demonstrate some examples for doing so. Read More

SPSS with Python - Merging Files

Add Filenames to Files Before Merging

"I'd like to merge a number of data files with the "add files" command. However, in the merged file I'd like to see which record came from which file. Is there an easy way to accomplish this?" Read More

Adjust String Lengths before Merging Files

"I'd like to merge a number of data files with the "ADD FILES" command but it doesn't work. Some similarly named string variables have different lengths over different files. Is there an easy way to automatically adjust the lengths of all string variables in order to merge my files?" Read More

Merge Many Data Files

"I have 200 .sav files that I'd like to merge by using "add files". Since "add files" merges a maximum of 50 data files, I need to run it four times. I know how to write the syntax but inserting all file names is a lot of tedious copying-pasting-editing. Is there a more elegant way?" Read More

Python for SPSS - Editing Data

Apply Dictionary Information from Excel

"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?" Read More

Suffix All Variable Names

I have a data file in which all variables were measured in 2012. I'd like to suffix their names with "_2012". What's the easiest way to do this? Read More

Strip Prefix from Variable Labels

"My variable labels all have prefixes that end with ": ". How can I remove those in order to prettify these labels?" Read More

Select Variables by Variable Labels

"I'd like to calculate the mean over all variables that have the "How would " in their variable labels. What's a good way to do so?" Read More

Prefix Many Value Labels

"In my data, many values are answers to questions. In order to make this clear, I'd like to prefix their value labels with "Answer: ". For now, I want to apply this only to the fifth through the fourteenth variable in my data." Read More

Sort Values within Cases

Sorting values within cases can be done very easily with the SPSSINC TRANS extension. If many variables are involved, this command may be generated by Python to increase the efficiency even further. Read More

Find Within Subjects Favorite over Several Variables

"My respondents rated several candidates on a 5 point Likert scale. I'd like to know for each rater his favorite candidate. In case more than one candidate received the highest score, the favorite should be a random choice from the winners. I'd also like to know for each rater how many winners there are and which score they got." Read More

Split String Variable into Components

"I have a long string variable in my data that actually holds the answers to several questions. These are separated by a semicolon (";"). How can I split this variable into the original answers?" Read More

Remove Value Label from Multiple Variables

"I'd like to completely remove the value label from a value for many variables at once. Is there an easy way to accomplish that?" Read More

Managing Files

Move all Files from Subfolders to Main Folder

"I'd like to work with a number of .sav files but they are scattered over different folders. All file names are unique. Is there any easy way to search through a number of folders for .sav files and move these into some root directory?" Read More

Copy all Files from Subfolders to Main Folder

"I'd like to work with some data files that are scattered over a number of (sub)folders. All file names are unique. How can I search all folders for .sav files and copy those into some root directory?" Read More

Export Output to Different Folders

"In my data, I have different teams nested within different areas. Right now I have five cities and in each city teams A through E. Some output for each combination of area and team separately should be exported into its own folder. Is there any flexible way to accomplish this?" Read More

Rename or Prefix Many Files

"I have a number of data files with incomprehensible file names. In order to keep track of these, I'd like to prefix their file names or perhaps even rename them to some prefix + constant. What would be the easiest way to do so?" Read More

Search Syntax Files for Expression

"I found a variable "v_4" in an old data file and I can't remember how exactly I created it. The syntax I used got a bit messy, I have different files and they're in different folders. Is there an easy way to find out which syntax files contain the expression "v_4"?" Read More

Create Dictionary Dataset

An often requested feature is to export variable and value labels to Excel. This handy tool creates an SPSS Dataset containing these labels. It can either be be saved as an Excel sheet or further edited in SPSS. Read More

Other Python Examples

Regression over Many Dependent Variables

"I have a data file on which I'd like to carry out several regression analyses. I have four dependent variables, v1 through v4. The independent variables (v5 through v14) are the same for all analyses. How can I carry out these four analyses in an efficient way that would also work for 100 dependent variables?" Read More

Insert Values from Output Tables into Text

"I'd like automatically generate text reports in which some values from SPSS output tables are automatically inserted. Is this possible and if so, how?" Read More

Restrict Command to Variables having Minimum Valid N

"I'd like to see the frequency distributions of some variables. However, I'd like variables with a small number of valid observations to be excluded automatically. Is there an easy way to accomplish this?" Read More