Group Items
Part of the Data and Numbers section of Coddy's COBOL Programming journey. Lesson 2 of 23.
A group's name stands for all the bytes under it, in order. DISPLAY of a group prints its items side by side, with nothing added between them:
01 FULL-NAME.
05 FIRST-N PIC X(6) VALUE "Grace".
05 LAST-N PIC X(6) VALUE "Hopper".
DISPLAY FULL-NAME. *> Grace HopperA group's size is the total of its items: FULL-NAME is 12 characters.
A group is always treated as text, like PIC X, even when all its items are numbers. A MOVE or ACCEPT into a group fills its bytes from the left, and each item gets its share:
01 TODAY.
05 YYYY PIC 9(4).
05 MM PIC 99.
05 DD PIC 99.
MOVE "20261123" TO TODAY.
DISPLAY DD "/" MM "/" YYYY. *> 23/11/2026This is how COBOL splits fixed-width data: the layout of the record says which characters belong to which item.
Challenge
EasyA date arrives as 8 digits, year first: 20261123. Read it into the group DATE-IN with one ACCEPT, then display it day first: 23/11/2026.
Example input, one line per ACCEPT: 20261123.
Try it yourself
IDENTIFICATION DIVISION.
PROGRAM-ID. DATES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 DATE-IN.
05 IN-YEAR PIC 9(4).
05 IN-MONTH PIC 99.
05 IN-DAY PIC 99.
PROCEDURE DIVISION.
*> Read DATE-IN, then display the date
STOP RUN.