RENAMES and level 66
A level 66 adds no field of its own. It gives a name to a range of fields that already exist, so two neighbouring items can be handed to a routine as one, or a group of five can be printed as one string. The keyword is RENAMES, and the entry has no picture, no value and no bytes.
A name for a range
The customer below has a first name and a last name as two fields, which is what a form wants. A report wants the full name as one string of ten characters, and the 66 gives it that without moving anything:
IDENTIFICATION DIVISION.
PROGRAM-ID. RENAMES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CUSTOMER.
05 FIRST-NAME PIC X(5).
05 LAST-NAME PIC X(5).
05 CITY-F PIC X(6).
66 FULL-NAME RENAMES FIRST-NAME THRU LAST-NAME.
PROCEDURE DIVISION.
MOVE "ADA " TO FIRST-NAME.
MOVE "LOVEL" TO LAST-NAME.
MOVE "LONDON" TO CITY-F.
DISPLAY "full [" FULL-NAME "]".
DISPLAY "len " LENGTH OF FULL-NAME.
DISPLAY "city [" CITY-F "]".
STOP RUN.
cobc -x -free -Wall -o renames renames.cob
./renames
full [ADA LOVEL]
len 10
city [LONDON]
The ten characters are the two names and the three spaces that "ADA " put in the first one; a rename does not join strings, it shows the bytes. CITY-F is outside the range and is not part of the ten, which is what THRU means: from the first item to the last one, in declaration order.
A 66 is read and written
The new name is not read-only. It is an alphanumeric view of the bytes, so a MOVE into it lands on the fields underneath, and the fields underneath land in it:
IDENTIFICATION DIVISION.
PROGRAM-ID. WRITE66.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CUSTOMER.
05 FIRST-NAME PIC X(5).
05 LAST-NAME PIC X(5).
05 CITY-F PIC X(6).
66 FULL-NAME RENAMES FIRST-NAME THRU LAST-NAME.
PROCEDURE DIVISION.
MOVE "GRACEBYNTE" TO FULL-NAME.
DISPLAY "first [" FIRST-NAME "]".
DISPLAY "last [" LAST-NAME "]".
DISPLAY "city [" CITY-F "]".
STOP RUN.
cobc -x -free -Wall -o write66 write66.cob
./write66
first [GRACE]
last [BYNTE]
city [ ]
Ten characters went in through one name and came out as two names of five each, and the city stayed empty. That is the same aliasing REDEFINES has, with one difference: the range is made of fields that already have names, so the program did not have to describe the bytes a second time.
A range inside a group
THRU follows declaration order, so a range can cross the children of a nested group. The employee record below has the identifier before the name and the department after it, and the two renames pick the name and the department without either neighbour:
IDENTIFICATION DIVISION.
PROGRAM-ID. NESTED.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 EMP.
05 EMP-ID PIC 9(4) VALUE 7.
05 NAME-GRP.
10 FIRST-NAME PIC X(5) VALUE "ADA ".
10 LAST-NAME PIC X(5) VALUE "LOVEL".
05 DEPT-F PIC X(3) VALUE "RND".
66 NAME-ALL RENAMES FIRST-NAME THRU LAST-NAME.
66 DEPT-ONLY RENAMES DEPT-F.
PROCEDURE DIVISION.
DISPLAY "id [" EMP-ID "]".
DISPLAY "name [" NAME-ALL "] len " LENGTH OF NAME-ALL.
DISPLAY "dept [" DEPT-ONLY "] len " LENGTH OF DEPT-ONLY.
MOVE "ZZZ" TO DEPT-ONLY.
DISPLAY "after [" DEPT-F "]".
STOP RUN.
cobc -x -free -Wall -o nested nested.cob
./nested
id [0007]
name [ADA LOVEL] len 10
dept [RND] len 3
after [ZZZ]
DEPT-ONLY is a rename with no THRU, which is the single-item form: the range starts and ends at the same field, and the new name is three characters wide. Writing through it changed DEPT-F the same way the range did.
What level 66 may not have
The entry is small because the compiler needs nothing from it. A picture would describe bytes that the range already describes, and it is refused:
IDENTIFICATION DIVISION.
PROGRAM-ID. RULEPIC.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CUSTOMER.
05 FIRST-NAME PIC X(5).
05 LAST-NAME PIC X(5).
66 FULL-NAME RENAMES FIRST-NAME THRU LAST-NAME PIC X(10).
PROCEDURE DIVISION.
DISPLAY FIRST-NAME.
STOP RUN.
cobc -x -free -Wall -o rulepic rulepic.cob
rulepic.cob:9: error: syntax error, unexpected PICTURE
7 | 05 FIRST-NAME PIC X(5).
8 | 05 LAST-NAME PIC X(5).
9 > 66 FULL-NAME RENAMES FIRST-NAME THRU LAST-NAME PIC X(10).
10 |
11 | PROCEDURE DIVISION.
A value is refused for the same reason: the bytes belong to the fields the range names, and their clauses are the ones that initialize them.
IDENTIFICATION DIVISION.
PROGRAM-ID. RULEVAL66.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CUSTOMER.
05 FIRST-NAME PIC X(5).
05 LAST-NAME PIC X(5).
66 FULL-NAME RENAMES FIRST-NAME THRU LAST-NAME VALUE "AB".
PROCEDURE DIVISION.
DISPLAY FIRST-NAME.
STOP RUN.
cobc -x -free -Wall -o ruleval66 ruleval66.cob
ruleval66.cob:9: error: syntax error, unexpected VALUE
7 | 05 FIRST-NAME PIC X(5).
8 | 05 LAST-NAME PIC X(5).
9 > 66 FULL-NAME RENAMES FIRST-NAME THRU LAST-NAME VALUE "AB".
10 |
11 | PROCEDURE DIVISION.
The order rule is the third. A 66 has to come after the items it names, because a range needs two ends that the compiler has already read:
IDENTIFICATION DIVISION.
PROGRAM-ID. RULEORDER.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CUSTOMER.
66 FULL-NAME RENAMES FIRST-NAME THRU LAST-NAME.
05 FIRST-NAME PIC X(5).
05 LAST-NAME PIC X(5).
PROCEDURE DIVISION.
DISPLAY FIRST-NAME.
STOP RUN.
cobc -x -free -Wall -o ruleorder ruleorder.cob
ruleorder.cob:7: error: 'FIRST-NAME' is not defined
5 | WORKING-STORAGE SECTION.
6 | 01 CUSTOMER.
7 > 66 FULL-NAME RENAMES FIRST-NAME THRU LAST-NAME.
8 | 05 FIRST-NAME PIC X(5).
9 | 05 LAST-NAME PIC X(5).
ruleorder.cob:8: error: no previous data item of level 05
6 | 01 CUSTOMER.
7 > 66 FULL-NAME RENAMES FIRST-NAME THRU LAST-NAME.
8 | 05 FIRST-NAME PIC X(5).
9 | 05 LAST-NAME PIC X(5).
10 |
ruleorder.cob:6: error: PICTURE clause required for 'CUSTOMER'
4 | DATA DIVISION.
5 | WORKING-STORAGE SECTION.
6 > 01 CUSTOMER.
7 | 66 FULL-NAME RENAMES FIRST-NAME THRU LAST-NAME.
8 | 05 FIRST-NAME PIC X(5).
9 | 05 LAST-NAME PIC X(5).
The first message is the useful one: the compiler had not yet read FIRST-NAME, so there is nothing to rename. The other two follow from the first, which is the usual shape of a COBOL error list.
RENAMES against REDEFINES
The two clauses look alike and are not. A REDEFINES entry describes the same storage instead of the original: it has its own picture, so it can say that four bytes are a number and not four characters, and it may be shorter than the original. A RENAMES entry names a range of fields that keep their own descriptions; it has no picture, takes no storage and its width is the sum of what it names.
Which one to use follows from that. Reading a record as a header or as a detail is REDEFINES, because the two views describe the bytes differently. Handing two neighbouring fields to a routine, or printing them as one, is RENAMES, because the bytes are already described and only need a second name.
Level 66 is a rare clause, and some coding standards forbid it outright on the grounds that a group with a proper name is clearer. The compiler supports it in both directions, as the runs above show, so the choice is the standard of the shop rather than the language.
Exercises
- Rename LAST-NAME through CITY-F as well and print LENGTH OF the new name. Does it include the city?
- Put a 66 in the middle of the record, after FIRST-NAME and naming it alone, and build. What does the compiler say about the entries that follow?
- Move a number into the 66 of the first program with MOVE 42 TO FULL-NAME and print the two names. How wide is the number, and where did it land?
- Write the same program twice, once with a REDEFINES of the two names and once with a 66, and list the clauses each one needs.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a level 66 entry add to a record?
Answer
A name for a range of fields that already exist
The 66 takes no storage: it names bytes that other entries already describe, from the first item of the range to the last.
-
Which clause is not allowed on a level 66?
Answer
PICTURE
The range already has descriptions, so a picture would contradict them; the compiler refuses it with a syntax error.
-
Where must a 66 be declared?
Answer
After the items it renames, in the same record
Both ends of the range have to be read already, so the entry comes after the last of them and the compiler reports an undefined name otherwise.
-
How does RENAMES differ from REDEFINES?
Answer
A 66 names existing fields and has no picture; a REDEFINES entry describes the same bytes with a picture of its own
The rename adds a name to bytes that keep their own descriptions; the redefinition adds a second description with its own picture.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 CUSTOMER. 05 FIRST-NAME PIC X(5). 05 LAST-NAME PIC X(5). 05 CITY-F PIC X(6). 66 FULL-NAME RENAMES FIRST-NAME THRU LAST-NAME. PROCEDURE DIVISION. MOVE "ADA " TO FIRST-NAME. MOVE "LOVEL" TO LAST-NAME. MOVE "LONDON" TO CITY-F. DISPLAY "[" FULL-NAME "]". STOP RUN.Answer
[ADA LOVEL]
The range is the two five-byte names, and the three spaces inside the first one are part of the ten characters.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 01 CUSTOMER. 05 FIRST-NAME PIC X(5). 05 LAST-NAME PIC X(5). 05 CITY-F PIC X(6). 66 FULL-NAME RENAMES FIRST-NAME THRU LAST-NAME. PROCEDURE DIVISION. MOVE "GRACEBYNTE" TO FULL-NAME. DISPLAY "[" FIRST-NAME "][" LAST-NAME "][" CITY-F "]". STOP RUN.Answer
[GRACE][BYNTE][ ]
Ten characters fill the ten bytes of the two names and stop there, so the city stays as it was.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 01 EMP. 05 EMP-ID PIC 9(4) VALUE 7. 05 NAME-GRP. 10 FIRST-NAME PIC X(5) VALUE "ADA ". 10 LAST-NAME PIC X(5) VALUE "LOVEL". 05 DEPT-F PIC X(3) VALUE "RND". 66 NAME-ALL RENAMES FIRST-NAME THRU LAST-NAME. PROCEDURE DIVISION. DISPLAY "[" NAME-ALL "]". DISPLAY LENGTH OF NAME-ALL. STOP RUN.Answer
[ADA LOVEL] and 10
The range starts at the first name and ends at the last name, inside the group, so the identifier and the department are outside it.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q4. DATA DIVISION. WORKING-STORAGE SECTION. 01 EMP. 05 DEPT-F PIC X(3) VALUE "RND". 66 DEPT-ONLY RENAMES DEPT-F. PROCEDURE DIVISION. MOVE "ZZZ" TO DEPT-ONLY. DISPLAY "[" DEPT-F "]". STOP RUN.Answer
[ZZZ]
The 66 names the same three bytes, so a write through it is a write through DEPT-F.
-
What is the first line of the compiler output for this program?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 CUSTOMER. 05 FIRST-NAME PIC X(5). 05 LAST-NAME PIC X(5). 66 FULL-NAME RENAMES FIRST-NAME THRU LAST-NAME PIC X(10). PROCEDURE DIVISION. DISPLAY FIRST-NAME. STOP RUN.Answer
error: syntax error, unexpected PICTURE
The parser meets a picture where the entry should end, and the token it reports is the full keyword PICTURE.
-
What is the first line of the compiler output for this program?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 CUSTOMER. 05 FIRST-NAME PIC X(5). 05 LAST-NAME PIC X(5). 66 FULL-NAME RENAMES FIRST-NAME THRU LAST-NAME VALUE "AB". PROCEDURE DIVISION. DISPLAY FIRST-NAME. STOP RUN.Answer
error: syntax error, unexpected VALUE
The range has no storage of its own, so a value clause has nothing to initialize and the parser stops at the keyword.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev