ILE COBOL – Directives
Compiler Directives
A compiler directive is a statement that causes the compiler to take a specific action during compilation. In ILE COBOL, the terms “compiler directives” and “compiler-directing statements” are often used interchangeably, and in many contexts, they refer to the same set of constructs that provide instructions to the compiler during the compilation process.
Types of compiler directives
ILE COBOL compiler directives can be broadly categorised into 5 types:
Type I – Listing Control Statements
- EJECT Statement
- SKIP1/2/3 Statement
- TITLE Statement
- *CONTROL (*CBL) Statement
Type II – Source Text Manipulation Statements
- REPLACE Statement
-
COPY Statement
- COPY Basic Format
- COPY DDS Format
- COPY IFS Format
Type III – Compilation Specification Statements
- PROCESS Statement
Type IV – Conditional Compiler Statements
- DEFINE Statement
- IF Statement (ELSE, END-IF)
- EVALUATE Statement (WHEN, WHEN OTHER, END-EVALUATE Statement)
Type V – Exception Handling & Debugging Statements
- USE Statement
Compilation output
The ILE COBOL compilation output report, also known as the compiler listing, is a spool file (often named as program name(*PGMID) in QUSRSYS IBMi library) that contains extensive information about the source program, the compilation options, and the generated object code. The source type CBL & CBLLE are compiled via CRTCBLPGM & CRTBNDCBL respectively and generates compilation output report. It consists of multiple sections which are user control:
1. Compilation command and options section
Command Options:
Program . . . . . . . . . . . . . . : DSPPGM01@
Library . . . . . . . . . . . . . : PROGRAMMER
Source file . . . . . . . . . . . . : ALL
Library . . . . . . . . . . . . . : PROGRAMMER
Source member . . . . . . . . . . . : DSPPGM01@
Output . . . . . . . . . . . . . . . : *PRINT
Generation severity level . . . . . : 30
Text 'description' . . . . . . . . . : *SRCMBRTXT
Compiler options . . . . . . . . . . : *XREF
Conversion options . . . . . . . . . : *NONE
Message limit:
Number of messages . . . . . . . . : *NOMAX
Message limit severity . . . . . . : 30
Debug view option . . . . . . . . . :
Debug view . . . . . . . . . . . . : *STMT
Compress listing view . . . . . . : *NOCOMPRESSDBG
Debug encryption key . . . . . . . : *NONE
Optimize level . . . . . . . . . . . : *NONE
2. Source code section
S o u r c e
STMT PL SEQNBR -A 1 B..+....2....+....3....+....4
1 000100 IDENTIFICATION DIVISION.
000200* ----------------------*
2 000300 PROGRAM-ID. DSPPGM01.
3 000400 AUTHOR. PROGRAMMER.
000500
000600* -------------------*
4 000700 ENVIRONMENT DIVISION.
000800* -------------------*
5 000900 CONFIGURATION SECTION.
6 001000 SOURCE-COMPUTER. IBM-ISERIES.
7 001100 OBJECT-COMPUTER. IBM-ISERIES.
001200
001300* -------------------*
8 001400 INPUT-OUTPUT SECTION.
001500* -------------------*
9 001600 FILE-CONTROL.
10 001700 SELECT DSP01
– SEQNBR (Column1 to 6) are called user-supplied sequence number.
– STMT numbers are source-file sequence number.
– Source code section also contains ILE COBOL Compiler options in effect, when PROCESS OPTIONS used in ILE COBOL program.
000100 PROCESS OPTIONS.
COBOL Compiler Options in Effect
SOURCE
NOXREF
GEN
NOSEQUENCE
NOVBSUM
NONUMBER
NOMAP
3. Verb usage by count section
V e r b U s a g e B y C o u n t VERB COUNT CALL 1 DISPLAY 3 GOBACK 2
4. Data division map section
D a t a D i v i s i o n M a p STMT LVL SOURCE NAME SECTION DISP LENGTH TYPE 24 FD DSP01 FS 25 01 DSP01-REC FS 00000000 1920 AN 26 FD DB01 FS 27 01 DB01-REC FS 00000000 91 GROUP
5. Cross reference section
C r o s s R e f e r e n c e
Data References:
Data-type is indicated by the letter following a data-name definition.
These letters and their meanings are:
E = EXTERNAL
G = GLOBAL
X = EXTERNAL and GLOBAL
DATA NAMES DEFINED REFERENCES (* = CHANGED)
ADD-OFF 121 258*
ADD-ON 120 257
ADD-SCREEN 187 274 283 294* 449
APPLY-ADDFMT-OFF 172
APPLY-ADDFMT-ON 171
APPLY-DLTFMT-OFF 178 413* 421* 421 430* 439*
– Cross reference section contains Data References & Procedure References.
6. Error message section
M e s s a g e s
STMT
* 2 MSGID: LNC0810 SEVERITY: 10 SEQNBR: 000300
Message . . . . : Module name 'DSPPGM01@' does not match outermost
PROGRAM-ID name 'DSPPGM01'. Accepted.
* 37 MSGID: LNC0929 SEVERITY: 0 SEQNBR: 004700
Message . . . . : The COPY statement used format 2 (DDS translate).
* 46 MSGID: LNC0929 SEVERITY: 0 SEQNBR: 004900
Message . . . . : The COPY statement used format 2 (DDS translate).
Message Summary
Message totals:
Information (00-04) . . . . . . . . : 0
Warning (05-19) . . . . . . . . : 2
Error (20-29) . . . . . . . . : 0
Severe (30-39) . . . . . . . . : 0
Terminal (40-99) . . . . . . . . : 0
--------------------------------------------
Total 2
– Error messages contain source-file sequence number, message-id, severity, user-supplied sequence number.
– Error message section has first level text message & second level text message containing Severity level number – 0, 10, 20 & 30.
– It also contains error summary section.
– e.g. of second level text message.
STMT
* 2 MSGID: LNC0810 SEVERITY: 10 SEQNBR: 000500
Message . . . . : Module name 'COBOL23A' does not match outermost
PROGRAM-ID name 'COBOL22A'. Accepted.
Cause . . . . . : The module object or program object name specified
on the CRTCBLMOD or CRTBNDCBL CL commands does not match the program
name specified in the outermost COBOL program's PROGRAM-ID paragraph.
This difference may lead to binding errors.
Recovery . . . : Make sure the PROGRAM-ID name of the outermost
COBOL program matches the module object or program object name.
7.Statistics section
Statistics: Source records read . . . . . . . . : 1199 Copy records read . . . . . . . . . : 92 Copy members processed . . . . . . : 11 Sequence errors . . . . . . . . . . : 0 Highest severity message issued . . : 30
Listing Control Statements
Listing control statements allow programmers to display or suppress the listing of source code in the compilation report (the spool file generated after compilation). These compiler directives have no impact on the program. Below are the compilation report modification statements:
- EJECT Statement
- SKIP1/2/3 Statement
- TITLE Statement
- *CONTROL (*CBL) Statement
EJECT Statement
The EJECT statement has no effect on the compilation of the source program itself. The ‘EJECT’ statement is applied in ILE COBOL source in AREA A or AREA B. When the source is compiled, it generates a compilation report that includes compilation details, source, warning and error details. The ‘EJECT’ statement effects in ejecting from current page to new page in the compilation report at the point where it is used.
Example: EJECT.
WORKING-STORAGE SECTION.
01 WS-LENGTH PIC S9(02) VALUE 0.
01 WS-STRING PIC X(12) VALUE ' SPIDERMAN '.
EJECT.
PROCEDURE DIVISION.
MAIN-LOGIC.
* Calculate Length of String without leading & trailing spaces
COMPUTE WS-LENGTH = FUNCTION LENGTH(
FUNCTION TRIM(WS-STRING))
Compilation Report Screenshot:
9 001600 WORKING-STORAGE SECTION.
10 001700 01 WS-LENGTH PIC S9(02) VALUE 0.
11 001800 01 WS-STRING PIC X(12) VALUE ' SPIDERMAN '.
5770WDS V7R3M0 160422 LN IBM ILE COBOL TESTLAB/STRFUNC02 S06F561P
STMT PL SEQNBR -A 1 B..+....2....+....3....+....4....+....5....+....6....+....7..IDENTFCN
12 002000 PROCEDURE DIVISION.
002100 MAIN-LOGIC.
002200* Calculate Length of String without leading & trailing spaces
13 002300 COMPUTE WS-LENGTH = FUNCTION LENGTH(
002400 FUNCTION TRIM(WS-STRING))
SKIP1/2/3 Statement
The SKIP1, SKIP2, or SKIP3 statements has no effect on the compilation of the source program itself. This statement is applied in ILE COBOL source in AREA A or AREA B. SKIP1, SKIP2, or SKIP3 effects skipping of 1, 2 or 3 lines in the compilation report. It is not visible in the compilation report but when compilation report is printed in paper then it skips required number of lines at the same place where it is used.
Example: SKIP3.
WORKING-STORAGE SECTION.
01 WS-LENGTH PIC S9(02) VALUE 0.
01 WS-STRING PIC X(12) VALUE ' SPIDERMAN '.
SKIP3.
PROCEDURE DIVISION.
MAIN-LOGIC.
* Calculate Length of String without leading & trailing spaces
COMPUTE WS-LENGTH = FUNCTION LENGTH(
FUNCTION TRIM(WS-STRING))
Compilation Report Screenshot:
001500*---------------------*
9 001600 WORKING-STORAGE SECTION.
10 001700 01 WS-LENGTH PIC S9(02) VALUE 0.
11 001800 01 WS-STRING PIC X(12) VALUE ' SPIDERMAN '.
12 002000 PROCEDURE DIVISION.
002100 MAIN-LOGIC.
002200* Calculate Length of String without leading & trailing spaces
13 002300 COMPUTE WS-LENGTH = FUNCTION LENGTH(
002400 FUNCTION TRIM(WS-STRING))
TITLE Statement
The TITLE statement has no effect on the compilation of the source program itself. The ‘TITLE’ statement is applied in ILE COBOL source in AREA A or AREA B. When the source is compiled, it generates a compilation report that includes compilation details, source, warning and error details. The ‘TITLE’ statement effects ejecting the current page reporting and creating a new page in the compilation report at the point where it is used and also title text is displayed.
Example: TITLE.
WORKING-STORAGE SECTION.
01 WS-LENGTH PIC S9(02) VALUE 0.
01 WS-STRING PIC X(12) VALUE ' SPIDERMAN '.
TITLE 'START'.
PROCEDURE DIVISION.
MAIN-LOGIC.
* Calculate Length of String without leading & trailing spaces
COMPUTE WS-LENGTH = FUNCTION LENGTH(
FUNCTION TRIM(WS-STRING))
Compilation Report Screenshot:
001500*---------------------*
9 001600 WORKING-STORAGE SECTION.
10 001700 01 WS-LENGTH PIC S9(02) VALUE 0.
11 001800 01 WS-STRING PIC X(12) VALUE ' SPIDERMAN '.
5770WDS V7R3M0 160422 LN IBM ILE COBOL TESTLAB/STRFUNC02
START
STMT PL SEQNBR -A 1 B..+....2....+....3....+....4....+....5....+....6....+....7..
12 002100 PROCEDURE DIVISION.
002200 MAIN-LOGIC.
002300* Calculate Length of String without leading & trailing spaces
13 002400 COMPUTE WS-LENGTH = FUNCTION LENGTH(
002500 FUNCTION TRIM(WS-STRING))
*CONTROL or *CBL Statement
The *CONTROL and *CBL statements are synonymous. Programmers can use this compiler directive to select display or suppress the listing of source code throughout the source program. Each parameter with *CBL statement brings its own effect, if used in the ILE COBOL source program. The *CONTROL or *CBL statement have following parameters:
- SOURCE, NOSOURCE
- LIST, NOLIST
- MAP, NOMAP
*CBL SOURCE/NOSOURCE
These *CBL SOURCE tells compiler to display the source listing and *CBL NOSOURCE tells to hid the source listing. By default, SOURCE is applied. It is some way similar to PROCESS OUTPUT. Actual use of both SOURCE & NOSOURCE is to reduce the length of compilation report which helps in debugging.
Example: Combined use of *CONTROL NOSOURCE & *CONTROL SOURCE.
FILE SECTION.
FD ACCOUNT-FILE LABEL RECORDS ARE STANDARD.
*CBL NOSOURCE.
01 ACCOUNT-REC.
COPY DD-ACCTPFR OF ACCTPF.
WORKING-STORAGE SECTION.
*CBL SOURCE.
01 WS-PSDS.
05 FILLER PIC X(46).
05 WS-EXCP-ID PIC X(07).
05 FILLER PIC X(41).
01 ACCOUNT-FILE-STATUS PIC X(02).
01 ACCOUNT-FILE-STATU1 LIKE ACCOUNT-FILE-STATUS.
Compilation Report Screenshot:
19 002400 FILE SECTION.
20 002500 FD ACCOUNT-FILE LABEL RECORDS ARE STANDARD.
003200 01 WS-PSDS.
28 003300 05 FILLER PIC X(46).
29 003400 05 WS-EXCP-ID PIC X(07).
30 003500 05 FILLER PIC X(41).
31 003600 01 ACCOUNT-FILE-STATUS PIC X(02).
32 003700 01 ACCOUNT-FILE-STATU1 LIKE ACCOUNT-FILE-STATUS.
* LIKE: ACCOUNT-FILE-STATU1 has inherited PICTURE X(2)
Note: All the printing source listing for fields of FD section has been omitted in the compilation report. As well as WORKING-STORAGE SECTION line has been omitted. And afterwards that source printing is resumed.
*CBL MAP/NOMAP and *CBL LIST/NOLIST do not have much effect in source printing of spool file.
Text Manipulation Statements
Text manipulation statements include source code from difference source member and replace the existing source. These statements do the following things:
- To substitute specific text strings within the source code, either within the main program or within a copybook during compilation.
- To include pre-written source code during compilation, known as a copybook.
Below are the compiler-directing statements:
- REPLACE Statement
- COPY Statement
REPLACE Statement
The REPLACE statement is used to replace source program text. The REPLACE statement can occur anywhere in the source program where a character-string can occur. It must be preceded by a separator period except when it is the first statement in a separately compiled program. It must be terminated by a separator period. The REPLACE statement resembles the REPLACING phrase of the COPY statement, except that it acts on the entire source program, not just the text in COPY libraries. It is an easy method of doing simple string substitutions.
Example: COPYBOOK – ZCPYFLD.
01 :TAG:.
05 :TAG:-WEEK PIC 9(02).
05 :TAG:-HOURS PIC 9(03).
05 :TAG:-RATE-PER-HOUR PIC 9(05).
05 :TAG:-GROSS-PAY PIC S9(05)V9(02).
Program COPYFMT11A.
IDENTIFICATION DIVISION.
*-----------------------*
PROGRAM-ID. COPYFMT11A.
AUTHOR. PROGRAMMER.
ENVIRONMENT DIVISION.
*---------------------*
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBMi-Series.
OBJECT-COMPUTER. IBMi-Series.
DATA DIVISION.
*--------------*
REPLACE ==:TAG:== BY ==CLASSA==.
COPY ZCPYFLD OF QCBLLESRC.
REPLACE OFF.
REPLACE ==:TAG:== BY ==CLASSB==.
COPY ZCPYFLD OF QCBLLESRC.
REPLACE OFF.
PROCEDURE DIVISION.
*--------------*
MAIN-PARA.
MOVE 2 TO CLASSA-WEEK
CLASSB-WEEK.
MOVE 40 TO CLASSA-RATE-PER-HOUR.
MOVE 35 TO CLASSB-RATE-PER-HOUR.
REPLACE ==:TAG:== BY ==CLASSA==.
COMPUTE :TAG:-HOURS = 40 * :TAG:-WEEK.
COMPUTE :TAG:-GROSS-PAY = :TAG:-HOURS
* :TAG:-RATE-PER-HOUR
DISPLAY 'CLASSA-GROSS-PAY :=' :TAG:-GROSS-PAY
REPLACE OFF.
REPLACE ==:TAG:== BY ==CLASSB==.
COMPUTE :TAG:-HOURS = 40 * :TAG:-WEEK.
COMPUTE :TAG:-GROSS-PAY = :TAG:-HOURS
* :TAG:-RATE-PER-HOUR.
DISPLAY 'CLASSB-GROSS-PAY :=' :TAG:-GROSS-PAY
REPLACE OFF.
STOP RUN.
Compilation Report – DATA DIVISION – Screenshot:
001500 COPY ZCPYFLD OF QCBLLESRC.
9 +000100 01 CLASSA.
10 +000200 05 CLASSA-WEEK PIC 9(02).
11 +000300 05 CLASSA-HOURS PIC 9(03).
12 +000400 05 CLASSA-RATE-PER-HOUR PIC 9(05).
13 +000500 05 CLASSA-GROSS-PAY PIC S9(05)V9(02).
001600 REPLACE OFF.
001700 REPLACE ==:TAG:== BY ==CLASSB==.
001800 COPY ZCPYFLD OF QCBLLESRC.
14 +000100 01 CLASSB.
15 +000200 05 CLASSB-WEEK PIC 9(02).
16 +000300 05 CLASSB-HOURS PIC 9(03).
17 +000400 05 CLASSB-RATE-PER-HOUR PIC 9(05).
18 +000500 05 CLASSB-GROSS-PAY PIC S9(05)V9(02).
001900 REPLACE OFF.
Compilation Report – PROCEDURE DIVISION – Screenshot:
23 003200 COMPUTE CLASSA-HOURS = 40 * CLASSA-WEEK.
24 003300 COMPUTE CLASSA-GROSS-PAY = CLASSA-HOURS
003400 * CLASSA-RATE-PER-HOUR
25 003500 DISPLAY 'CLASSA-GROSS-PAY :=' CLASSA-GROSS-PAY
003600 REPLACE OFF.
003700 REPLACE ==:TAG:== BY ==CLASSB==.
26 003800 COMPUTE CLASSB-HOURS = 40 * CLASSB-WEEK.
27 003900 COMPUTE CLASSB-GROSS-PAY = CLASSB-HOURS
004000 * CLASSB-RATE-PER-HOUR.
28 004100 DISPLAY 'CLASSB-GROSS-PAY :=' CLASSB-GROSS-PAY
004300 REPLACE OFF.
Output:
CLASSA-GROSS-PAY :=0320000 CLASSB-GROSS-PAY :=0280000
COPY Statement
The COPY directive statement is a library statement that inserts pre-written text into a COBOL program. It is a compiler-directive statement in COBOL programs. In IBM i-Series platform, it offers several features that reduce the length of code in CBLLE programs and allow it to be implemented in multiple sources.
The COPY statement has 3 formats:
- Basic Format – To copy CBL source from different source member, source file or library.
- DDS Translate Format – To copy DDS specifications of PF, LF, DSPF and PRTF files.
- IFS Format – To copy file path of IFS file.
COPY Basic Format
Before compiling a COBOL program containing COPY directive statements, several important points should be considered:
- Copybook source code cannot be compiled.
- The library and source file are optional parts of the ‘COPY’ directive statement.
- Programmers should hardcode the library and source file in the ‘COPY’ statement.
- If the library name is not specified in the ‘COPY’ statement, it will search for the source file in the library list. If the source file is not found in the library list, a compilation error will occur. If the source member is not found within the source file, a compilation error will occur.
- If the source file and library names are not specified in the ‘COPY’ statement, it will search for the source member in the library list. If the source member is not found in the library list, a compilation error will occur. If the same source member exists in two different source files within a library, it will select the source file whose name comes first alphabetically.
- Once compiled, it does not check during execution whether the library, source file, or source member exists in the library list.
- In a COBOL program, two or more paragraphs or sections cannot have the same name, as this will result in a compilation error. Similarly, if two or more copybooks used in the same COBOL program contain paragraphs/sections with the same name, a compilation error will not occur. However, problems can arise when the ‘PERFORM’ verb is used to process paragraphs.
Syntax:
There are multiple variants of the basic formats of the COPY statement. Below are the following:
- COPY CPYBKNAM.
- COPY CPYBKNAM OF SOURCEFILE.
- COPY CPYBKNAM IN SOURCEFILE.
- COPY CPYBKNAM OF LIBRARY-SOURCEFILE.
- COPY CPYBKNAM IN LIBRARY-SOURCEFILE.
Example1:
- Demo how ILE COBOL program selects library & source file from library list.
- All the copybook source has same paragraph name. And the program is compiled perfectly.
- When the source was compiled the PIOLAB library was above of the TESTLAB library. And the main source is in TESTLAB.
SRCFILE (TESTLAB/QCBLLESRC) SRCMBR(COPYFMT01C).
IDENTIFICATION DIVISION.
*-----------------------*
PROGRAM-ID. COPYFMT01C.
AUTHOR. PROGRAMMER.
PROCEDURE DIVISION.
*-------------------*
MAIN-PARA.
COPY INITIAL01.
COPY INITIAL01 OF QCOPYSRC.
COPY INITIAL01 OF TESTLAB-QCOPYSRC.
COPY INITIAL01 IN QCOPYSRC.
COPY INITIAL01 IN PIOLAB-QCOPYSRC.
STOP RUN.
SRCFILE (PIOLAB/QCBLLESRC) SRCMBR(INITIAL01).
000-INITIAL-PARA.
DISPLAY 'COPYBOOK OF PIOLAB/QCBLLESRC/INITIAL01'.
000-INITIAL-PARA-EXIT. EXIT.
SRCFILE (PIOLAB/QCOPYSRC) SRCMBR(INITIAL01).
000-INITIAL-PARA.
DISPLAY 'COPYBOOK OF PIOLAB/QCOPYSRC/INITIAL01'.
000-INITIAL-PARA-EXIT. EXIT.
SRCFILE (TESTLAB/QCBLLESRC) SRCMBR(INITIAL01).
000-INITIAL-PARA.
DISPLAY 'COPYBOOK OF PIOLAB/QCBLLESRC/INITIAL01'.
000-INITIAL-PARA-EXIT. EXIT.
SRCFILE (TESTLAB/QCOPYSRC) SRCMBR(INITIAL01).
000-INITIAL-PARA.
DISPLAY 'COPYBOOK OF PIOLAB/QCOPYSRC/INITIAL01'.
000-INITIAL-PARA-EXIT. EXIT.
Output:
COPYBOOK OF PIOLAB/QCBLLESRC/INITIAL01 COPYBOOK OF PIOLAB/QCOPYSRC/INITIAL01 COPYBOOK OF TESTLAB/QCOPYSRC/INITIAL01 COPYBOOK OF PIOLAB/QCOPYSRC/INITIAL01 COPYBOOK OF PIOLAB/QCOPYSRC/INITIAL01
SUPPRESS Phrase
The SUPPRESS phrase causes a COPY statement to suppress the listing of copied statements. For its duration, this type of COPY statement overrides any *CONTROL or *CBL statement. If the copied member contains *CONTROL or *CBL statements, the last one runs once the COPY member has been processed.
REPLACING Phrase
Replacing phrase in COPY directive statement is almost equivalent to COBOL REPLACE directive statement.
Rules of replacing phrase:
- Programmers can replace one full string to another, where strings are not partial.
- When partial string replacement is used then it should use “:TAG:” or “open & close” bracket techniques.
- Programmers can replace one string to another, where strings are not partial.
Example2: Demonstrate Replacing Phrase in the ‘COPY’ statement.
SRCFILE (TESTLAB/QCBLLESRC) SRCMBR(COPYFMT12A).
IDENTIFICATION DIVISION.
*-----------------------*
PROGRAM-ID. COPYFMT12A.
AUTHOR. PROGRAMMER.
ENVIRONMENT DIVISION.
*---------------------*
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBMi-Series.
OBJECT-COMPUTER. IBMi-Series.
DATA DIVISION.
*--------------*
COPY ZCPYFLD OF QCBLLESRC
REPLACING ==:TAG:== BY ==CLASSA==.
COPY ZCPYFLD OF QCBLLESRC
REPLACING ==:TAG:== BY ==CLASSB==.
COPY ZCPYFLDA OF QCBLLESRC
REPLACING ==(GROUP)== BY ==CLASSC==,
MAX BY 52.
Copybook – ZCPYFLD: SRCFILE (TESTLAB/QCBLLESRC) SRCMBR(ZCPYFLD).
01 :TAG:.
05 :TAG:-WEEK PIC 9(02).
05 :TAG:-HOURS PIC 9(03).
05 :TAG:-RATE-PER-HOUR PIC 9(05).
05 :TAG:-GROSS-PAY PIC S9(05)V9(02).
Copybook – ZCPYFLDA: SRCFILE (TESTLAB/QCBLLESRC) SRCMBR(ZCPYFLDA).
01 (GROUP).
05 (GROUP)-WEEK PIC 9(02).
05 (GROUP)-HOURS PIC 9(03).
05 (GROUP)-RATE-PER-HOUR PIC 9(05).
05 (GROUP)-GROSS-PAY PIC S9(05)V9(02).
05 (GROUP)-GROSS-HOURS PIC S9(04) OCCURS MAX TIMES.
Screenshot of compilation report:
001500 COPY ZCPYFLD OF QCBLLESRC 001600 REPLACING ==:TAG:== BY ==CLASSA==. +000100 01 CLASSA. +000200 05 CLASSA-WEEK PIC 9(02). +000300 05 CLASSA-HOURS PIC 9(03). +000400 05 CLASSA-RATE-PER-HOUR PIC 9(05). +000500 05 CLASSA-GROSS-PAY PIC S9(05)V9(02). 001800 COPY ZCPYFLD OF QCBLLESRC 001900 REPLACING ==:TAG:== BY ==CLASSB==. +000100 01 CLASSB. +000200 05 CLASSB-WEEK PIC 9(02). +000300 05 CLASSB-HOURS PIC 9(03). +000400 05 CLASSB-RATE-PER-HOUR PIC 9(05). +000500 05 CLASSB-GROSS-PAY PIC S9(05)V9(02). 002000 COPY ZCPYFLDA OF QCBLLESRC 002100 REPLACING ==(GROUP)== BY ==CLASSC==, 002200 MAX BY 52. +000100 01 CLASSC. +000200 05 CLASSC-WEEK PIC 9(02). +000300 05 CLASSC-HOURS PIC 9(03). +000400 05 CLASSC-RATE-PER-HOUR PIC 9(05). +000500 05 CLASSC-GROSS-PAY PIC S9(05)V9(02). +000600 05 CLASSC-GROSS-HOURS PIC S9(04) OCCURS 52 TIMES.
Note: When REPLACING == GROUP == BY ==CLASSC== is used, it will not work. It will work for header line, GROUP will be replaced by CLASSC, but rest GROUP- will not be replaced. As for replacing before and after the word should be same blanks, colons, or open & close brackets. This happens in V7R3 or below systems.
LEADING & TRAILING Phrase
When the LEADING or TRAILING keyword of the REPLACING phrase is specified, each operand of the REPLACING phrase must be a partial-word. These phrases have been added in V7R4 or above systems.
Example3: Demonstrate Replacing Leading Phrase in the ‘COPY’ statement in V7R4 or above statements.
SRCFILE (TESTLAB/QCBLLESRC) SRCMBR(COPYFMT12E).
IDENTIFICATION DIVISION.
*-----------------------*
PROGRAM-ID. COPYFMT12E.
AUTHOR. PROGRAMMER.
ENVIRONMENT DIVISION.
*---------------------*
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBMi-Series.
OBJECT-COMPUTER. IBMi-Series.
DATA DIVISION.
*--------------*
COPY ZCPYFLDE OF QCOBOL
REPLACING LEADING ==GROUP== BY ==CLASSC==,
MAX BY 52.
Copybook – ZCPYFLDE: SRCFILE (TESTLAB/QCOBOL) SRCMBR(ZCPYFLDE).
01 GROUP-GV.
05 GROUP-WEEK PIC 9(02).
05 GROUP-HOURS PIC 9(03).
05 GROUP-RATE-PER-HOUR PIC 9(05).
05 GROUP-GROSS-PAY PIC S9(05)V9(02).
05 GROUP-GROSS-HOURS PIC S9(04) OCCURS MAX TIMES.
Screenshot of compilation report:
DATA DIVISION.
* -------------*
WORKING-STORAGE SECTION.
COPY ZCPYFLDE OF QCOBOL
REPLACING LEADING ==GROUP== BY ==CLASSD==
MAX BY 52.
01 CLASSD-GV.
05 CLASSD-WEEK PIC 9(02).
05 CLASSD-HOURS PIC 9(03).
05 CLASSD-RATE-PER-HOUR PIC 9(05).
05 CLASSD-GROSS-PAY PIC S9(05)V9(02).
05 CLASSD-GROSS-HOURS PIC S9(04) OCCURS 52 TIMES.
COPY DDS Translate
COPY statement is also used to translate DDS file layout to incorporate external file descriptions from the DDS files on an IBM i system into a COBOL program. There are mainly 4 variants of the COPY DDS translate
- COPY DD-
- COPY DDR-
- COPY DDS-
- COPY DDSR-
Syntax:
Explanation of terms used in COPY DDS translate statements:
format-name Phrase
The ‘format-name’ phrase refers to the externally described record format of the DDS file.
e.g. (ORDERR is record format name of the physical file ORDER)
COPY DD-ORDERR OF ORDER. COPY DDR-ORDERR OF ORDER. COPY DDS-ORDERR OF ORDER. COPY DDSR-ORDERR OF ORDER.
ALL-FORMATS Phrase
The ‘ALL-FORMATS’ phrase refers to all the record formats of the externally described record format of the DDS file. For single record format files in ILE COBOL program ‘format-name’ or ‘ALL-FORMATS’ is same. e.g.
COPY DD-ALL-FORMATS OF ORDER. COPY DDR-ALL-FORMATS OF ORDER. COPY DDS-ALL-FORMATS OF ORDER. COPY DDSR-ALL-FORMATS OF ORDER
-I or WITH I-FIELDS/-O or O-FIELDS/-I-O or I-O-FIELDS Phrase
This tells compiler translate DDS only for either input fields or output fields or both. This required only for display files (DSPF) as user intervention is required. For, PF, LF, and PRTF files this phrase is not required. If neither -I or I-FIELDS, nor -O or O-FIELDS is specified, then -I-O or I-O-FIELDS is assumed.
* Use any of the copybook for input fields of the RCDFMT01
COPY DD-RCDFMT01-I.
COPY DD-RCDFMT01 WITH I-FIELDS.
* Use any of the copybook for output fields of the RCDFMT01
COPY DD-RCDFMT01-O.
COPY DD-RCDFMT01 WITH O-FIELDS.
* Use any of the copybook for both input-output fields of the RCDFMT01
COPY DD-RCDFMT01-I-O
COPY DD-RCDFMT01 WITH I-O-FIELDS.
-INDICATOR/-INDICATORS/-INDIC Phrase
WITH INDICATOR/INDICATORS/INDIC Phrase
The INDICATOR attribute specifies whether or not data description entries are generated for indicators. If the INDICATOR attribute is specified, data description entries are generated for indicators, but not for data fields. If I-INDICATOR is specified, data description entries for input indicators are generated for indicators used in the input record area. If O-INDICATOR is specified, data description entries for output (option) indicators are generated for indicators used in the output record area. If I-O-INDICATOR is specified or assumed, separate data description entries for both input and output indicators are generated for indicators used in the input and output record areas.
NULL-MAP/NULL-MAP-KEY/NULL-MAP-ALPHANUMERIC
When a field is defined with keyword ALWNULL in DDS, the COPY DDS statement identifies the field as null-capable with a comment. In order to work with null-capable fields, use null-indicator specifying like RRN1, field FLD01 is null-capable and currently it contains value NULL or not. e.g.
* Use below copybook to get null-capable key fields of ORDER file
COPY DDS-ALL-FORMATS OF ORDER
WITH NULL-KEY-MAP
WITH NULL-MAP.
PREFIX BY Phrase
The PREFIX BY phrase add a literal in all DDS field names. Programmer should use PREFIX BY to avoid ambiguity of fields names in ILE COBOL program and it helps in PROCEDURE DIVISION statements not to use ‘OF’ clause for every field. e.g.
COPY DDR-DTAMSTR OF DTAMSTPF
WITH PRFIX BY ‘A1-‘.
ALIAS Phrase
The ALIAS phrase is used to select alias name rather than DDS field name.
VLR Phrase
The VLR phrase option should be used with variable record files. The option specifies copying from variable-length fields. e.g.
COPY DDR-DTAMSTR OF DTAMSTPF
WITH VLR.
This overrides the CVTOPT(*VARCHAR) option on the CRTCBLMOD and CRTBNDCBL commands if used. This also overrides the PROCESS VARCHAR directive statement if used.
COPY DD- (Without & With ALIAS Keyword)
DD option (without ALIAS keyword in DDS):
The ‘DD’ phrase option without ALIAS keyword in the COPY statement translates DDS file fields data type compatible with ILE COBOL program data type.
Disadvantage of this option:
- If the field name (Actual name) having data types – Variable Character field, L(Date), T(Time), Z(Timestamp) and F (Floating fields) are renamed as FILLER.
- If the fields having data types – L(Date), T(Time) or Z(Timestamp), then program fields are translated to alphanumeric fields like PIC X (10), PIC X (08), PIC X (26). It is not converted to FORMAT date, time and timestamp fields.
- If the field name is having characters @, #, $, and _ then, program will not be compiled.
Example 1: DTAMSTPF file – DTAMSTR record format.
PF – No key file – COBOL Source – COPY DD- and without ALIAS OPTION.
A R DTAMSTR
* -- Character fields --
A CHRDFT 10 DFT('AK')
A FIXCHR 10A
A VARCHR 10A VARLEN(2) ALWNULL
* -- Numeric fields --
A ZONED1 6S 0
A ZONED2 6S 2
A PACKED1 6 0
A PACKED2 6P 0 ALWNULL
A PACKED3 6P 2 ALWNULL
* -- Date, Time & Timestamp fields --
A STRDATE L DATFMT(*ISO)
A ENDDATE L DATFMT(*YMD) DATSEP('/')
A STRTIME T TIMFMT(*HMS) TIMSEP(':')
A CURTIMS Z
* -- Floating fields --
A SNGFLT 4F 2 FLTPCN(*SINGLE)
A DBLFLT 17F 6 FLTPCN(*DOUBLE)
COBOL Source – COPY DD- and without ALIAS OPTION.
IDENTIFICATION DIVISION.
* -----------------------*
PROGRAM-ID. CPYDDSALL.
AUTHOR. PROGRAMMER.
ENVIRONMENT DIVISION.
* --------------------*
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT DTAMST-FILE
ASSIGN TO DATABASE-DTAMSTPF
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL.
DATA DIVISION.
* -------------*
FILE SECTION.
FD DTAMST-FILE LABEL RECORDS ARE STANDARD.
01 DTAMSTPF-RECORD.
COPY DD-DTAMSTR OF DTAMSTPF.
PROCEDURE DIVISION.
* ------------------*
MAINLINE-PARA.
OPEN INPUT DTAMST-FILE.
CLOSE DTAMST-FILE. STOP RUN.
Compilation report – FD Section of COPY DD-DTAMSTR without ALIAS
FILE SECTION.
FD DTAMST-FILE LABEL RECORDS ARE STANDARD.
01 DTAMSTPF-RECORD.
COPY DD-DTAMSTR OF DTAMSTPF.
* I-O FORMAT:DTAMSTR FROM FILE DTAMSTPF OF LIBRARY TESTLAB
*
05 DTAMSTR.
06 CHRDFT PIC X(10).
06 FIXCHR PIC X(10).
06 FILLER PIC X(12).
* (Variable length field) //VARLEN(2)
* (Null-capable field) //ALWNULL
06 ZONED1 PIC S9(6).
06 ZONED2 PIC S9(4)V9(2).
06 PACKED1 PIC S9(6) COMP-3.
06 PACKED2 PIC S9(6) COMP-3.
* (Null-capable field)
06 PACKED3 PIC S9(4)V9(2) COMP-3.
* (Null-capable field)
06 FILLER PIC X(10).
* (Date field) //*ISO – YYYY-MM-DD
06 FILLER PIC X(8).
* (Date field) //*YMD/ – YY/MM/DD
06 FILLER PIC X(8).
* (Time field) //*HMS: – HH:MM:SS
06 FILLER PIC X(26).
* (Timestamp field) //YYYY-MM-DD-HH:MM:SS:Ms.
06 FILLER PIC 9(5) COMP-4.
* (Floating-point field) //4F 2
06 FILLER PIC 9(10) COMP-4.
* (Floating-point field) //17F 6
Use of PROCESS directive to remove disadvantage:
-
There are multiple FILLER fields in the compilation of the DD-DTAMSTR. This do not allow to work with – VARCHAR, DATE, TIME, TIMESTAMP and FLOAT. This issue can be resolved using by either of 2 methods:
-
Using below PROCESS directive statement before ID DIVISION and then compiling.
PROCESS VARCHAR, DATETIME, FLOAT. IDENTIFICATION DIVISION. * -----------------------* PROGRAM-ID. CPYDDSALL.
-
Using parameter CVTOPT (VARCHAR FLOAT DATE TIME TIMESTAMP) during compilation.
CRTBNDCBL PGM(TESTLAB/CPYDDSALL) SRCFILE(TESTLAB/QCOBOL) SRCMBR(CPYDDSALL) CVTOPT(*VARCHAR *DATETIME *FLOAT)
-
-
DATE, TIME and TIMESTAMP fields are translated into alphanumeric fields instead of FORMAT clause. This issue can be resolved using by either of 2 methods:
-
Using below PROCESS directive statement before ID DIVISION and then compiling.
PROCESS DATE, TIME, TIMESTAMP. IDENTIFICATION DIVISION. * -----------------------* PROGRAM-ID. CPYDDSALL.
-
Using parameter CVTOPT (VARCHAR FLOAT DATE TIME TIMESTAMP) during compilation.
CRTBNDCBL PGM(TESTLAB/CPYDDSALL) SRCFILE(TESTLAB/QCOBOL) SRCMBR(CPYDDSALL) CVTOPT(*DATE *TIME *TIMESTAMP)
-
Compilation report – FD Section of COPY DD-DTAMSTR without ALIAS, but using 6 PROCESS Statements (VARCHAR, DATETIME, FLOAT, DATE, TIME, TIMESTAMP)
FILE SECTION.
FD DTAMST-FILE LABEL RECORDS ARE STANDARD.
01 DTAMSTPF-RECORD.
COPY DD-DTAMSTR OF DTAMSTPF.
* I-O FORMAT:DTAMSTR FROM FILE DTAMSTPF OF LIBRARY TESTLAB
*
05 DTAMSTR.
06 CHRDFT PIC X(10).
06 FIXCHR PIC X(10).
06 VARCHR.
* (Variable length field)
49 VARCHR-LENGTH PIC S9(4) COMP-4.
49 VARCHR-DATA PIC X(10).
* (Null-capable field)
06 ZONED1 PIC S9(6).
06 ZONED2 PIC S9(4)V9(2).
06 PACKED1 PIC S9(6) COMP-3.
06 PACKED2 PIC S9(6) COMP-3.
* (Null-capable field)
06 PACKED3 PIC S9(4)V9(2) COMP-3.
* (Null-capable field)
06 STRDATE FORMAT DATE "@Y-%m-%d".
* (Date field)
06 ENDDATE FORMAT DATE "%y/%m/%d".
* (Date field)
06 STRTIME FORMAT TIME "%H:%M:%S".
* (Time field)
06 CURTIMS FORMAT TIMESTAMP.
* (Timestamp field)
06 SNGFLT COMP-1.
* (Floating-point field)
06 DBLFLT COMP-2.
* (Floating-point field)
Note: Now programmers can use all fields, there are no FILLER file field and DATE-TIME class FORMAT clause data types are available to use.
DD option (with ALIAS keyword in DDS):
The DD option with the ALIAS keyword prioritizes alias names over internal DDS field names. The specification of an alias name in DDS allows a data name of up to 30 characters to be included in the COBOL program. When the DD option with the ALIAS option is used, any alias names present replace the corresponding DDS field names. All underscores (‘_’) in the alias names are translated into hyphens (‘-’) before any replacing occurs.
Example 2: DTAMSTPF1 file – DTAMSTR record format.
PF – No key file – COBOL Source – COPY DD- and with ALIAS OPTION.
A R DTAMSTR
* -- Character fields --
A CHRDFT 10 ALIAS(FIXED_CHAR_DFT)
A FIXCHR 10A ALIAS(FIXED_CHAR)
A VARCHR 10A ALIAS(VARIABLE_CHAR)
A VARLEN(2)
* -- Numeric fields --
A ZONED1 6S 0 ALIAS(ZONED1_NUMERIC)
A ZONED2 6S 2 ALIAS(ZONED2_NUMERIC)
A PACKED1 6 0 ALIAS(PACKED1_NUMERIC)
A PACKED2 6P 0 ALIAS(PACKED2_NUMERIC)
A ALWNULL
A PACKED3 6P 2 ALIAS(PACKED3_NUMERIC)
A ALWNULL
* -- Date, Time & Timestamp fields --
A STRDATE L DATFMT(*ISO)
A ALIAS(START_DATE)
A ENDDATE L DATFMT(*YMD) DATSEP('/')
A ALIAS(END_DATE)
A STRTIME T TIMFMT(*HMS) TIMSEP(':')
A ALIAS(START_TIME)
A CURTIMS Z ALIAS(CURRENT_TIMESTAMP)
* -- Floating fields --
A SNGFLT 4F 2 FLTPCN(*SINGLE)
A ALIAS(FLOAT1_NUM)
A DBLFLT 17F 6 FLTPCN(*DOUBLE)
A ALIAS(FLOAT2_NUM)
COBOL Source – COPY DD- and without ALIAS OPTION.
IDENTIFICATION DIVISION.
* -----------------------*
PROGRAM-ID. CPYDDSALL1.
AUTHOR. PROGRAMMER.
ENVIRONMENT DIVISION.
* --------------------*
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT DTAMST-FILE
ASSIGN TO DATABASE-DTAMSTPF1
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL.
DATA DIVISION.
* -------------*
FILE SECTION.
FD DTAMST-FILE LABEL RECORDS ARE STANDARD.
01 DTAMSTPF-RECORD.
COPY DD-DTAMSTR OF DTAMSTPF.
PROCEDURE DIVISION.
* ------------------*
MAINLINE-PARA.
OPEN INPUT DTAMST-FILE.
CLOSE DTAMST-FILE. STOP RUN.
Compilation report – FD Section of COPY DD-DTAMSTR with ALIAS
FILE SECTION.
FD DTAMST-FILE LABEL RECORDS ARE STANDARD.
01 DTAMSTPF-RECORD.
COPY DD-DTAMSTR OF DTAMSTPF1.
* I-O FORMAT:DTAMSTR FROM FILE DTAMSTPF1 OF LIBRARY TESTLAB
*
05 DTAMSTR.
06 FIXED-CHAR-DFT PIC X(10).
06 FIXED-CHAR PIC X(10).
06 VARIABLE-CHAR.
* (Variable length field)
49 VARIABLE-CHAR-LENGTH
PIC S9(4) COMP-4.
49 VARIABLE-CHAR-DATA
PIC X(10).
06 ZONED1-NUMERIC PIC S9(6).
06 ZONED2-NUMERIC PIC S9(4)V9(2).
06 PACKED1-NUMERIC PIC S9(6) COMP-3.
06 PACKED2-NUMERIC PIC S9(6) COMP-3.
* (Null-capable field)
06 PACKED3-NUMERIC PIC S9(4)V9(2) COMP-3.
* (Null-capable field)
06 START-DATE FORMAT DATE "@Y-%m-%d".
* (Date field)
06 END-DATE FORMAT DATE "%y/%m/%d".
* (Date field)
06 START-TIME FORMAT TIME "%H:%M:%S".
* (Time field)
06 CURRENT-TIMESTAMP FORMAT TIMESTAMP.
* (Timestamp field)
06 FLOAT1-NUM COMP-1.
* (Floating-point field)
06 FLOAT2-NUM COMP-2.
* (Floating-point field)
Note: Now programmers can use all alias name field where underscore is replaced by hyphen in ILE COBOL program instead of actual DDS field names.
COPY DDR-
The ‘DDR’ option without ALIAS keyword does everything that the DD option does. It also replaces the invalid COBOL characters @, #, $, and underscore (_) in a field name with the corresponding valid COBOL characters A, N, D, and hyphen (-) respectively. As well, it removes underscores from the end of a field name.
Example 3: DTAMSTPF2 file – DTAMSTR record format.
PF – No key file – COBOL Source – COPY DDR- and without ALIAS OPTION.
A R DTAMSTR
* -- Character fields --
A CHRDFT@ 10 DFT('AK')
A FIXCHR# 10A
A VARCHR$ 10A VARLEN(2) ALWNULL
* -- Numeric fields --
A ZONED1_ 6S 0
A ZONED_2 6S 2
A PACKED1 6 0
A PACKED2 6P 0 ALWNULL
A PACKED3 6P 2 ALWNULL
* -- Date, Time & Timestamp fields --
A STRDATE L DATFMT(*ISO)
A ENDDATE L DATFMT(*YMD)
A STRTIME T TIMFMT(*HMS) TIMSEP(':')
A CURTIMS Z
* -- Floating fields --
A SNGFLT 4F 2 FLTPCN(*SINGLE)
A DBLFLT 17F 6 FLTPCN(*DOUBLE)
COBOL Source – COPY DDR- and without ALIAS OPTION.
PROCESS VARCHAR DATETIME FLOAT DATE TIME TIMESTAMP.
IDENTIFICATION DIVISION.
* -----------------------*
PROGRAM-ID. CPYDDSALL2.
AUTHOR. PROGRAMMER.
ENVIRONMENT DIVISION.
* --------------------*
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT DTAMST-FILE
ASSIGN TO DATABASE-DTAMSTPF2
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL.
DATA DIVISION.
* -------------*
FILE SECTION.
FD DTAMST-FILE LABEL RECORDS ARE STANDARD.
01 DTAMSTPF-RECORD.
COPY DDR-DTAMSTR OF DTAMSTPF2.
PROCEDURE DIVISION.
* ------------------*
MAINLINE-PARA.
OPEN INPUT DTAMST-FILE.
CLOSE DTAMST-FILE. STOP RUN.
Compilation report – FD Section of COPY DDR-DTAMSTR without ALIAS
01 DTAMSTPF-RECORD.
COPY DDR-DTAMSTR OF DTAMSTPF2.
* I-O FORMAT:DTAMSTR FROM FILE DTAMSTPF2 OF LIBRARY ASHWANI
*
05 DTAMSTR.
06 CHRDFTA PIC X(10).
06 FIXCHRN PIC X(10).
06 VARCHRD.
* (Variable length field)
49 VARCHRD-LENGTH PIC S9(4) COMP-4.
49 VARCHRD-DATA PIC X(10).
* (Null-capable field)
06 ZONED1 PIC S9(6).
06 ZONED-2 PIC S9(4)V9(2).
06 PACKED1 PIC S9(6) COMP-3.
06 PACKED2 PIC S9(6) COMP-3.
* (Null-capable field)
06 PACKED3 PIC S9(4)V9(2) COMP-3.
* (Null-capable field)
06 STRDATE FORMAT DATE "@Y-%m-%d".
* (Date field)
06 ENDDATE FORMAT DATE "%y/%m/%d".
* (Date field)
06 STRTIME FORMAT TIME "%H:%M:%S".
* (Time field)
06 CURTIMS FORMAT TIMESTAMP.
* (Timestamp field)
06 SNGFLT COMP-1.
* (Floating-point field)
06 DBLFLT COMP-2.
* (Floating-point field)
- CHRDFT@ is translated to CHRDFTA.
- FIXCHR# is translated to FIXCHRN.
- VARCHR$is translated to VARCHRD. Subfields have also adopted ‘$’ to ‘D’.
- ZONED1_ is translated to ZONED1. (Underscore is removed).
- ZONED_2 is translated to ZONED-2.
Note: Now programmers can use replaced DDS fields in ILE COBOL program.
The ‘DDR’ option with ALIAS keyword is exactly same as that of the ‘DD’ option with ALIAS keyword.
The ‘DDR’ option with ALIAS keyword prefers alias names over internal DDS field names.
COPY DDS-
The DDS option copies in the internal DDS field names for the specified DDS format. In this option ALIAS keyword does not get the precedence over internal DDS field.
Disadvantage of this option:
- If the field name (Actual name) having data types – Variable Character field, L(Date), T(Time),
Z(Timestamp) and F (Floating fields) are renamed as FILLER.
(Solution: PROCESS VARCHAR DATETIME FLOAT) - If the fields having data types – L(Date), T(Time) or Z(Timestamp), then program fields are translated to
alphanumeric fields like PIC X (10), PIC X (08), PIC X (26). It is not converted to FORMAT date, time and
timestamp fields.
(Solution: PROCESS DATE TIME TIMESTAMP) - If the field name is having characters @, #, $, and _ then, program will not be compiled.
Example 4: DTAMSTPF3 file – DTAMSTR record format.
PF – No key file – COBOL Source – COPY DDS- and without ALIAS OPTION.
A R DTAMSTR
* -- Character fields --
A CHRDFT 10 DFT('AK')
A FIXCHR 10A
A VARCHR 10A VARLEN(2) ALWNULL
* -- Numeric fields --
A ZONED1 6S 0
A ZONED2 6S 2
A PACKED1 6 0
A PACKED2 6P 0 ALWNULL
A PACKED3 6P 2 ALWNULL
* -- Date, Time & Timestamp fields --
A STRDATE L DATFMT(*ISO)
A ENDDATE L DATFMT(*YMD)
A STRTIME T TIMFMT(*HMS) TIMSEP(':')
A CURTIMS Z
* -- Floating fields --
A SNGFLT 4F 2 FLTPCN(*SINGLE)
A DBLFLT 17F 6 FLTPCN(*DOUBLE)
COBOL Source – COPY DDS- and without ALIAS OPTION.
PROCESS VARCHAR DATETIME FLOAT DATE TIME TIMESTAMP.
IDENTIFICATION DIVISION.
* -----------------------*
PROGRAM-ID. CPYDDSALL3.
AUTHOR. PROGRAMMER.
ENVIRONMENT DIVISION.
* --------------------*
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT DTAMST-FILE
ASSIGN TO DATABASE-DTAMSTPF3
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL.
DATA DIVISION.
* -------------*
FILE SECTION.
FD DTAMST-FILE LABEL RECORDS ARE STANDARD.
01 DTAMSTPF-RECORD.
COPY DDS-DTAMSTR OF DTAMSTPF3.
PROCEDURE DIVISION.
* ------------------*
MAINLINE-PARA.
OPEN INPUT DTAMST-FILE.
CLOSE DTAMST-FILE. STOP RUN.
Compilation report – FD Section of COPY DDS-DTAMSTR without ALIAS
01 DTAMSTPF-RECORD.
COPY DDR-DTAMSTR OF DTAMSTPF3.
* I-O FORMAT:DTAMSTR FROM FILE DTAMSTPF3 OF LIBRARY ASHWANI
*
05 DTAMSTR.
06 CHRDFT PIC X(10).
06 FIXCHR PIC X(10).
06 VARCHR.
* (Variable length field)
49 VARCHR-LENGTH PIC S9(4) COMP-4.
49 VARCHR-DATA PIC X(10).
* (Null-capable field)
06 ZONED1 PIC S9(6).
06 ZONED2 PIC S9(4)V9(2).
06 PACKED1 PIC S9(6) COMP-3.
06 PACKED2 PIC S9(6) COMP-3.
* (Null-capable field)
06 PACKED3 PIC S9(4)V9(2) COMP-3.
* (Null-capable field)
06 STRDATE FORMAT DATE "@Y-%m-%d".
* (Date field)
06 ENDDATE FORMAT DATE "%y/%m/%d".
* (Date field)
06 STRTIME FORMAT TIME "%H:%M:%S".
* (Time field)
06 CURTIMS FORMAT TIMESTAMP.
* (Timestamp field)
06 SNGFLT COMP-1.
* (Floating-point field)
06 DBLFLT COMP-2.
* (Floating-point field)
In this option, if ALIAS keyword is used and programmer want to use alias name in program then use ‘WITH ALIAS Phrase’.
DATA DIVISION.
* -------------*
FILE SECTION.
FD DTAMST-FILE LABEL RECORDS ARE STANDARD.
01 DTAMSTPF-RECORD.
COPY DDS-DTAMSTR OF DTAMSTPF4
WITH ALIAS.
COPY DDSR-
The DDSR option does everything that the DDS option does. It also copies the internal DDS field names in the specified DDS format, replacing the invalid COBOL characters @, #, $, and _ with the valid COBOL characters A, N, D, and hyphen (-) accordingly. This option also removes any underscores from the ends of the field names. In this option ALIAS keyword does not get the precedence over internal DDS field.
Example 5: DTAMSTPF5 file – DTAMSTR record format./p>
PF – No key file – COBOL Source – COPY DDSR- and without ALIAS OPTION.
A R DTAMSTR
* -- Character fields --
A CHRDFT@ 10 DFT('AK')
A FIXCHR# 10A
A VARCHR$ 10A VARLEN(2) ALWNULL
* -- Numeric fields --
A ZONED1_ 6S 0
A ZONED_2 6S 2
A PACKED1 6 0
A PACKED2 6P 0 ALWNULL
A PACKED3 6P 2 ALWNULL
* -- Date, Time & Timestamp fields --
A STRDATE L DATFMT(*ISO)
A ENDDATE L DATFMT(*YMD)
A STRTIME T TIMFMT(*HMS) TIMSEP(':')
A CURTIMS Z
* -- Floating fields --
A SNGFLT 4F 2 FLTPCN(*SINGLE)
A DBLFLT 17F 6 FLTPCN(*DOUBLE)
COBOL Source – COPY DDSR- and without ALIAS OPTION.
PROCESS VARCHAR DATETIME FLOAT DATE TIME TIMESTAMP.
IDENTIFICATION DIVISION.
* -----------------------*
PROGRAM-ID. CPYDDSALL5.
AUTHOR. PROGRAMMER.
ENVIRONMENT DIVISION.
* --------------------*
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT DTAMST-FILE
ASSIGN TO DATABASE-DTAMSTPF5
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL.
DATA DIVISION.
* -------------*
FILE SECTION.
FD DTAMST-FILE LABEL RECORDS ARE STANDARD.
01 DTAMSTPF-RECORD.
COPY DDSR-DTAMSTR OF DTAMSTPF5.
PROCEDURE DIVISION.
* ------------------*
MAINLINE-PARA.
OPEN INPUT DTAMST-FILE.
CLOSE DTAMST-FILE. STOP RUN.
Compilation report – FD Section of COPY DDSR-DTAMSTR without ALIAS
01 DTAMSTPF-RECORD.
COPY DDSR-DTAMSTR OF DTAMSTPF5.
* I-O FORMAT:DTAMSTR FROM FILE DTAMSTPF5 OF LIBRARY ASHWANI
*
05 DTAMSTR.
06 CHRDFTA PIC X(10).
06 FIXCHRN PIC X(10).
06 VARCHRD.
* (Variable length field)
49 VARCHRD-LENGTH PIC S9(4) COMP-4.
49 VARCHRD-DATA PIC X(10).
* (Null-capable field)
06 ZONED1 PIC S9(6).
06 ZONED-2 PIC S9(4)V9(2).
06 PACKED1 PIC S9(6) COMP-3.
06 PACKED2 PIC S9(6) COMP-3.
* (Null-capable field)
06 PACKED3 PIC S9(4)V9(2) COMP-3.
* (Null-capable field)
06 STRDATE FORMAT DATE "@Y-%m-%d".
* (Date field)
06 ENDDATE FORMAT DATE "%y/%m/%d".
* (Date field)
06 STRTIME FORMAT TIME "%H:%M:%S".
* (Time field)
06 CURTIMS FORMAT TIMESTAMP.
* (Timestamp field)
06 SNGFLT COMP-1.
* (Floating-point field)
06 DBLFLT COMP-2.
* (Floating-point field)
- CHRDFT@ is translated to CHRDFTA.
- FIXCHR# is translated to FIXCHRN.
- VARCHR$is translated to VARCHRD. Subfields have also adopted ‘$’ to ‘D’.
- ZONED1_ is translated to ZONED1. (Underscore is removed).
- ZONED_2 is translated to ZONED-2.
Note: Now programmers can use replaced DDS fields in ILE COBOL program
In this option, if ALIAS keyword is used and programmer want to use alias name in program then use ‘WITH ALIAS Phrase’.
DATA DIVISION.
* -------------*
FILE SECTION.
FD DTAMST-FILE LABEL RECORDS ARE STANDARD.
01 DTAMSTPF-RECORD.
COPY DDSR-DTAMSTR OF DTAMSTPF6
WITH ALIAS.
Conditional Directives
The conditional compilation directive statements allow programmers to conditionally include or exclude sections of source code from the compile. Including and excluding of sections is similar to *CBL SOURCE and *CBL NOSOURCE of listing control directive statements. At the same time, conditional directive includes sections or excludes sections of codes which impacts code flow.
Below are the conditional compiler directives:
- >>DEFINE Statement
- >>IF Statement (>>ELSE, >>END-IF)
- >>EVALUATE Statement (>>WHEN, >>WHEN OTHER, >>END-EVALUATE Statement)
The conditional directive statements are compatible with V7R4M0 or above. These conditional statements start by ‘>>’. This symbol tells compiler that conditional directive statements are being used. These ILE COBOL conditional directive statements are similar to ILE RPG conditional directive statements /DEFINE, /UNDEFINE, /IF, /ELSEIF, /ELSE, /ENDIF, /EOF.
>>DEFINE Statement
The DEFINE directive statement defines or undefines a compilation variable. The compilation variables can be used within any of the conditional compilation directives (DEFINE, EVALUATE, and IF). These variables cannot be used with program variables, file field variables, constants, figurative constants.
Syntax:
Explanation of terms:
‘>>’
Conditional directive statement starts from AREA A or AREA B from a new line. It begins with ‘>>’ sign.
DEFINE
DEFINE directive statement defines or undefines a compilation variable. There are few systems
defined compilation variables also called as predefined compilation variables. Compilation variable can be a
literal or arithmetic expression or a parameter field. There are 6 types of the DEFINE statement compilation
variables.
TYPE I. Compilation variable as literal-1.
| >> DEFINE DEFVAR0 AS 6. | DEFVAR0 is a Numeric compilation variable with value as 6. |
| >> DEFINE DEFVAR1 ‘TRUE’. | DEFVAR1 is a Character compilation variable with value ‘TRUE’. |
| >> DEFINE DEFVAR2 ‘M01’. | >DEFVAR2 is Alphanumeric compilation variable with value ‘M01’. |
| >> DEFINE DEFVAR3 B’1’. | DEFVAR3 is a Boolean compilation variable with value *ON. |
| >> DEFINE DEFVAR4 B’0’. | DEFVAR4 is a Boolean compilation variable with value *OFF. |
| >> DEFINE DEFVAR5 AS x’F1’ | DEFVAR5 is a Hexadecimal compilation variable with value x’F1′. |
– The above statement means compilation variable DEFVAR* is defined from this sentence.
After this sentence below conditions will behave as true.
>> IF DEFVAR0 IS DEFINED, >> IF DEFVAR0 IS EQUAL TO 6
>> IF DEFVAR1 IS DEFINED, >> IF DEFVAR1 IS EQUAL TO ‘TRUE’
>> IF DEFVAR2 IS DEFINED, >> IF DEFVAR2 IS EQUAL TO ‘M01’
>> IF DEFVAR3 IS DEFINED, >> IF DEFVAR3 = B’1’, >> IF DEFVAR3
>> IF DEFVAR4 IS DEFINED, >> IF DEFVAR4 = B’0’, >> IF NOT DEFVAR4
>> IF DEFVAR5 IS DEFINED, >> IF DEFVAR5 = x’F1’
– After this sentence below conditions will behave as false.
>> IF DEFVAR0 IS NOT DEFINED, >>IF DEFVAR0 = 5
>> IF DEFVAR1 IS NOT DEFINED, >>IF DEFVAR1 = ‘TRUE1’
>> IF DEFVAR2 IS NOT DEFINED, >>IF DEFVAR2 = ‘M02’
>> IF DEFVAR3 IS NOT DEFINED, >>IF DEFVAR2 = B’0’, >> IF NOT DEFVAR3
>> IF DEFVAR4 IS NOT DEFINED, >>IF DEFVAR3 = B’1’, >> IF DEFVAR4
>> IF DEFVAR5 IS NOT DEFINED, >>IF DEFVAR5 <> x’F1’
TYPE II. Compilation variable as arithmetic expression.
| >> DEFINE DEFAE1 AS 10 * 2. | DEFAE1 is an arithmetic expression with value as 20. |
TYPE III. Compilation variable as parameter.
| >> DEFINE ENV AS PARAMETER. |
ENV is a parameter where value is passed from DEFINE parameter of the CRTBNDCBL or CRTCBLMOD CL
Command. CRTBNDCBL DEFINE(‘ENV’) |Only defined with no value CRTBNDCBL DEFINE(‘ENV = PROD’) |Value = PROD CRTBNDCBL DEFINE(‘ENV,TEST’) |Value = TEST CRTBNDCBL DEFINE(‘ENV as BAU’) |Value = BAU |
– Syntax of DEFINE parameter of the CRTBNDCBL or CRTCBLPGM. This parameter is available in V7R4 or above systems.
– CRTBNDCBL PGM(LIB1/PGM1) SRCFILE(LIB1/SRCPF1) SRCMBR(PGM1)
DEFINE(‘ENV = PROD’)
TYPE IV. Predefined compilation variable.
| >> DEFINE COMPILER-VER |
COMPILER-VER is a predefined compilation variable which returns an integer VVRRMMMM. e.g. for V7R4M0 -> 07040000, for V7R5M0 -> 07050000 |
– The COMPILER-VRM in ILE COBOL is a predefined compilation variable used within conditional compilation directives to check the compiler’s version, release, and modification levels. It has an integer value in the format VVRRMMMM (or VVRRMM depending on the exact platform/version).
– Other predefined compilation variables in ILE COBOL are ARCH, CICS, DLL, DYNAM, SQL, SQLIMS, OPTIMIZE, THREAD.
– Each predefined compilation variable has its own description and valid return values.
– Every IBM system with programming language has their own predefined compilation variables.
- IBM Mainframe – IGY-ARCH, IGY-CICS, IGY-COMPILER-VRM, etc.
- IBM COBOL for Linux on x86 – IGY-ADDR, IGY-CICS, IGY-COMPILER-VRM etc.
- In RPGLE programming on IBMi Platform – *ILERPG, *CRTBNDRPG, *CRTRPGMOD, *VxRxMx, *THREAD_CONCURRENT, *THREAD_SERIALIZE.
TYPE V. Predefined compilation variable as OFF.
| >> DEFINE DEFVAR0 OFF | DEFVAR0 is not defined. |
– DEFVAR0 is not define after this compilation statement.
– After this sentence below conditions will behave as false.
>>IF DEFVAR0 IS DEFINED.
Example:
>> DEFINE DEFVAR0 12.
..
>> DEFINE DEFVAR0 OFF.
>> IF DEFVAR0 IS DEFINED.
MOVE B’1’ TO WS-PGM-VAR. //Exclude
>> ELSE
MOVE B’0’ TO WS-PGM-VAR. //Include
>> END-IF
TYPE VI. Predefined compilation variable as OVERRIDE.
| >> DEFINE VAR AS VAR -2 OVERRIDE | It overrides the last value of compilation variable and does arithmetic process. |
Example:
>> DEFINE VAR 12.
..
>> DEFINE VAR AS VAR – 2 OVERRIDE.
>> IF VAR IS EQUAL TO 10.
MOVE B’1’ TO WS-PGM-VAR. //Include
>> ELSE
MOVE B’0’ TO WS-PGM-VAR. //Exclude
>> END-IF
>>IF Statement
The IF directive provides for a one-way or two-way conditional compilation. The condition is evaluated to true or false. This often involves checking if a condition name defined by DEFINE exists, or a comparison of values.
Syntax:
Multiple variants of >>IF statements:
- >> IF DEFVAR1 IS DEFINED
- >> IF DEFVAR2 IS NOT DEFINED
- >> IF BOOLVAR1
- >> IF BOOLVAR1 AND BOOLVAR2
- >> IF DECVAR1 = 6
>>EVALUATE Statement
The EVALUATE directive provides a multi-branch method of choosing the source lines to include in a compilation group.
FORMAT1
FORMAT2
Example: For DEFINE, OFF, IF, ELSE, DEFINED
>>DEFINE A1 AS ‘DEF’
IDENTIFICATION DIVISION.
* -----------------------*
PROGRAM-ID. DIRECTDEF1.
AUTHOR. PROGRAMMER.
PROCEDURE DIVISION.
* -----------------------*
MAIN-LOGIC.
>>IF A1 IS DEFINED
DISPLAY "Statement1 - Flag A1 is active"
>>ELSE
DISPLAY "Statement2 - Flag A1 is inactive"
>>END-IF
>>DEFINE A1 AS OFF.
>>IF A1 IS DEFINED
DISPLAY "Statement3 - Flag A1 is active"
>>ELSE
DISPLAY "statement4 - Flag A1 is inactive"
>>END-IF
STOP RUN.
Compilation output report:
STMT PL SEQNBR -A 1 B..+....2....+....3....+....4....+....5....+...
1 000200 IDENTIFICATION DIVISION.
000300* -----------------------*
2 000400 PROGRAM-ID. DIRECTDEF1.
3 000500 AUTHOR. PROGRAMMER.
4 000600 PROCEDURE DIVISION.
000700* -----------------------*
000800 MAIN-LOGIC.
5 001000 DISPLAY "Statement1 - Flag A1 is active"
6 001800 DISPLAY "statement4 - Flag A1 is inactive"
7 002000 STOP RUN.
Example2: For DEFINE, PARAMETER, IF, EVALUATE, WHEN
PROCESS APOST.
>>DEFINE ENVTYP AS PARAMETER
>>DEFINE DEBUG AS PARAMETER
IDENTIFICATION DIVISION.
* -----------------------*
PROGRAM-ID. DIRECTDEF2.
AUTHOR. PROGRAMMER.
PROCEDURE DIVISION.
* -----------------------*
MAIN-LOGIC.
>>IF ENVTYP = 'PROD'
DISPLAY 'In Production Environment'
>>ELSE
>>IF ENVTYP = 'TEST'
DISPLAY 'In Test Environment'
>>END-IF
>>END-IF
>>EVALUATE TRUE
>>WHEN DEBUG = '*ON' AND ENVTYP = 'PROD'
DISPLAY 'Do not write into production file'
>>WHEN DEBUG = '*ON' AND ENVTYP = 'TEST'
DISPLAY 'Do not write into test file'
>>WHEN DEBUG = '*OFF' AND ENVTYP = 'PROD'
DISPLAY 'Write into production file'
>>WHEN DEBUG = '*OFF' AND ENVTYP = 'TEST'
DISPLAY 'Write into test file'
>>WHEN OTHER
NEXT STATEMENT
>> END-EVALUATE
STOP RUN.
When compiled with below CRTBNDCBL and DEFINE parameters
ENVTYP = ‘PROD’ and ‘DEBUG = ‘*ON’
CRTBNDCBL PGM(ASHWANI/DIRECTDEF2)
SRCFILE(ASHWANI/QCOBOL)
SRCMBR(DIRECTDEF2)
OPTION(*SOURCE)
DBGVIEW(*SOURCE)
FLAG(29)
DEFINE('ENVTYP as ''PROD''' 'DEBUG as ''*ON''')
Compilation output report:
Generate program information . . . . : *NO
Define names . . . . . . . . . . . . : ENVTYP as 'PROD'
: DEBUG as '*ON'
TARGET CODED CHARACTER SET ID . . . : *SRC
Compiler . . . . . . . . . . . . . . : IBM ILE COBOL
5770WDS V7R5M0 220415 LN IBM ILE COBOL ASHWANI/DIRECTDEF2
S o u r c e
STMT PL SEQNBR -A 1 B..+....2....+....3....+....4....+....5....+....6....+....
000100 PROCESS APOST.
1 000400 IDENTIFICATION DIVISION.
000500* -----------------------*
2 000600 PROGRAM-ID. DIRECTDEF2.
3 000700 AUTHOR. PROGRAMMER.
4 000800 PROCEDURE DIVISION.
000900* -----------------------*
001000 MAIN-LOGIC.
5 001200 DISPLAY 'In Production Environment'
6 002100 DISPLAY 'Do not write into production file'
7 003800 STOP RUN.
Note: Programmers should apply this architecture of program which can be debugged in production by changing DEBUG value.