
Larry C. answered 07/23/19
Computer Science and Mathematics professional
Frankly, you can't, at least not with a single statement. Since COBOL automatically pads the rightmost characters in an alphanumeric field, an INSPECT might left-justify and pad the characters with a statement like this:
INSPECT some-field REPLACING ALL SPACES BY ''. (assumes by 'whitespace' you only mean spaces. If not, you would need additional REPLACING clauses for other characters to bypass.)
However, a way guaranteed to work is to copy non-whitespace characters into another field.
77 whitespace-test PIC X.
88 is-whitespace VALUES ' ', ... (whatever additional characters you want to remove)
77 char-count PIC S9(4) COMP.
77 sub1 PIC S9(4) COMP.
01 work-fields.
05 source-field PIC X(something).
10 source-field-char REDEFINES source-field PIC X OCCURS something TIMES.
05 target-field PIC X(something) VALUE SPACES.
10 target-field-char REDEFINES target-field PIC X OCCURS something TIMES.
MOVE ZERO TO char-count.
PERFORM VARYING sub1 FROM 1 BY 1 UNTIL sub1 EQUAL something
MOVE source-field-char(sub1) TO whitespace-test
IF NOT is-whitespace
ADD 1 TO char-count
MOVE whitespace-test TO target-field-char(char-count)
END-IF
END-PERFORM.