By Ruben Geert van den Berg on September 11, 2013 under SPSS String Variables.

SPSS String Variables Tutorial


Working with string variables in SPSS is pretty straightforward if one masters some basic string functions. This tutorial will quickly walk you through the important ones.

SPSS Main String Functions

SPSS Syntax Example

We asked respondents to type in their first name, surname prefix and last name. We'd like to combine these into full names and correct some irregularities such as incorrect casing and double spaces. For creating some test data, close all open datasets and run the syntax below.

*Create mini test dataset.

set unicode off.
data list free/s1 s2 s3 (3a20).
begin data
'ANNEKE' ' VAN DEN ' 'BERG' 'daan' '' 'balvert' 'a' '' 'b'
end data.

1. Correcting First Names

*1. Declare new string variables.

string n1 to n4 (a20).

*2. Extract first letter of first name.

compute n1 = char.substr(s1,1,1).

*3. Convert to upper case.

compute n1 = upcase(n1).

*4. Substitution: use substring function within upcase function.

compute n1 = upcase(char.substr(s1,1,1)).

*5. Extract remaining letters and convert to lower case.

compute n1 = lower(char.substr(s1,2)).

*6. Substitution: concatenate results from previous attempts.

compute n1 = concat(upcase(char.substr(s1,1,1)),lower(char.substr(s1,2))).

2. Correcting Surname Prefixes

*1. Remove leading spaces.

compute n2 = ltrim(s2).

*2. Substitution: remove leading spaces and convert to lower case.

compute n2 = lower(ltrim(s2)).

*3. Replace double spaces by single spaces.

compute n2 = replace(n2,' ',' ').

3. Combining First and Last Names

*1. Reuse capitalization syntax used for first name on last name.

compute n3 = concat(upcase(char.substr(s3,1,1)),lower(char.substr(s3,2))).

*2. If rtrim is omitted, concat doesn't seem to work.

compute n4 = concat(n1,n2,n3).

*3. Correct concatenation but spaces should be inserted.

compute n4 = concat(rtrim(n1),rtrim(n2),rtrim(n3)).

*4. Final concatenation.

compute n4 = concat(rtrim(n1),' ',rtrim(n2),' ',rtrim(n3)).

*5. Replace double spaces by single spaces.

compute n4 = replace(n4,' ',' ').

4. Flag Single Letter Names

*1. Find short first/last names from separate name components.

compute flag_1a = char.length(s1).
compute flag_1b = char.length(s3).

*2. Find short first/last names from combined names.

compute flag_2a = char.index(n4,' ') -1.
compute flag_2b = char.length(n4) - char.rindex(rtrim(n4),' ').

Related Tutorials

SPSS String Variables Basics

String variables are variables that hold zero or more text characters. This tutorial demonstrates their main properties by running simple examples on a test data file. Read more

Comment on this Tutorial

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

This Tutorial has 18 Comments