SPSS Tutorials

BASICS REGRESSION T-TEST ANOVA CORRELATION

SPSS REPLACE Function

Definition

SPSS REPLACE replaces a substring in a string by a different (possibly empty) substring.

SPSS Replace - Removing Spaces

SPSS Replace Function URLs are created from "title" by using REPLACE. The syntax below demonstrates how to do this.

We have a dataset holding the titles of web pages and we'd like to convert these to URLs. For one thing, we don't like spaces in URLs. The syntax below shows how to remove them. Step 1 creates a tiny dataset (just run and otherwise ignore it) and step 3 demonstrates how to remove spaces using REPLACE.

SPSS Replace Syntax Example 1

*1. Create mini dataset.

data list free/title(a50).
begin data
"Suffix All Variable Names"
"SPSS Syntax - Six Reasons you Should Use it"
"Reverse Code Variables with Value Labels"
end data.

*2. Declare new string variable for URL.

string url(a50).

*3. URL is title with spaces removed.

compute url = replace(title,' ','').
exe.

SPSS Replace - Replacing Spaces

SPSS Replace Syntax Example 2

*4. URL is title with spaces replaced by dashes.

compute url = replace(rtrim(title),' ','-').
exe.

*5. Replace triple dashes by single dashes.

compute url = replace(url,'---','-').
exe.

*6. Convert URL to lowercase.

compute url = lower(url).
exe.

*7. Delete values from URL.

compute url = ''.
exe.

*8. Compute URL in one go.

compute url = lower(replace(replace(rtrim(title),' ','-'),'---','-')).
exe.

Previous tutorial: SPSS LTRIM Function

Next tutorial: SPSS RINDEX Function

Let me know what you think!

*Required field. Your comment will show up after approval from a moderator.

This tutorial has 2 comments