SAS9

Mastering TRIM and Concatenation

Simon 21 views

When processing text data in Base SAS©, there are two constantly recurring operations: cleaning up unnecessary spaces and joining parts of text.

A simple question on the forum ('What is the difference between TRIM and CONCAT?') raised several important technical points about handling character strings.

1. TRIM: The Space Remover

In SAS©, a character variable has a fixed length. If you define a 10-character variable and store the word "CAT" in it, SAS© pads the remaining 6 characters with spaces. This is called trailing blanks.

The TRIM function is used to remove these trailing spaces.

  • Example: If VAR1 has the value '1234 ' (with spaces). VAR2 = TRIM(VAR1); results in '1234'.

Technical Note: As a response clarifies, TRIM does not affect spaces before the text (leading blanks). To remove spaces before and after the text, TRIM(LEFT(value)) was historically used. Today, the function STRIP does this in a single step.

2. Concatenation: The Assembler

Concatenation is the process of joining two character strings together.

There was some confusion in the original thread here. The user asks for a definition of the "CONCAT" function.

  • In standard Base SAS©, there was historically no function simply called "CONCAT". The operation is performed using the operator || (double vertical bar) or !!.

  • The complex technical answer in the forum (which mentions "character matrices") refers to the SAS©/IML module (matrix language), which is irrelevant for most classic users.

1Nom = "Bond";
2Prenom = "James";
3Full = Nom || Prenom;
4/* Résultat : "Bond James" (Les espaces de 'Bond' sont conservés !) */

3. The Winning Duo: TRIM + Concatenation

This is the classic beginner's trap. Since the concatenation operator || retains the spaces of the first variable, the result often contains unwanted "gaps".

That's why these two concepts are almost always used together.

Note :
The old method (Classic):
1Full = TRIM(Nom) || " " || Prenom;
2 
3/* On nettoie 'Nom', on ajoute un espace manuel, on colle 'Prenom' */
4/* Résultat : "Bond James" */

4. The Modern Solution (CAT functions)

Since version 9, SAS© has introduced a family of functions that significantly simplify life and make the explicit use of TRIM and || less necessary for simple concatenation.

Instead of juggling with TRIM, use the CAT functions:

  • CATS(arg1, arg2): Concatenates and Strips (removes leading/trailing spaces).

    • Equivalent to: TRIM(LEFT(arg1)) || TRIM(LEFT(arg2))

  • CATX('sep', arg1, arg2): Concatenates, Strips, and inserts a separator if text is present.

    • This is the recommended method today.

Note :
Modern Example:
1Full = CATX(" ", Nom, Prenom);
2 
3/* Résultat propre : "Bond James" */
Function / OperatorAction
TRIM(var)Removes spaces only at the end.
STRIP(var)Removes spaces at the beginning and end.
CATS(a, b)Joins a and b and removes all unnecessary spaces.
CATX(' ', a, b)Joins a and b and cleanly inserts a space in between.