SPSS tutorials website header logo SPSS TUTORIALS BASICS ANOVA REGRESSION FACTOR CORRELATION

Substring – What is it & How to Get it?

A substring is a subset of characters from a string. Extracting substrings in SPSS is done with CHAR.SUBSTR (SPSS versions 16+) or just SUBSTR (SPSS versions 15-). CHAR.SUBSTR takes two or three arguments as shown by the minimal example below.

SPSS CHAR.SUBSTR - Minimal Example

COMPUTE var_2 = CHAR.SUBSTR(var_1,3,2).
The three arguments mean the following:

Altogether, this first example implies that var_2 will consist of characters 3 and 4 of var_1.

SPSS Substring Syntax Examples

The examples below use webdesigners.sav.

cd 'C:\xampp\htdocs\spss-tutorials\wp-content\themes\spss-tutorials-10\dont_upload\@external files\SPSS\test_data_creation\webdesigners'.

get file 'webdesigners.sav'.

string fname lname company tld (a30).

compute fname = char.substr(email,1,1).
execute.

compute fname = char.substr(email,3,2).
execute.

compute fname = char.substr(email,1,char.index(email,'.') - 1).
execute.

compute fname = concat(upper(char.substr(fname,1,1)),char.substr(fname,2)).
execute.

compute lname = char.substr(email,char.index(email,'.') + 1,char.index(email,'@') - 1 - char.index(email,'.')).
execute.

compute lname = concat(upper(char.substr(lname,1,1)),char.substr(lname,2)).
execute.



compute company = char.substr(email,char.index(email,'@') + 1).
execute.

compute tld = char.substr(company,char.rindex(company,'.') + 1).
execute.


document 'bal'.

document 'bol'.

display documents.

Explanation

Python Substring Examples

begin program.
pets = 'Cat Dog Rat'
print pets[4:7]
print pets[pets.rfind(" ") +1:]
end program.

Explanation

Tell us what you think!

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

THIS TUTORIAL HAS 5 COMMENTS: