COBOL Basics
Lets first understand the basic structure of a COBOL program, how it is divided and the reason.
Divisions Of Cobol Program
A COBOL program has 4 divisions and they are
IDENTIFICATION DIVISION
ENVIRONMENT DIVISION
DATA DIVISION
PROCEDURE DIVISION
Identification Division
This is the first and the only mandatory division of a COBOL program. It is used to supply information regarding the identity of the program to the compiler. This is how the division is coded in MARGIN A which starts at 8th byte. Think of this division as a one word documentation of a program.
IDENTIFICATION DIVISION.
Program-ID. HelloWorld.
Author. ProgrammersPub.
Installation. ProgrammersPub.
Date-Written. 18/03/2021.
Date-Compiled. 18/03/2021.
Name | Required |
Program-ID | Mandatory |
Author | Optional |
Installation | Optional |
Date-Written | Optional |
Date-Compiled | Optional |
Environment Division
Environment Division describes the computing environment to the compiler in which the program will run. It consists of two sections,
CONFIGURATION SECTION and INPUT-OUTPUT SECTION. Each section has its own paragraphs as seen below.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. [Value].
OBJECT-COMPUTER. [<code class="language-git">Value</code>].
SPECIAL-NAMES. [<code class="language-git">Value</code>].
INPUT-OUTPUT SECTION.
FILE-CONTROL. [<code class="language-git">Value</code>].
I-O CONTROL. [<code class="language-git">Value</code>].
Name | Description |
SOURCE-COMPUTER | System where the program is compiled |
OBJECT-COMPUTER | System where the program is executed |
SPECIAL-NAMES | Special items such as the currency sign and symbolic characters |
FILE-CONTROL | Provides information about datasets that are used in the program |
I-O CONTROL | Specifies information needed for efficient transmission of data between the external data set and the COBOL program. |
The FILE-CONTROL is used to link the dataset between JCL and COBOL program. Here is an example
IDENTIFICATION DIVISION. 00010000
PROGRAM-ID. REPORT1. 00020000
* 00030000
ENVIRONMENT DIVISION. 00040000
INPUT-OUTPUT SECTION. 00050000
FILE-CONTROL. 00060000
* 00070000
SELECT EXTRACT ASSIGN TO DD1 00080000
ORGANIZATION IS SEQUENTIAL 00090000
ACCESS MODE IS SEQUENTIAL 00100000
FILE STATUS IS WF-EXTRACT. 00110000
* 00120000
SELECT PAY-REPORT ASSIGN TO DD2 00130000
ORGANIZATION IS SEQUENTIAL 00140000
ACCESS MODE IS SEQUENTIAL 00150000
FILE STATUS IS WF-PAY-REPORT. 00160000
* 00170000
Name | Description |
SELECT | The SELECT clause chooses a file in the COBOL program to be associated with an external data set |
ASSIGN | The ASSIGN clause associates the program’s name for the file with the external name for the actual data file. You can define the external name with a DD statement |
ORGANIZATION | The ORGANIZATION clause describes the file’s organization. For QSAM files, the ORGANIZATION clause is optional |
ACCESS MODE | The ACCESS MODE clause defines the manner in which the records are made available for processing: sequential, random, or dynamic. For QSAM and line-sequential files, the ACCESS MODE clause is optional. These files always have sequential organization. |
FILE STATUS | It is a variable where the status of the file is saved. |
For VSAM files, you might have additional statements in the FILE-CONTROL paragraph depending on the type of VSAM file you use.
Data Division
Data Division provides the descriptions of the data to be processed by the program used in input and output operations.
DATA DIVISION.
FILE-SECTION.
...
WORKING-STORAGE SECTION.
...
LINKAGE SECTION.
...
REPORT SECTION.
LOCAL STORAGE SECTION.
SCREEN SECTION.
Example:
IDENTIFICATION DIVISION. 00010000
PROGRAM-ID. REPORT1. 00020000
* 00030000
ENVIRONMENT DIVISION. 00040000
INPUT-OUTPUT SECTION. 00050000
FILE-CONTROL. 00060000
* 00070000
SELECT EXTRACT ASSIGN TO DD1 00080000
ORGANIZATION IS SEQUENTIAL 00090000
ACCESS MODE IS SEQUENTIAL 00100000
FILE STATUS IS WF-EXTRACT. 00110000
* 00120000
SELECT PAY-REPORT ASSIGN TO DD2 00130000
ORGANIZATION IS SEQUENTIAL 00140000
ACCESS MODE IS SEQUENTIAL 00150000
FILE STATUS IS WF-PAY-REPORT. 00160000
* 00170000
DATA DIVISION. 00180002
FILE SECTION. 00190002
* 00191001
*---->INPUT FILE 00192001
* 00193001
FD EXTRACT. 00200001
COPY PAYROLL. 00210001
* 00220001
*----> OUTPUT FILE 00230001
* 00240001
FD PAY-REPORT. 00250001
01 REPORT-REC PIC X(80). 00260001
* 00270001
WORKING-STORAGE SECTION. 00280002
* 00290002
01 WS-DATE-RNAME. 00300003
05 FILLER PIC X(02). 00310002
05 FILLER PIC X(15) VALUE 'PAYROLL REPORT'. 00320002
05 FILLER PIC X(53). 00330002
05 WS-SYS-DATE PIC X(08). 00340002
05 FILLER PIC X(02). 00350002
* 00360002
Name | Description |
FILE-SECTION | The file section defines the structure of data files |
WORKING-STORAGE SECTION | The working-storage section describes data records that are not part of data files but are developed and processed by a program. Think of it as a temporary variable and file structure. |
LINKAGE SECTION | The linkage section describes data made available from another program example when calling sub-programs. |
LOCAL STORAGE SECTION | Same as working storage, but local storage is specific to program invocation. On each invocation, data items defined in the local-storage section are reallocated. Each data item that has a VALUE clause is initialized to the value specified in that clause. |
SCREEN SECTION | Used as inline cics screen. |
Procedure Division
All the data described in the DATA DIVISION are processed here. This division has the custom defined business logic of the program. Example,
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO1.
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
0000-INPUT-PARA.
DISPLAY "HELLO WORLD".
STOP RUN