Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data concatenation in SPSS

Tags:

spss

I have two data columns in SPSS

hhid = 1 2 3

carid = 26 27 28

I want to create a new column which concatenates both the hhid and carid and gives me an output

Newid = 1_26 2_27 3_28

I am new to SPSS, I appreciate your help.

like image 227
user3637176 Avatar asked Dec 20 '25 03:12

user3637176


1 Answers

The following code assumes, that your id variables are numeric. Otherwise just delete the STRING functions from the COMPUTE command.

* generate a string variable of 17 characters. Adjust length as you wish.
STRING Newid (A17).
COMPUTE Newid = CONCAT(LTRIM(STRING(hhid,F8)), "_", LTRIM(STRING(carid,F8))).
EXECUTE.

Explanation: The STRING(var,format) function transforms the numeric value of "var" into a string of the given format. In the code above the format is an 8 digit number without any decimals. The Output of this function is an 8 character long string with leading blanks. To delete the leading blanks, this function is wrapped in the LTRIM(string) function.

like image 70
mirirai Avatar answered Dec 21 '25 21:12

mirirai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!