How To Remove Trailing Spaces In Cobol

Trailing spaces in COBOL are a common data processing challenge. Inconsistent data formats can lead to errors in calculations, comparisons, and reports. This article provides practical COBOL code snippets for removing trailing spaces, focusing on efficiency and compatibility.
Identifying Trailing Spaces
Trailing spaces, also known as trailing blanks, are spaces that occur after the last non-blank character in a string. These spaces are often invisible but can affect data integrity. Examining raw data output, particularly character-based reports or file dumps, is crucial. Incorrect calculations, sorting issues, or unexpected program behavior can indicate the presence of trailing spaces.
Techniques for Removing Trailing Spaces
Several methods can be used to remove trailing spaces in COBOL. The choice of method depends on the specific requirements of the application and the desired level of performance.
Must Read
Using the INSPECT Statement
The INSPECT statement is a versatile COBOL command for character manipulation. To remove trailing spaces, the INSPECT REPLACING clause can be used. This method replaces all occurrences of a specific character (in this case, a space) from the rightmost position of a data item until a non-space character is encountered.
INSPECT MY-DATA-ITEM
REPLACING TRAILING SPACE BY LOW-VALUE.
This code snippet replaces trailing spaces in MY-DATA-ITEM with LOW-VALUE. LOW-VALUE represents the lowest value in the character set, effectively truncating the string. To ensure data integrity, define MY-DATA-ITEM as alphanumeric (PIC X). After this operation, you might need to move the data to another field to ensure correct display of the data.
A more comprehensive example with data definition:

DATA DIVISION.
WORKING-STORAGE SECTION.
01 MY-DATA-ITEM PIC X(20) VALUE 'ABC '.
01 CLEAN-DATA-ITEM PIC X(20).
PROCEDURE DIVISION.
MOVE MY-DATA-ITEM TO CLEAN-DATA-ITEM.
INSPECT CLEAN-DATA-ITEM
REPLACING TRAILING SPACE BY LOW-VALUE.
DISPLAY CLEAN-DATA-ITEM.
In this example, MY-DATA-ITEM contains the string 'ABC ' (ABC followed by five spaces). The INSPECT statement replaces these trailing spaces with LOW-VALUE in CLEAN-DATA-ITEM. The subsequent DISPLAY statement will show 'ABC' followed by low-values which may appear as blank depending on the terminal or output device.
Using Reference Modification
Reference modification allows accessing a substring of a data item. This technique involves finding the position of the last non-space character and then moving only the relevant portion of the string to another field.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 MY-DATA-ITEM PIC X(20) VALUE 'ABC '.
01 DATA-LENGTH PIC 9(02) COMP.
01 CLEAN-DATA-ITEM PIC X(20).
PROCEDURE DIVISION.
PERFORM VARYING DATA-LENGTH FROM 20 BY -1 UNTIL
DATA-LENGTH < 1 OR MY-DATA-ITEM(DATA-LENGTH:1) NOT = SPACE
END-PERFORM.
IF DATA-LENGTH > 0 THEN
MOVE MY-DATA-ITEM(1:DATA-LENGTH) TO CLEAN-DATA-ITEM(1:DATA-LENGTH)
ELSE
MOVE SPACE TO CLEAN-DATA-ITEM.
DISPLAY CLEAN-DATA-ITEM.
This code iterates through the string from right to left, determining the length of the non-space portion. The PERFORM VARYING loop starts from the end of the string (position 20) and decrements the DATA-LENGTH until a non-space character is found or the beginning of the string is reached. The IF statement then moves only the relevant portion of MY-DATA-ITEM to CLEAN-DATA-ITEM.
Using Intrinsic Functions (COBOL 2002 and later)
COBOL 2002 introduced intrinsic functions, which provide built-in functionality for string manipulation. The FUNCTION TRIM is specifically designed to remove leading and trailing spaces.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 MY-DATA-ITEM PIC X(20) VALUE ' ABC '.
01 CLEAN-DATA-ITEM PIC X(20).
PROCEDURE DIVISION.
MOVE FUNCTION TRIM(MY-DATA-ITEM, TRAILING) TO CLEAN-DATA-ITEM.
DISPLAY CLEAN-DATA-ITEM.
The FUNCTION TRIM(MY-DATA-ITEM, TRAILING) removes trailing spaces from MY-DATA-ITEM and moves the result to CLEAN-DATA-ITEM. The first argument is the input string, and the second argument specifies which spaces to remove (TRAILING, LEADING, or LEADING TRAILING or ALL). Note that using ALL removes leading and trailing spaces and embedded spaces, which might not be desired.
Combining Techniques
Combining techniques can provide optimized solutions. For instance, using INSPECT to initially replace trailing spaces with a unique character (e.g., '') followed by reference modification to truncate the string.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 MY-DATA-ITEM PIC X(20) VALUE 'ABC '.
01 DATA-LENGTH PIC 9(02) COMP.
01 CLEAN-DATA-ITEM PIC X(20).
PROCEDURE DIVISION.
INSPECT MY-DATA-ITEM
REPLACING TRAILING SPACE BY ''.
PERFORM VARYING DATA-LENGTH FROM 20 BY -1 UNTIL
DATA-LENGTH < 1 OR MY-DATA-ITEM(DATA-LENGTH:1) NOT = '*'
END-PERFORM.
IF DATA-LENGTH > 0 THEN
MOVE MY-DATA-ITEM(1:DATA-LENGTH) TO CLEAN-DATA-ITEM(1:DATA-LENGTH)
ELSE
MOVE SPACE TO CLEAN-DATA-ITEM.
DISPLAY CLEAN-DATA-ITEM.
This method leverages INSPECT for efficiency and reference modification for precision.
Performance Considerations
The performance of these techniques can vary based on the size of the data, the frequency of execution, and the COBOL compiler being used. The INSPECT statement is generally efficient for simple replacements. Reference modification involves looping, which can be slower for large strings. Intrinsic functions, when available, are often optimized for performance.

Testing different approaches with realistic data volumes is recommended to determine the most efficient method for a particular environment. Analyzing the generated object code can provide further insights into the performance characteristics of each technique.
Error Handling
Implement error handling to gracefully manage unexpected situations. For instance, checking for input strings that are entirely spaces or exceeding predefined length limits. Providing meaningful error messages can aid in debugging and data validation.
Before applying any modifications, it's vital to create backups of original data to prevent data loss. Employ thorough testing procedures to ensure that the chosen technique correctly removes trailing spaces without introducing unintended side effects.
Context and Compatibility
The availability of certain features, such as intrinsic functions, depends on the COBOL compiler and the specific version of the COBOL standard being used. Code written for a newer COBOL standard may not be directly compatible with older compilers. Consider the target environment when selecting a technique.

When working with data from external sources, understand the data format and encoding. Inconsistencies in data encoding can lead to unexpected results. Implement data validation routines to ensure data conforms to expected standards.
Conclusion
Removing trailing spaces in COBOL requires careful selection of the appropriate technique. The INSPECT statement, reference modification, and intrinsic functions (when available) offer different approaches. Consider performance, compatibility, and error handling when implementing these techniques.
Key Takeaways:
- Understand the data format and the context of trailing spaces.
- Use
INSPECTfor simple replacements; reference modification for substring manipulation.- Utilize intrinsic functions (e.g.,
FUNCTION TRIM) when COBOL 2002 or later is supported.- Test thoroughly with realistic data volumes to assess performance.
- Implement error handling and data validation to ensure data integrity.
