Class and sign conditions
Input data can't be trusted. A field that should hold an account number may contain spaces, letters or binary garbage from a bad upstream file, and doing arithmetic on it can abend the job. Class conditions let you test what kind of characters a field holds before you use it.
IDENTIFICATION DIVISION.
PROGRAM-ID. CLASSES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-ID PIC X(6).
01 WS-NAME PIC X(8).
01 WS-AMT PIC S9(5)V99 VALUE -0.01.
PROCEDURE DIVISION.
MOVE "004512" TO WS-ID
IF WS-ID IS NUMERIC DISPLAY "004512 IS NUMERIC" END-IF
MOVE "4512" TO WS-ID
IF WS-ID IS NOT NUMERIC
DISPLAY "4512 + 2 SPACES IS NOT NUMERIC"
END-IF
MOVE "VAN DYKE" TO WS-NAME
IF WS-NAME IS ALPHABETIC
DISPLAY "VAN DYKE IS ALPHABETIC"
END-IF
MOVE "O'NEIL" TO WS-NAME
IF WS-NAME IS NOT ALPHABETIC
DISPLAY "O'NEIL IS NOT ALPHABETIC"
END-IF
MOVE SPACES TO WS-NAME
IF WS-NAME IS ALPHABETIC
DISPLAY "ALL SPACES IS ALPHABETIC"
END-IF
IF WS-AMT IS NEGATIVE DISPLAY "-0.01 IS NEGATIVE" END-IF
STOP RUN.
Output:
004512 IS NUMERIC
4512 + 2 SPACES IS NOT NUMERIC
VAN DYKE IS ALPHABETIC
O'NEIL IS NOT ALPHABETIC
ALL SPACES IS ALPHABETIC
-0.01 IS NEGATIVE
Class conditions
| Test | True when every character is… |
|---|---|
NUMERIC |
a digit 0–9 (for a signed numeric field, a valid sign is allowed too) |
ALPHABETIC |
a letter A–Z, a–z, or a space |
ALPHABETIC-UPPER |
A–Z or space |
ALPHABETIC-LOWER |
a–z or space |
Each can be negated with NOT. Watch out for the edge cases:
- Spaces are not numeric.
"4512 "failsNUMERIC. Short input typed into aPIC X(6)is padded with spaces, so it fails too. - Spaces are alphabetic. A completely blank name passes
ALPHABETIC. If blank isn't acceptable, test= SPACESseparately. - A minus sign or decimal point in a
PIC Xfield makes it non-numeric.NUMERICchecks characters, not "does this look like a number".
Sign conditions
For numeric fields you can ask about the sign directly:
IF WS-AMT IS POSITIVE ...
IF WS-AMT IS NEGATIVE ...
IF WS-AMT IS ZERO ...
IF WS-AMT IS NOT POSITIVE ...
POSITIVE means greater than zero — zero is neither positive nor
negative. NOT POSITIVE is therefore "zero or negative". These read better
than > 0 / < 0 and are common in financial code (credits versus
debits).
On the job
IF WS-AMOUNT NOT NUMERIC before arithmetic is the standard guard
against the dreaded S0C7 data exception — the abend you get on z/OS
when packed or zoned arithmetic meets invalid data. Validation programs
that check every field of every input record with class tests are a
normal part of batch systems.
Your task
A customer-maintenance job validates input from a data-entry screen. Read three lines:
- an account ID into
WS-ACCT-ID(PIC X(6)) - a surname into
WS-SURNAME(PIC X(12)) - an adjustment amount into
WS-AMOUNT(PIC S9(7)V99)
Display three result lines:
ACCOUNT ID: VALIDif the ID is all digits, otherwiseACCOUNT ID: INVALIDSURNAME: MISSINGif the surname is blank; otherwiseSURNAME: VALIDif it's alphabetic, elseSURNAME: INVALIDAMOUNT: CREDITif positive,AMOUNT: DEBITif negative,AMOUNT: ZEROif zero
Use class conditions (NUMERIC, ALPHABETIC) and sign conditions
(POSITIVE, NEGATIVE), not > 0.