SPSS Syntax Part 3: RECODE and ~
While you can do a lot of different computations using the DO IF and COMPUTE commands that I detailed in my last post, there are two more pieces of syntax that are useful to know.
First, RECODE enables you to change the values in a variable from one thing to another, similar to what we did with the DO IF and COMPUTE commands in my last post. (The reason we didn't use RECODE in that post is because we also wanted to keep a sum of the values).
One of the key uses for RECODE is when you need to "reverse score" an item. "Reverse scoring" is when you have an item that is in one direction (e.g., higher values mean higher levels of happiness) and you want to switch it around (e.g., higher values mean lower levels of happiness) so it can be used as an indication of something (e.g., depression). Having items that need to be "reverse scored" is a common measure development tool, which I might discuss in a future post.
To use RECODE, you have to: 1) specify the variable you'll be recoding, 2) indicate how the values should be changed, and 3) tell SPSS what variable the new values should be saved into (optional). Here is an example piece of syntax:
RECODE var (5 = 1) (4 = 2) (3 = 3) (2 = 4) (1 = 5) INTO var_recode. EXECUTE.
Here, we're calling the RECODE function and telling it that the variable "var" holds the original values. We then specify how the different values should be recoded (e.g., a 5 will become a 1). In this snippet I have each specified recode on a new line, though they can be on the same line; I just find it easier to read through when it's in this format.
The INTO piece is optional (though highly recommended so you aren't overwriting anything!) and indicates that the new values should be saved into the "var_recode" variable.
And, finally, we have EXECUTE, which SPSS requires for RECODE (because why not?).
So that's the RECODE function. It's pretty simple, but a useful tool to have.
Next is the "~" operator. In SPSS, "~" functions as a NOT operator. Basically, it takes a function (which will return a True or False) and flips it.
For example, if you want to check if a variable equals 2, then you can use something like:
DO IF(var = 2)
But if you want to see if a variable does not equal 2, then you can use:
DO IF(var ~= 2)
In the second example, the code following the DO IF will only run if the value of "var" is not 2. ("~=" in SPSS is equivalent to "!=" that is used by other syntax languages).
The main use I have found for the "~" operator is to check for missing data. If you only want to run an analysis on participants who have complete data on all of the variables, you can tell SPSS to only do a computation if the variables are not missing. SPSS contains a MISSING() function that will be True if a variable has missing data, so to flip it around we can do this:
DO IF(~MISSING(var) AND ~MISSING(var2))
By adding the "~", we are telling SPSS that the DO IF should only be triggered if both of those variables are not missing. (In the parentheses of a function, you can include things like AND and OR to include multiple qualifiers).
And that's about it! Hopefully this information on RECODE and ~ has been useful.
Have questions about how to do other things using SPSS syntax? Let me know in the comments, and I'll write a post to let you know!