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
- Concatenating in SPSS is done by the
CONCAT
function. - The most basic usage is
COMPUTE A = CONCAT(B,C,D)
. - Note that you can concatenate only strings. For concatenating numbers, first convert them to strings, for example by using the stringfunction.
- Note that you may sometimes need RTRIM within
CONCAT
as demonstrated by the syntax below.
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.
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.

Concatenating in Python
- In Python, the
+
operator is used for concatenating. - As in SPSS, you can only concatenate strings, not numbers in Python. Convert numbers into strings before concatenating them.
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.
begin program.
firstName = "John"
lastName = "Doe"
cars = 2
sentence = firstName + ' ' + lastName + ' has ' + str(cars) + ' cars.'
print sentence
end program.
THIS TUTORIAL HAS 6 COMMENTS:
By Dr Rajendran on December 15th, 2015
excellent
By Mehari Gizaw on July 26th, 2018
Recognizing your outshining work
By Sabeen saif on February 15th, 2020
Plz teach how to concatenate numeric variables
By Ruben Geert van den Berg on February 15th, 2020
Are you able to work from syntax? How many digits do the numbers have?
By Zachary Gillette on December 3rd, 2020
How to concatenate value labels of numeric variables? Example, Brand has value labels 1 "Chevy" 2 "Ford"... and Cars is number owned, and I want a variable that is essentially string "Ford 2" if both variables have a value of 2.