Chapter 26
IN THIS CHAPTER
Learning about the Paste button
Dealing with repetitive calculations more efficiently
Labeling with Syntax
Repeatedly generating routine reports
You've probably encountered folks who perform statistical calculations and build statistical models by coding in R and Python, two open-source languages. Well, SPSS has a programming language, and for decades this language was the only way to work in SPSS.
Frankly, you might be glad that you can work in menus because they provide a lot of help. However, almost everything that happens in SPSS is the result of Syntax running behind the scenes. For example, when you use a dialog to specify a set of options and then click OK, Syntax commands are generated and executed. In fact, you’ve already seen examples of Syntax at the top of the SPSS Statistics Viewer window every time a command runs.
If you take a look at the Help menu, you will find the Command Syntax Reference, which is a real door stopper at more than 2,000 pages. (Don’t try reading that until you read this chapter and the next one.) This chapter describes some Syntax language fundamentals and explains why Syntax is helpful. The next chapter gets into more of the details on using Syntax.
Why use Syntax? First, it’s a lot easier than you might think, but no tip or trick in SPSS is worthwhile unless it improves your results or makes your life easier. Syntax may both improve your results and make your life easier. Second, Syntax might be a better option than menus in the following four situations:
In this chapter, you explore how to use Syntax in these situations through the use of a simple web survey as a case study. Many researchers check survey results daily, as new data arrives. Here you’ll see how to deal with daily routines like this more efficiently.
Here’s the secret to learning Syntax, but this is a secret you can tell all your friends: You usually don’t have to type anything. This also means you usually don’t have to look up commands.
How can this be? It all looks so complicated at first. The secret is the Paste button. Almost every dialog in SPSS has a Paste button that writes Syntax code for you. You just have to save the code for the next time you need it.
A super-easy example is recoding variables, such as the example demonstrated in Chapter 8. (Refer to Chapter 8 for the specifics on how to recode variables.) In this example, we created a new attending
variable using the existing response
variable, as shown in Figure 26-1.
The following steps create the recoded values and store them in a new variable:
From the main menu, choose File ⇒ Open ⇒ Data and load the rsvp.sav file.
This file can be downloaded from the book's companion website at www.dummies.com/go/spss
.
response
variable, which holds the values you want to change, to the center panel.Click the Change button.
The output variable is defined, as shown in Figure 26-1.
FIGURE 26-1: The Recode into Different Variables dialog.
Define the recoding:
We created the attending
variable by assigning a new value of 1 to all the scores of 1 on the response
variable, and assigning a value of 0 to all the scores of 0 and -1, as shown in Figure 26-2.
Click Paste.
Normally you would click OK to run the command. Now, however, you click the Paste button in the bottom left of the dialog. Voilà! — the result shown in Figure 26-3 appears in the Syntax editor window.
FIGURE 26-2: Recoding variables.
FIGURE 26-3: The Syntax Editor window.
When you click OK in the Recode into Different Variables dialog, the recode command is run automatically behind the scenes. Working with Syntax, you construct the recode command in the Syntax Editor window and then run the command from there. But in both cases, the same recode command is run.
Whenever you want to run Syntax, you can simply choose Run ⇒ All from the Syntax Editor window, or you can highlight just a portion of Syntax and choose Data ⇒ Select. It's that simple.
Now, there's more to Syntax than that. We have an entire second chapter on Syntax (and entire books are dedicated to it), but that's about all you need to know about pasting.
Here's what makes pasting so powerful. After you have pasted the Syntax code, you can easily modify it and reuse it. With practice, doing so is faster than the using the menus for many tasks.
Consider the following line of code you just pasted:
RECODE Sat1 (4 thru 5=1) (ELSE=0) INTO HighSat1.
Now assume that you want to do this calculation for all five variables (Sat1
through Sat5
), as follows:
RECODE Sat1 (4 thru 5=1) (ELSE=0) INTO HighSat1.
RECODE Sat2 (4 thru 5=1) (ELSE=0) INTO HighSat2.
RECODE Sat3 (4 thru 5=1) (ELSE=0) INTO HighSat3.
RECODE Sat4 (4 thru 5=1) (ELSE=0) INTO HighSat4.
RECODE Sat5 (4 thru 5=1) (ELSE=0) INTO HighSat5.
Using a copy-and-paste maneuver in the Syntax window and changing the number in the original and new names is easier than using the Recode dialog five times.
Adding variable and value labels in Data Editor can be time consuming because it involves clicking in many cells. However, typing Syntax commands in the Syntax editor is easy. For example, open the Syntax window, and enter the following commands:
VARIABLE LABELS
Sat1 "Product Selection"
Sat2 "Pricing"
Sat3 "Product Quality"
Sat4 "Website"
Sat5 "Customer Service".
VALUE LABELS Sat1 TO Sat5
1 'Very Dissatisfied’
2 'Dissatisfied'
3 'Neither Satisfied nor Dissatisfied'
4 'Satisfied'
5 'Very Satisfied'.
Using Syntax to create variable and value labels is considerably faster, especially if you use this data repeatedly. And because you can save your Syntax, your coworkers will love it because they can borrow your code.
Imagine that every morning your boss wants a quick rundown on the web survey activity that’s taken place since the previous report. She’s busy, so she wants it organized as follows:
In other words, your boss wants a Descriptive Statistics report like the one in Figure 26-4 and another report like the one in Figure 26-5.
FIGURE 26-4: Satisfaction variables reported in order of satisfaction.
FIGURE 26-5: A Case Summaries showing only cases with comments and sorted.
The following code produces both reports. Just choose Run ⇒ All from the Syntax window.
To take a closer look at the syntax, choose File ⇒ Open ⇒ Data and load the Web Survey2.sav file. Then choose File ⇒ Open ⇒ Syntax and load the Web Survey.sps file. These files can be downloaded from the book’s companion website at www.dummies.com/go/spss
.
DESCRIPTIVES VARIABLES=Sat1 Sat2 Sat3 Sat4 Sat5
/STATISTICS=MEAN MIN MAX
/SORT=MEAN (D).
SORT CASES BY Mean_Sat(D).
TEMPORARY.
SELECT IF length >0.
SUMMARIZE
/TABLES=Name Comment Mean_Sat BY Any_Ones
/FORMAT=VALIDLIST NOCASENUM TOTAL LIMIT=100
/TITLE='Case Summaries'
/MISSING=VARIABLE
/CELLS=COUNT.
At first glance, it may look like you would need a substantial amount of knowledge to put together this code. However, the DESCRIPTIVES
command can be pasted as described at the beginning of the chapter. The same is true with SORT
and SUMMARIZE
. The SUMMARIZE
command is found under Analyze ⇒ Reports ⇒ Case Summaries. We typed the other commands, TEMPORARY
and SELECT
. For more on both commands as well as typing commands in the Syntax window, see the next chapter.
The main point for now is that SPSS will do 80 percent of the typing for you if you use the dialogs. Just remember to use the Paste button to save your work. After you’ve tested the code, and you know that it works, save the whole thing so you can reuse it at any time.