# SPSS Syntax Part 2: Computation

In my previous post, I covered the use of SPSS syntax for creating a history of analyses that have been conducted, and how to add comments so you don't have to re-run the analyses to get the relevant output every time you need it. The other main use for SPSS syntax, and the one that I use much more frequently, is to compute variables so they are ready to be used for analyses.

Although SPSS technically has a point-and-click menu for computing a variable (Transform > Compute Variable...), it ends up giving you a window where you need to write the syntax for the computation you want to do. So, I generally prefer to just do it all in a syntax file from the start.

There are two main functions that you need to know in order to utilize most of the computing power of SPSS: COMPUTE and DO IF

COMPUTE is the function used to actually do a computation. You use it by writing COMPUTE, followed by the name of the new variable you want to make, then defining what that new variable should hold. For example:

COMPUTE depMood = MEAN(var1,var2,var3,var4). EXECUTE.

Here, the syntax is basically saying "create a new variable called depMood, and in that variable put the mean* of var1, var2, var3, and var4." The EXECUTE is simply needed to tell SPSS to actually run the computation (why they require it I don't fully understand, but the syntax won't work without it).

When you run a COMPUTE command, it computes the values for each participant in the database. That is, the above syntax won't calculate a single mean based on the means of the variables given, but it will give each person a value in the "depMood" variable which contains the mean of the values that person has for var1, var2, var3, and var4.

Alright, pretty simple so far. But there are a lot of times that you don't want to run the same computation for every single person. In order to clarify when certain calculations should be conducted, DO IF can be used. DO IF allows for specifying a condition, and the computation will only happen if that condition is true.

To help understand how COMPUTE and DO IF can work together, it's helpful to look at an example. I recently had to calculate the average number of hours per week a child was sedentary. Participants were asked how much time they spend on a normal weekday doing various sedentary tasks (across a few different items asking about TV watching, computer use, and video game playing), and they had options along a scale (ranging from "0 hours", "1/2 an hour", to "5+ hours"). Unfortunately, the values held by the variables don't correspond with the number of hours (e.g., 2 = 0.5 hours, 3 = 1 hour, 4 = 2 hours). In addition, the items asked about a normal weekday (and separate items asked about a normal weekend day), and the goal was to get an average number of hours *per week*.

To calculate this value, two things needed to be done. First, a conversion needed to happen to convert the values in the relevant items to actual number of hours. Simultaneously, the weekday hours needed to be summed as did weekend day hours (across TV watching, computer use, and video game playing). In order to do this, DO IF and COMPUTE can be combined. Here's the first piece:

COMPUTE avgWeekSed = 0. DO IF (chrb1a = 2). COMPUTE avgWeekSed = avgWeekSed + 0.5. END IF. DO IF (chrb1a = 3). COMPUTE avgWeekSed = avgWeekSed + 1. END IF. DO IF (chrb1a = 4). COMPUTE avgWeekSed = avgWeekSed + 2. END IF. DO IF (chrb1a = 5). COMPUTE avgWeekSed = avgWeekSed + 3. END IF. DO IF (chrb1a = 6). COMPUTE avgWeekSed = avgWeekSed + 4. END IF. DO IF (chrb1a = 7). COMPUTE avgWeekSed = avgWeekSed + 5. END IF.

First, "COMPUTE avgWeekSed = 0." creates the new variable, avgWeekSed, and starts it at 0. After that, various DO IF statements are used to specify when different computations should be done. If chrb1a equals 2, then avgWeekSed should have 0.5 added to it. If chrb1a equals 3, then avgWeekSed should have 1 added to it. And so on.

Notice, there is an END IF following the COMPUTE line in each of these cases. DO IF requires a closing END IF so SPSS knows how many calculations to run for the participants who meet the criteria in the DO IF statement; without it, it will try to run everything after the DO IF once it finds someone who meets the criteria. (There is technically an ELSE IF in SPSS, but I've had mixed luck with it working properly so I'm not using it here.)

So, with that understanding of how the above syntax works, we can expand it to include the other items for weekdays, and the weekend variables:

*Calculate sedentary behavior. COMPUTE avgWeekSed = 0. DO IF (chrb1a = 2). COMPUTE avgWeekSed = avgWeekSed + 0.5. END IF. DO IF (chrb1a = 3). COMPUTE avgWeekSed = avgWeekSed + 1. END IF. DO IF (chrb1a = 4). COMPUTE avgWeekSed = avgWeekSed + 2. END IF. DO IF (chrb1a = 5). COMPUTE avgWeekSed = avgWeekSed + 3. END IF. DO IF (chrb1a = 6). COMPUTE avgWeekSed = avgWeekSed + 4. END IF. DO IF (chrb1a = 7). COMPUTE avgWeekSed = avgWeekSed + 5. END IF. DO IF (chrb1b = 2). COMPUTE avgWeekSed = avgWeekSed + 0.5. END IF. DO IF (chrb1b = 3). COMPUTE avgWeekSed = avgWeekSed + 1. END IF. DO IF (chrb1b = 4). COMPUTE avgWeekSed = avgWeekSed + 2. END IF. DO IF (chrb1b = 5). COMPUTE avgWeekSed = avgWeekSed + 3. END IF. DO IF (chrb1b = 6). COMPUTE avgWeekSed = avgWeekSed + 4. END IF. DO IF (chrb1b = 7). COMPUTE avgWeekSed = avgWeekSed + 5. END IF. DO IF (chrb1c = 2). COMPUTE avgWeekSed = avgWeekSed + 0.5. END IF. DO IF (chrb1c = 3). COMPUTE avgWeekSed = avgWeekSed + 1. END IF. DO IF (chrb1c = 4). COMPUTE avgWeekSed = avgWeekSed + 2. END IF. DO IF (chrb1c = 5). COMPUTE avgWeekSed = avgWeekSed + 3. END IF. DO IF (chrb1c = 6). COMPUTE avgWeekSed = avgWeekSed + 4. END IF. DO IF (chrb1c = 7). COMPUTE avgWeekSed = avgWeekSed + 5. END IF. COMPUTE avgWeekSed = avgWeekSed * 5. *Compute weekend. COMPUTE avgWkendSed = 0. DO IF (chrb2a = 2). COMPUTE avgWkendSed = avgWkendSed + 0.5. END IF. DO IF (chrb2a = 3). COMPUTE avgWkendSed = avgWkendSed + 1. END IF. DO IF (chrb2a = 4). COMPUTE avgWkendSed = avgWkendSed + 2. END IF. DO IF (chrb2a = 5). COMPUTE avgWkendSed = avgWkendSed + 3. END IF. DO IF (chrb2a = 6). COMPUTE avgWkendSed = avgWkendSed + 4. END IF. DO IF (chrb2a = 7). COMPUTE avgWkendSed = avgWkendSed + 5. END IF. DO IF (chrb2b = 2). COMPUTE avgWkendSed = avgWkendSed + 0.5. END IF. DO IF (chrb2b = 3). COMPUTE avgWkendSed = avgWkendSed + 1. END IF. DO IF (chrb2b = 4). COMPUTE avgWkendSed = avgWkendSed + 2. END IF. DO IF (chrb2b = 5). COMPUTE avgWkendSed = avgWkendSed + 3. END IF. DO IF (chrb2b = 6). COMPUTE avgWkendSed = avgWkendSed + 4. END IF. DO IF (chrb2b = 7). COMPUTE avgWkendSed = avgWkendSed + 5. END IF. DO IF (chrb2c = 2). COMPUTE avgWkendSed = avgWkendSed + 0.5. END IF. DO IF (chrb2c = 3). COMPUTE avgWkendSed = avgWkendSed + 1. END IF. DO IF (chrb2c = 4). COMPUTE avgWkendSed = avgWkendSed + 2. END IF. DO IF (chrb2c = 5). COMPUTE avgWkendSed = avgWkendSed + 3. END IF. DO IF (chrb2c = 6). COMPUTE avgWkendSed = avgWkendSed + 4. END IF. DO IF (chrb2c = 7). COMPUTE avgWkendSed = avgWkendSed + 5. END IF. COMPUTE avgWkendSed = avgWkendSed * 2. *Combine. COMPUTE avgSed = avgWeekSed + avgWkendSed. EXECUTE.

As you can see above, the DO IF chain was repeated with the chrb variable adjusted as needed. By using the COMPUTE function to add the right values, an ongoing sum of the hours was held by avgWeekSed and avgWkendSed. Then, because those represented average *days*, they were multiplied by the relevant number (5 weekdays, 2 weekend days) and summed to create avgSed.

You can also see that I included comments so I could keep track of what was happening at each stage, and there is an EXECUTE command at the very end. (The EXECUTE command is necessary, but only one is needed at the end in order for the entire block of syntax to work.)

So, those are the basics of how COMPUTE and DO IF can be used to calculate new variables. These two can be paired in many different ways, including the creation of syntax for getting summary scores based on a manual which details how to hand score a measure.

I don't have an example here, but you can also nest DO IF statements within one another. There is also another command, RECODE, which is useful, and there is a very helpful ~ operator, but those will be for a future post.

Hopefully this helps to make SPSS syntax more friendly and useful. If you have any questions, or want anything else covered, let me know in the comments!

*Note: SPSS supports several functions that make some computations easier, such as MEAN() and SUM(). You can use searches to determine which functions are available.