Master SAS in 30 days!

A jargon-free, easy-to-learn SAS base course that is tailor-made for students with no prior knowledge of SAS.

Trim Function: How to Remove Trailing Spaces from Character Values

The TRIM function is used to remove trailing spaces from character values. It is usually used when concatenating variables.

Example

The data set above contains 2 columns: VAR1 and VAR2.

Both variables have a length of 10:
 
You can create the data set using the code below:
 

data string;
length var1 var2 $ 10;
input var1 $ var2 $;
datalines;
ABC DEF
abc def
1234 5678
;
run;

Remove Trailing Spaces from Text

When concatenating variables in SAS, you might get unwanted spaces between the values being combined.

Example

data string2;
set string;
comb = var1 || var2;
run;

The VAR1 and VAR2 variables are concatenated into the COMB variable.

There are blank spaces between the two values:
 

The blank spaces come from the trailing spaces from the VAR1 variable.

You can use the TRIM function to remove them:

Example

data string2;
set string;
comb = trim(var1) || var2;
run;

The TRIM function removes the trailing spaces from the VAR1 variable.

The character values are concatenated with the unwanted spaces removed:

Become a Certified SAS Specialist

Get access to two SAS base certification prep courses and 150+ practice exercises

Master SAS in 30 Days

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
iconmail

Get latest articles from SASCrunch

SAS Base Certification Exam Prep Course

Two Certificate Prep Courses and 300+ Practice Exercises