The CONDENSE statement is used to remove unnecessary whitespace from a character-type data object (usually of type C - fixed-length string). It modifies the content of the specified variable directly in place.
Specifically, CONDENSE performs the following actions in its default behavior:
- Removes all leading blanks.
- Replaces sequences of multiple internal blanks (blanks between visible characters) with exactly one single blank.
Important: By default, CONDENSE does not remove trailing blanks at the end of the string!
Syntax
CONDENSE <variable> [NO-GAPS].<variable>: The name of the character-type variable (usually typeC) whose content is to be condensed.[NO-GAPS](Optional): An addition that changes the behavior: WhenNO-GAPSis specified, all blanks (leading, internal, and trailing) are completely removed from the variable.
Behavior in Detail
Without NO-GAPS (CONDENSE <variable>.)
" Hello World "becomes"Hello World "(if<variable>is e.g.,C(30). The trailing blanks up to the field length remain.)" "becomes" "(only blanks, left-aligned with blanks up to field length)" ABC "becomes"ABC ""A B C"becomes"A B C "
With NO-GAPS (CONDENSE <variable> NO-GAPS.)
" Hello World "becomes"HelloWorld "(rest filled with blanks)" "becomes" "(empty field)" ABC "becomes"ABC ""A B C"becomes"ABC "
Applicability (Type C vs. STRING)
Type C (fixed-length string): This is the primary use case for CONDENSE. Type-C fields are often right-padded with blanks after assignments or may contain leading/multiple internal blanks through processing.
Type STRING (variable-length string): CONDENSE can also be applied to strings.
- Standard
CONDENSEremoves leading blanks and reduces internal multiple blanks to one. Trailing blanks that are part of the string value are preserved. CONDENSE ... NO-GAPSremoves all blanks from the string.- For strings, there are often more specific and modern alternatives like
SHIFT ... DELETING LEADING/TRAILING space.,REPLACE, or string templates/functions that offer more control.
Examples
1. Standard CONDENSE with Type C
DATA text_c TYPE c LENGTH 40 VALUE ' First Part second Part '.
WRITE: / 'Type C Before :', |'{ text_c }'|.CONDENSE text_c.WRITE: / 'Type C After :', |'{ text_c }'|.Output:
Type C Before : '{ First Part second Part }'Type C After : '{First Part second Part }'(Leading blanks removed, internal reduced to 1, trailing blanks remain up to length 40).
2. CONDENSE with NO-GAPS on Type C
DATA text_c_ng TYPE c LENGTH 40 VALUE ' First Part second Part '.
WRITE: / 'Type C NG Before :', |'{ text_c_ng }'|.CONDENSE text_c_ng NO-GAPS.WRITE: / 'Type C NG After :', |'{ text_c_ng }'|.Output:
Type C NG Before : '{ First Part second Part }'Type C NG After : '{FirstPartsecondPart }'(All blanks removed, the rest is left-aligned).
3. CONDENSE on Type STRING
DATA text_s TYPE string VALUE ` String with lots of space `.
WRITE: / 'String Before :', |'{ text_s }'|.CONDENSE text_s.WRITE: / 'String After :', |'{ text_s }'|. " Leading removed, internal reduced, trailing remains
DATA text_s_ng TYPE string VALUE ` String with lots of space `.CONDENSE text_s_ng NO-GAPS.WRITE: / 'String After NG:', |'{ text_s_ng }'|. " All blanks removedOutput:
String Before : '{ String with lots of space }'String After : '{String with lots of space }'String After NG: '{Stringwithlotsofspace}'Important Notes / Best Practice
CONDENSEmodifies the passed variable directly. Save the original value beforehand if it’s still needed.- The most common misconception is that
CONDENSEremoves trailing blanks. This is false in the standard behavior. OnlyCONDENSE ... NO-GAPSremoves all blanks. - To remove only trailing blanks, use
SHIFT <str> RIGHT DELETING TRAILING space.for strings, or for type C, for example, assignment to a string and back, or special function modules. CONDENSEis useful for cleaning up input fields or data from external sources that may contain unwanted blanks.- For complex string manipulations, modern string functions or operators are often a better choice than
CONDENSE.