SPSS tutorials website header logo SPSS TUTORIALS VIDEO COURSE BASICS ANOVA REGRESSION FACTOR

SPSS String Variables Tutorial

SPSS CHAR.SUBSTR - Example

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).
exe.

*3. Convert to upper case.

compute n1 = upcase(n1).
exe.

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

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

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

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

*6. Substitution: concatenate results from previous attempts.

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

2. Correcting Surname Prefixes

*1. Remove leading spaces.

compute n2 = ltrim(s2).
exe.

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

compute n2 = lower(ltrim(s2)).
exe.

*3. Replace double spaces by single spaces.

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

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))).
exe.

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

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

*3. Correct concatenation but spaces should be inserted.

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

*4. Final concatenation.

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

*5. Replace double spaces by single spaces.

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

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).
exe.

*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),' ').
exe.

Tell us what you think!

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

THIS TUTORIAL HAS 18 COMMENTS: