[Skip Global Navigation]

SPSS Tech Tips

SPSS Direct Home

Using the REPLACE function with SPSS 14.0 to clean up data sets

This is a complimentary tip available only to SPSS Direct subscribers.

Question

I have a dataset that needs some clean up. I have spaces and characters that need to be removed, and I need to replace a specific word within a variable with another word. Is there a way
I can do this using syntax?

Answer

SPSS has several string functions you can use to manipulate data. Beginning with SPSS 14.0, there is a new function called REPLACE that should help address these problems.

REPLACE can take a string character and replace it with another.

Here are some examples:


Figure 1: There are several strings of characters that need to be replaced.
Click image to enlarge.

To remove the spaces between the values in variable “id” and remove the quote marks under “responses,” replace these characters with null values.

COMPUTE id = REPLACE(id," ", "").
EXECUTE.

COMPUTE responses = REPLACE(responses,'"',"").
EXECUTE.


Figure 2: You’ve successfully replaced the spaces between the variables under the variable “id” and removed the quote marks around the characters under “responses.”
Click image to enlarge.

To replace capital letters with lowercase letters:

IFv(INDEX(responses,'ABODES'))>0 responses=REPLACE(responses,'ABODES','abodes').
EXECUTE.


Figure 3: The capital letters in the third “responses” line (ABODES) have been replaced with lower-case letters.
Click image to enlarge.

To replace a single letter:

IF INDEX(responses,'Abodes'))>0 responses=REPLACE(responses,'A','a').

IF (INDEX(responses,'abodeS'))>0 responses=REPLACE(responses,'S','s').
EXECUTE.


Figure 4: The capital letter in the fourth “responses” line (abodeS) has been replaced with a lower-case “s.”
Click image to enlarge.

Finally, you can also replace a word with another word:

IF (INDEX(responses,'abodes'))>0 house=REPLACE(responses,'abodes','homes').
EXECUTE.


Figure 5: “Abodes” has been replaced by a new word, “homes.”
Click image to enlarge.