1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
* (Q)  I have a problem with CONCAT. I use the following code:

NEW FILE.
DATA LIST LIST /id .
BEGIN DATA
1
2
END DATA.

STRING v1 v2 v3 (A10).
COMPUTE v1 = "a".
COMPUTE v2 = CONCAT(v1,"b").
COMPUTE v3 = CONCAT("c",v1).
EXECUTE.

I wanted to concatenate some string variables to a new one, however,
if the first argument of CONCAT function was the variable itself,
then this method failed. The result in the Data Editor was

id       v1   v2   v3
1,00    a     a   ca
2,00    a     a   ca

but I think it should be

 id       v1   v2   v3
1,00    a    ab   ca
2,00    a    ab   ca


* (A) Posted to SPSS-X list by Raynald Levesque on 2004/03/06.

This is the expected behaviour.

v1 and v2 have format A10.
thus the result of
CONCAT(v1,"b") is
the 10 characters of v1 (the last 9 of which are blanks)
followed by "c"

In other words the result of CONCAT(v1,"b") has 11 characters.
Since v2 has room for only 10 characters, the command
COMPUTE v2=CONCAT(v1,"b").
assigns only the first 10 characters of the right hand side to v2.

The solution is to use
COMPUTE v2=CONCAT(RTRIM(v1),"b").

The RTRIM function removes the trailing blanks contained in v1. The
resulting string (after concatenation with "b") and only 2 characters and
these are assigned to v2.