TRIM Function

Removes trailing blanks from a character string, and returns one blank if the string is missing.

Category: Character
Restriction: I18N Level 0 functions are designed for use with Single Byte Character Sets (SBCS) only.
Tip: DBCS equivalent function is KTRIM Function in SAS National Language Support (NLS): Reference Guide.



Required Argument


specifies a character constant, variable, or expression.


Length of Returned Variable

In a DATA step, if the TRIM function returns a value to a variable that has not previously been assigned a length, then that variable is given the length of the argument.

The Basics

TRIM copies a character argument, removes trailing blanks, and returns the trimmed argument as a result. If the argument is blank, TRIM returns one blank. TRIM is useful for concatenating because concatenation does not remove trailing blanks.
Assigning the results of TRIM to a variable does not affect the length of the receiving variable. If the trimmed value is shorter than the length of the receiving variable, SAS pads the value with new blanks as it assigns it to the variable.


The TRIM and TRIMN functions are similar. TRIM returns one blank for a blank string. TRIMN returns a string with a length of zero for a blank string.


Example 1: Removing Trailing Blanks

These statements and this data line produce these results:
data test;
   input part1 $ 1-10 part2 $ 11-20;
   put hasblank;
   put noblank;
Data Line
apple     sauce
apple     sauce

Example 2: Concatenating a Blank Character Expression

SAS Statement
x="A"||trim(" ")||"B"; put x;
  A B
x="    "; y=">"||trim(x)||"<"; put y;
   > <