SPSS Tutorials

BASICS REGRESSION T-TEST CHI-SQUARE TEST ANOVA

Concatenate (General Concept)

Concatenating two or more strings is creating a new string consisting of the characters of the first string followed by the characters of the subsequent string(s) in their original order.

SPSS CONCAT Function

SPSS Concatenate Syntax Example

*1. Create single case test data.

data list free/first_name(a5) last_name(a5) cars.
begin data
'John' 'Doe' 2
'Chris' 'Colt' 1
end data.

*2. Concatenate first and last name.

string full_name(a10).
compute full_name = concat(rtrim(first_name),' ',rtrim(last_name)).
exe.

*3. Convert cars to string and concatenate into sentence.

string has_cars (a25).
compute has_cars = concat(rtrim(full_name),' has ',string(cars,f1),' cars.').
exe.

*4. Correct plural if needed.

if cars eq 1 has_cars = replace(has_cars,'cars','car').
exe.
SPSS ConcatenateSPSS Concatenation Result

Concatenating in Python

Python Concatenate Example

* Concatenate strings and number into sentence.

begin program.
firstName = "John"
lastName = "Doe"
cars = 2
sentence = firstName + ' ' + lastName + ' has ' + str(cars) + ' cars.'
print sentence
end program.

Previous tutorial: Command Syntax Reference

Next tutorial: Escape Sequence (General Concept)

Let me know what you think!

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

This tutorial has 1 comment!