Summary
By default, SPSS right pads string values with spaces up to the length of their containing string variables. You don't usually see this but it may complicate concatenating strings. Such complications are avoided by trimming off trailing spaces using RTRIM
(right trim). In Unicode mode, RTRIM
is applied automatically but it's fine to use it anyway.
SPSS Rtrim Example
The syntax below demonstrates two complications that may result from omitting RTRIM
. We recommend you run it and inspect the results after each step. Make sure you have no datasets open because they'll prevent SPSS from switching Unicode Mode off.
SPSS Rtrim Syntax Example
*1. Remember settings and switch Unicode mode off.
preserve.
set unicode off.
*2. Create mini dataset.
data list free / first last (2a10).
begin data
John Doe
end data.
*3. Declare new string variable.
string full(a10).
*4. Attempt 1. Concat does not seem to work.
compute full = concat(first,last).
exe.
*5. Increase string length.
alter type full(a20).
*6. Attempt 2. Results in excessive spaces.
compute full = concat(first,last).
exe.
*7. Attempt 3. Rtrim removes excessive spaces.
compute full = concat(rtrim(first),' ',rtrim(last)).
exe.
*9. Close all open data.
dataset close all.
new file.
*10. Restore system settings.
restore.
preserve.
set unicode off.
*2. Create mini dataset.
data list free / first last (2a10).
begin data
John Doe
end data.
*3. Declare new string variable.
string full(a10).
*4. Attempt 1. Concat does not seem to work.
compute full = concat(first,last).
exe.
*5. Increase string length.
alter type full(a20).
*6. Attempt 2. Results in excessive spaces.
compute full = concat(first,last).
exe.
*7. Attempt 3. Rtrim removes excessive spaces.
compute full = concat(rtrim(first),' ',rtrim(last)).
exe.
*9. Close all open data.
dataset close all.
new file.
*10. Restore system settings.
restore.
SPSS Rtrim Syntax Notes
- Wrapping all syntax between
PRESERVE.
andRESTORE.
ensures your system settings (in this case just Unicode Mode) don't change by running the syntax. - In step 4,
CONCAT
doesn't seem to work. However, the real problem is that the concatenation results in a string value of 20 characters for a 10 character string variable. In this case, SPSS discards the last 10 characters that don't fit into this string. - The entire 20 character result can be seen by increasing the length of
full
to 20 characters by using ALTER TYPE in step 5. - Before you can switch Unicode Mode on or off, make sure there's no open datasets. This is done in step 9.
- Use LTRIM in case you need to remove leading rather than trailing spaces.