Tech Tip: Concatenating String Variables in IBM SPSS Statistics

6
Concatenating String Variables in IBM SPSS Statistics

Transcript of Tech Tip: Concatenating String Variables in IBM SPSS Statistics

Page 1: Tech Tip: Concatenating String Variables in IBM SPSS Statistics

Concatenating String Variables

in IBM SPSS Statistics

Page 2: Tech Tip: Concatenating String Variables in IBM SPSS Statistics

What is Concatenating?

Merging two or more sting variables into a single variable is

called concatenating variables

This function can be very useful if you are working with string

data in SPSS

One common use of this function is to bring first name and

last name from two variables into one single full name

variable

2

Page 3: Tech Tip: Concatenating String Variables in IBM SPSS Statistics

How to Concatenate?

String Variables can be concatenated in IBM SPSS Statistics

using the CONCAT function

The following syntax demonstrates using a compute

command to bring three single name variables together into

a single variable, which combines the three into a full name

variable

3

Page 4: Tech Tip: Concatenating String Variables in IBM SPSS Statistics

The first piece of syntax simply creates a datafile in SPSS to

be worked with -

• DATA LIST

/First_Name 1-4(A) Initial 5-6(A) Last_Name 6-20(A).

BEGIN DATA

Rob E Barry

Ian L O'Connell

Ted W Payne

Rod G Early

Con T Not

Cat P Murphy

Pat C Clifford

Bob A O'Sullivan

END DATA.

CONCAT Function in Syntax

4

Page 5: Tech Tip: Concatenating String Variables in IBM SPSS Statistics

The STRING function below creates a string variable with 64

characters allocated to it-

• STRING Full_Name(A64)

The CONCAT Function is then used to combine the three

variables ‘First_Name’, ‘Initial’ and ‘Last_Name’

Notice how CONCAT needs to use the functions ‘rtrim’ and

‘trim’, which remove any blank spaces on the left side (ltrim)

and the right side (rtrim)

• COMPUTE Full_Name =CONCAT(rtrim(ltrim(First_Name)),"

",rtrim(ltrim(Initial))," ",rtrim(ltrim(Last_Name))).

EXEUTE.

CONCAT Function in Syntax

5