Grabbing SPSS Syntax
I’ve asked a few of my colleagues and we estimate that around 90% of SPSS users only use the GUI interface. While the GUI interface has it’s merits (I still use it from time to time) using syntax is one of the best ways to be more productive.
In the below video I demonstrate three ways to get /create SPSS syntax generated by using the GUI interface. This is a great way to begin learning how to utilize code!
Why you should learn SPSS syntax
For more than a decade I’ve been teaching people SPSS syntax, macros, and scripts. Somehow throughout that time I never wrote this post to help explain some of the main benefits of using syntax over the GUI interface. To me there are lots of reasons why you should make it a priority to learn how to code in SPSS. I’ve made an attempt to prioritize them and demonstrated many in the below video.
- Without any real additional work you can reuse:
- renaming of variables
- setting variable labels
- define format (numeric, scale, dates, % etc)
- regroup variables
- Work ahead-
- Export small amount of data on initial kick-off and work on the data file/format.
- Have the frame-work of your report done.
- Then, when done collecting data, export and write-up analysis
- @from Jon Peck- Review what analysis was run in the past. Determine what you (or perhaps a colleague scratching his head) did right/wrong or just borrow from the past. (Hint* did you know there is a log file you can check which might have all GUI/syntax you have ever run? I mention it in this video.)
- Redo analysis done on different data set
- Create macros (special type of SPSS syntax) to re-purpose code for an amazing time savings!
- Apply similar re-coding / analytics on multiple variables
- Macros– True gems of saving time
- Some things are only available in syntax
- Allow for re-applying settings
- Use from other sources (labels from questionnaire ,database, etc.)
In the video below I explain the above concepts and I firmly believe that, after watching the video, you’ll understand why learning SPSS syntax is a great next step for you.
In SPSS syntax you can streamline your code by putting the word “TO” between the first and last variable you want. For instance, in the image below, Category, Lang, cname, Date, and Sent is highlighted. If I wanted to run frequencies perform something on all of these variables I could write the following code:
frequency Category Lang cname Date Sent.
or I could simply do the following:
frequency Category TO Sent.
Both return the same results yet the second one is much simpler, cleaner and can save a lot of space (especially when there are dozens of variables).
I have a short AutoHotKey script which grabs the variables highlighted, copies them to the clipboard and takes the first item, then puts the word ” TO ” then grabs the list item.
This helps me write clean-looking syntax much faster than doing it manually.
Grabbing first and last variable with SPSS syntax
The following is the AutoHotKey code:
;************************Grab first and Last variables for SPSS syntax*******************************.
^!v:: ;Control Alt V is the hotkey that pastes what is on the clipboard
Loop, parse, Clipboard, `n, `r ; Specifying `n prior to `r allows both Windows and Unix files to be parsed.
If A_Index =1
FirstToLast:= " " . First . " TO " . Last . " "
Writing (and re-writing) SPSS syntax can be mundane and time consuming. Sometimes my data set has hundreds of variables which would take me quite some time to rename and relabel. I created an Excel template which generates the necessary syntax and greatly helped automate the process.
The Excel file is available for download from here and the video below helps you see how easy it can be to use. There is also a concatenation tab which allows you to add specific words as a prefix for your variables.