Chapter 26

Getting Acquainted with Syntax

IN THIS CHAPTER

check Learning about the Paste button

check Dealing with repetitive calculations more efficiently

check Labeling with Syntax

check 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.

Technical Stuff You can use R and Python in SPSS. Through a set of features called programmability, you can customize SPSS so that you can add almost anything to SPSS, including the capability to program in R and Python.

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:

  • Pasting
  • Performing repetitive calculations
  • Labeling
  • Requesting repetitive output

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.

Pasting

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.

Remember The Paste button translates mouse clicks into code.

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:

  1. 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.

  2. Choose Transform ⇒ Recode into Different Variables.
  3. In the left panel, move the response variable, which holds the values you want to change, to the center panel.
  4. In the Output Variable area, enter a name (attending) and label (Attending or not) for the new variable.
  5. Click the Change button.

    The output variable is defined, as shown in Figure 26-1.

    Screenshot of the Recode into Different Variables dialog for creating a new attending variable using the existing response variable.

    FIGURE 26-1: The Recode into Different Variables dialog.

  6. Click the Old and New Values button.
  7. Define the recoding:

    1. In the Old Value text box, enter an existing value.
    2. In the New Value text box, enter the new recoded value.
    3. Click the Add button for the Old ⇒   New list (see Figure 26-2).

    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.

  8. Click Continue.
  9. 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.

Screenshot of the Recoding Variables dialog for creating 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.

FIGURE 26-2: Recoding variables.

Screenshot of the Syntax Editor window to construct the recode command and then run the command from there.

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.

Remember Most commands are available from the dialogs. However, some commands and options are available only by using the command language, which is another motivation to learn Syntax.

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.

Warning Make sure you don’t confuse the Paste button with the Ctrl+V operation in SPSS Statistics and Microsoft Excel. The Paste button doesn't perform a copy and paste; it generates Syntax code for you. However, after the code is in the Syntax window, you'll frequently reuse and repurpose that code by copying and pasting it.

Technical Stuff The EXECUTE command (refer to Figure 26-3) looks so official that you’d probably guess that you need it. The truth is, you usually don’t. In this case, we need it because nothing generates output after EXECUTE. If you had any kind of table- or graph-generating command after EXECUTE — and you almost always do — you could (and should) delete it. More on this in the next chapter.

Performing a Series of Related Compute Statements

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.

Labeling

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.

Repeatedly Generating the Same Report

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:

  • At the top, she wants to know which aspects of the business people are the most happy with.
  • She wants to read comments left by survey takers, but she doesn’t want blank comments getting in the way.
  • She wants the comments from happy folks at the beginning.

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.

Illustration of Descriptive Statistics report displaying the minimum, maximum, and mean numbers of the satisfaction variables.

FIGURE 26-4: Satisfaction variables reported in order of satisfaction.

Illustration of a Case Summary report displaying only cases with comments and sorted, limited to first 100 cases.

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.