Summary
SPSS LTRIM
(left trim) removes leading spaces from string values. These occur especially when converting numbers to strings by using the stringfunction.The reason this occurs is that SPSS' default alignment for numeric variables is right and string values are always padded with spaces up to the length of their containing variable. For removing trailing rather than leading spaces, see RTRIM.

SPSS Ltrim Example
The syntax below demonstrates a situation where you'll like to use LTRIM
. Running step 1 simply creates a mini dataset. Step 3 uses CONCAT without LTRIM
and thus results in a values containing undesired spaces. Finally, step 4 shows how to avoid these by using LTRIM
. The results of steps 3 and 4 are shown in the above screenshot.
SPSS Ltrim Syntax Example
*1. Create mini dataset.
data list free / id(f5).
begin data
1 12 123 1234 12345
end data.
*2. Declare new string variable.
string sentence(a10).
*3. Results in undesired spaces before numbers.
compute sentence = concat("id = ",string(id,f5)).
exe.
*4. Ltrim undesired spaces and then concatenate.
compute sentence = concat("id = ",ltrim(string(id,f5))).
exe.
data list free / id(f5).
begin data
1 12 123 1234 12345
end data.
*2. Declare new string variable.
string sentence(a10).
*3. Results in undesired spaces before numbers.
compute sentence = concat("id = ",string(id,f5)).
exe.
*4. Ltrim undesired spaces and then concatenate.
compute sentence = concat("id = ",ltrim(string(id,f5))).
exe.
THIS TUTORIAL HAS 4 COMMENTS:
By Joe Feinglass on December 31st, 2020
just want to delete 6 leading characters of a string var-what's with the concat stuff? Unable to replicate
By Ruben Geert van den Berg on December 31st, 2020
Hi Joe!
Try the CHAR.SUBSTR function as in
*Create test data.
data list free/mystring(a20).
begin data
abcdefghijklm
end data.
*Remove first 6 chars.
compute mystring = char.substr(mystring,7)./*Keep characters 7 through end of string value.
execute.
Hope that helps!
SPSS tutorials
By Johnny Cash on March 29th, 2022
What do I do if I ALREADY HAVE a String and just want to trim it?
Let's say the name of my string variable is "text". I've got some 200 values, some of them start with a blank space like " hello!"
How do i trim this "text"-variable? What is the right syntax?
I really don't get it.
By Ruben Geert van den Berg on March 30th, 2022
Hi Johnny!
Try the syntax below. If that doesn't help, please let me know.
data list free/text(a20).
begin data
' What' ' do' ' I' ' do' ' if' ' I' ' ALREADY' ' HAVE' ' a' ' String' ' and' ' just' ' want' ' to' ' trim' ' it?'
end data.
compute text = ltrim(text).
execute.