Easytrieve Trimming

Trimming removes unwanted spaces—usually trailing—from character fields before you compare, concatenate, or export delimited data. COBOL developers reach for FUNCTION TRIM; SQL has RTRIM and LTRIM. Easytrieve offers fixed-length A fields and expects you to implement trim logic with INDEX loops, overlays, and careful MOVE lengths. That sounds tedious, but payroll and customer extract programs have used the same patterns for decades. This page teaches why trailing spaces appear, how to detect them with DISPLAY HEX, reverse-scan algorithms, left-trim for imported data, and when trimming hurts if downstream systems expect fixed padding.

Progress0 of 0 lessons

Why Trailing Spaces Exist

Fixed-length records assign every byte. A thirty-byte name field may hold SMITH plus twenty-five spaces. COBOL MOVE of shorter literal space-fills the target. Easytrieve FILE A fields read those bytes faithfully. Comparisons against unpadded literals fail. CSV exports include ugly trailing spaces before delimiters unless you trim. Understanding the source prevents blaming Easytrieve for data shaped upstream.

No Built-In TRIM

Broadcom documentation does not define TRIM, RTRIM, or LTRIM functions for standard batch Easytrieve. Forum archives confirm developers implement custom logic. Plan a reusable PROC or paragraph TRIM-RIGHT that accepts source field, length, and target work field—copy across programs in your shop macro library for consistency.

Reverse Scan Algorithm

Define single-byte OCCURS field over the source with INDEX. Set index to last byte position. DO WHILE current byte is space and index greater than zero, decrement index. When non-space found, MOVE from start through index length to trimmed work field—or record trimmed length in a separate numeric counter for delimited output. EBCDIC space is X'40'—compare to literal space or use known hex in DISPLAY debug.

text
1
2
3
4
5
6
7
DEFINE WS-RAW W 30 A DEFINE WS-TRIM W 30 A DEFINE WS-LEN W 3 N 0 * Conceptual: loop from byte 30 down to 1 * while byte = space, decrement position * WS-LEN = last non-space position * MOVE overlay WS-RAW length WS-LEN to WS-TRIM

INDEX Formula Reminder

INDEX value equals (occurrence number minus one) times element length. For one-byte OCCURS, index equals byte offset. For multi-byte elements, stepping index by one jumps entire element width—use one-byte scan for trim logic. Setting index to field-length minus one targets last byte on zero-based occurrence math per Broadcom manual examples.

Trim strategies
GoalApproach
Remove trailing spacesReverse INDEX scan, MOVE slice
Compare to short literalTrim first or use equal-length overlay
CSV exportTrim then append delimiter literal
Debug invisible spacesDISPLAY HEX field
Remove leading spacesForward scan until non-space

Left Trim (Leading Spaces)

Imported ASCII files or poorly formatted input may pad left. Scan forward from byte one until non-space byte found. MOVE from that offset for remaining length into work field. Combine left and right trim for full normalization when building free-text search keys. Watch empty-all-space fields—trim result length zero; guard IF before MOVE to avoid negative length logic errors in hand-coded counters.

Trim vs Full-Length Compare

Alternative to trim: define overlay matching literal length for compare only. IF STATUS-1 EQ 'Y' where STATUS-1 overlays first byte of five-byte STATUS avoids trimming entire field. Choose overlay when only first character matters; choose full trim when exporting human-readable text to spreadsheets or emails.

Trimming and VARYING Fields

VARYING A fields store explicit length prefix—data portion may not include trailing spaces if SQL loader set length correctly. Still verify :DATA bytes for spaces when length counts trailing spaces as significant. FLDA:LENGTH trim may mean adjusting binary length after removing spaces from :DATA before INSERT—advanced pattern for DB2 interface programs.

When Not to Trim

  • Downstream COBOL expects fixed thirty-byte names—preserve spaces on WRITE.
  • Sort keys rely on fixed width collating—trim changes order.
  • Mainframe audit compares raw record hex—trim hides data issues.
  • Password or code fields where space is valid trailing character—rare but possible.

DISPLAY HEX for Diagnosis

When IF results mystify, DISPLAY HEX shows each byte. Trailing X'40' bytes confirm space padding. Low-values X'00' indicate different problem— uninitialized data not trim case. Teach support staff to read hex snippet in compile listing SYSPRINT output during incident calls.

Performance on Large Files

Per-record trim loop adds CPU on million-row extracts. Trim once when reading if reused many times in same record processing. Avoid trim inside innermost nested IF on every field when only one field exports. Batch trim PROC inlined by compiler still executes per row— profile before production peak windows.

Common Trimming Mistakes

  1. INDEX on wrong element length—skips bytes during scan.
  2. Trimming then comparing to space-padded file without updating both sides.
  3. Writing trimmed names back when partner expects fixed layout.
  4. Forgetting all-space field yields length zero edge case.
  5. Assuming DISPLAY trimmed visually—spaces may remain in storage.

Explain It Like I'm Five

Trimming is erasing empty space stickers at the end of a word on your label. The box still has thirty slots but only five letters matter. You walk from the end backward until you find a real letter, then copy only those letters to a clean label. Easytrieve does not give you an eraser tool—you count boxes with your finger in a loop.

Exercises

  1. Describe reverse scan steps for 20-byte A field in plain language.
  2. Explain when overlay compare beats full trim.
  3. State EBCDIC hex for space byte.
  4. List two cases when you should not trim before WRITE.
  5. Outline PROC parameters for reusable TRIM-RIGHT routine.

Quiz

Test Your Knowledge

1. Easytrieve provides a built-in TRIM function:

  • No—use loops or overlays
  • Yes—TRIM(field)
  • Only in REPORT
  • Only on z/OS 6.4

2. Trailing spaces in file name fields often come from:

  • Fixed-length COBOL PIC X records
  • Packed decimal
  • Binary counters
  • JCL CLASS

3. To trim, you typically scan:

  • From end backward until non-space found
  • Always column 1 only
  • SYSPRINT only
  • Without loops

4. After trimming for compare, store result in:

  • Separate work field or overlay slice
  • JCL only
  • SYSPRINT
  • Cannot store

5. Trimming changes file data on disk when:

  • You WRITE the trimmed record back
  • You DISPLAY only
  • You compile
  • Never
Published
Read time12 min
AuthorMainframeMaster
Reviewed by MainframeMaster teamVerified: Easytrieve INDEX scan trim patterns and assignment comparison rulesSources: Broadcom Easytrieve Report Generator 11.6 TechDocs, Assignment Statement, Define Files and Fields, community trim patternsApplies to: Easytrieve trailing and leading space removal