Continuing my discussion on the use of SPSS Syntax scripting to perform file operations, today I’ve investigated how to use SPSS to perform some very general physical file restructuring. Why is this important? It’s important because it again shows how SPSS can be used to arrange the data pool into the most effective format based upon the analysis procedure that you want to perform on it.
First, as a reminder, the SPSS Restructure Data Wizard can be used to restructure your data. Using your provided selections, the wizard replaces a file with a new, restructured file.
The wizard can:
• Restructure selected variables into cases
• Restructure selected cases into variables
• Transpose all data
But, what if you don’t want to “drive the UI”? And what if you want to automate the restructuring process to be used again and again? Of course, the answer is a Syntax script.
Cases to Variables Example
To illustrate the point, we’ll use a “cases to variables” example and leverage a file from my previous post.
In that post, I utilized a syntax script to generate a (merged) file of quarterly hours-billed data. In that file, I had a record (or case) for each consultant, for each month:
In this case, I’d rather have my file have a single record (case) per consultant. Of course I could use other methods and tools to restructure my file, but SPSS gives me everything I need. I can write a short Syntax script to do exactly that.
SORT CASES and CASESTOVARS
In my example script, I used 2 SPSS functions SORT CASES and CASETOVARS. CASETOVARS is the “heavy lifter” in that I supply a variable name to be used as the “unique ID” (consultant”) and SPSS does the rest.
In addition, in the new file it doesn’t make sense to “repeat” the month variable on the same record (as it can be easily inferred from hoursbilled.1, hoursbilled.2 and hoursbilled.3), so I’ve added the /DROP parameter to the script so that field (variable) gets dropped out of my new file.
A side note, when you are writing your scripts it is important to maintain GOOD programming style, so get familiar with 2 icons I circled here – comment line and auto-indent – both will help you write and maintain easily readable and understandable scripts.
After we save and run the script, we get the file in the format I am looking for:
And of course, SPSS Statistics viewer gives the following evidence:
My next post will show the results of some of my explorations into the extensive SPSS Syntax function library!