IBM-Mainframes: by Mani Kumar
IBM-Mainframes: by Mani Kumar
IBM-Mainframes: by Mani Kumar
By Mani Kumar
Background and History
DATA DIVISION : Used to declare variables & File layout of the files.
Describes all the data to be used in a program.
Sections: FILE, WORKING-STORAGE, LINKAGE
FILE SECTION is used to define the files record layout that will be used
in the program.
WORKING-STORAGE is used to define any variables/data that will be
used in the program that is not part of a file section.
LINKAGE SECTION is used in a subprogram to define variables/data
that will be passed as arguments to the Main program.
And also used in Main Programs to define variables that will be used
to accept the input data from PARM parameter in JCL(outside from
the program).
Division’s Contd..
1-6 Sequence Number This area is used to enter the line numbers
of code which automatically assigned by the compiler.
7 Comments This area is used to comment out the existing code to
skip it from execution instead of deleting.
• * This line is a comment.
8-11 Area A This area is used to enter Division names, Section
names, Paragraph names and 01, 77 level no. declarations.
12-72 Area B This area is used to enter all statements in Procedure
Division. To declaration level numbers other than 01, 77 in Data
Division. To declare file attributes in Environment Division.
73-80 User Area/Comment Area This area has been used by
programmers to write the comments. Ignored by Compiler.
COBOL Layout/COBOL Code Sheet (1 - 80)
Sample COBOL Program
IDENTIFICATION DIVISION.
PROGRAM-ID. PGMNAME. can have A-Z/0-9/S.Characters
AUTHOR. AUTHOR NAME. Optional
ENVIRONMENT DIVISION.
CONFIGURATION SECTION. Optional
SOURCE COMPUTER. NAME1.
OBJECT COMPUTER. NAME2.
INPUT-OUTPUT SECTION. will be used only while using files
FILE CONTROL.
Select Logical-File Assign To DDNAME.
File Organization,
File Access mode
File Record Key
File status.
Syntax:
Select Logical-File Assign To DDNAME
Organization is Sequential/Indexed/Relative
Access Mode is Sequential/Random/Dynamic
File Record Key is Key-field Key field of KSDS
File status is WS-STATUS W-S Section Variable
DATA DIVISION.
FILE SECTION.
FD Logical-File File attributes
File recording mode File format
Record contains n characters Length of each record
Block contains 10*n characters Length of each Block
WORKING-STORAGE SECTION.
01 ws-var PIC X(n). Variables Declaration.
LINKAGE SECTION.
01 LK-VAR1 PIC X(n). In 2 scenarios we will use this one.
PROCEDURE DIVISION.
ACCEPT A.
ACCEPT B.
DISPLAY A.
ADD A TO B.
SUB B FROM A.
MOVE WS-VAR1 TO WS-VAR2.
STOP RUN.
My First COBOL Program
IDENTIFICATION DIVISION.
PROGRAM-ID. FirstPgm.
AUTHOR. ABCD.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-Num1 PIC 9(02) VALUE 10.
01 WS-Num2 PIC 9(10) VALUE 30.
01 WS-Result PIC 9(18) VALUE ZEROS.
PROCEDURE DIVISION.
CalculateResult.
MULTIPLY Num1 BY Num2 GIVING Result.
DISPLAY "Result is = ", Result.
STOP RUN.
For numeric items the default justification is Right for Integer part and
Left for Decimal part. And it cannot be changed with Justified clause.
So Justified clause is not allowed to Numeric items
For Alphabetic and Alphanumeric the default justification is Left and it
can be changed with 'Justified Right'. If the receiving field is bigger
than the sending field then the spaces will be filled for unallocated data.
Ex: The below are without Justified & with Justified clauses.
In program In JCL
WORKING-STORAGE SEC.
PROCEDURE DIVISION. OR
IND
DISPLAY: Same as like PrintF in C Language
In program
PROCEDURE DIVISION.
ACCEPT WS-GROUP.
DISPLAY ‘The entered details are :’ WS-GROUP.
In OUTPUT:
The entered details are IND20120305
ARITHMETIC OPERATIONS
TO
Identifier
Identifier FROM
VERB
Literal BY Identifier GIVING Identifier
INTO
For example:
ADD A TO B.
ADD 10 TO B GIVING C.
SUBTRACT B FROM A.
SUBTRACT 10 FROM B GIVING C.
DIVIDE D BY 2 GIVING OUTPUT1.
DIVIDE D BY 4 GIVING OUTPUT2
REMAINDER REMAIN.
The MOVE copies data from the source identifier/literal to one or more
destination identifiers.
The source and destination identifiers can be group or elementary data
items.
When the destination item is alphanumeric or alphabetic (PIC X or A)
data is copies the data from left to right.
When data is moved into the output then the contents of the output item
are completely replaced.
Move with Reference modification is allowed on data types X and A and it
is not allowed on data type 9. And it is used to move the partial data from
source to destination fields.
Move with corresponding is allowed on group items. Data is moved on
fields which having the same names in both the groups and other fields
are remained same.
MOVE
Group Move with corresponding: It is used to move the data from one
group to another group even though the structure of the two groups are not
same. It moves elementary items with same name and leaves other
elements.
Syntax: MOVE CORR group-item-1 TO group-item-2
Ex: 01 IN-REC.
01 DETAIL-REC.
01 IN-REC.
05 EMP-NUM PIC x(4).
05Above
EMP-NUM PIC x(4). are not
two structures
05 EMP-NAME PIC x(10).
05 FILLER PIC x(2) VALUE SPACES.
05 EMP-NAME PIC x(10).
05 EMP-DOJ.
05 FILLER PIC x(2) VALUE SPACES.
10 EMP-DOJ-CCYY PIC 9(4).
05 EMP-DOJ.
10 EMP-DOJ-MM PIC 9(2).
10 EMP-DOJ-MM PIC 9(2).
10 EMP-DOJ-DD PIC 9(2).
10 FILLER PIC X(1) VALUE '/'.
05 EMP-SAL PIC S9(6)V99.
10 EMP-DOJ-DD PIC 9(2).
10 FILLER PIC x(1) VALUE '/'.
10 EMP-DOJ-CCYY PIC 9(4).
05 TOTAL-SALARY PIC ZZZZZZ.99.
MOVE
In the above example, the two structures are not same so simple group
move cannot move elementary to elementary.
So the below syntax moves the data correctly whichever fields having
with same names.
END-IF
END-IF
END-EVALUATE
We can code max 15 levels for IF Condition
FILES
Files are used to store the data. And these files are used in the
programs to read, write, update or delete the data based on the
requirement.
These are mostly used to carry the processing data from one
program to another program.
There are 2 types of files as follow.
1. Physical Sequential files(PS files)
2. Virtual Storage Access Method files(VSAM files)
Sequential files are used to Read and write the data in a
sequential order.
Logical files are given in the COBOL program where as
physical files are supplied in the JCL.
To use the files in the program, we have to follow the flow as
below.
FILES
Syntax:
SELECT LOGICAL-FILE ASSIGN TO DDNAME
ORGANIZATION IS SEQUENTIAL/INDEXED
ACCESSMODE IS SEQUENTIAL/RANDOM/DYNAMIC
RECORD KEY IS VSAM-PKEY
FILE STATUS IS WS-STATUS.
FILES
StudentRecord
StudentID StudentName Course.
9 3 3 4 5 6 7 F r a n k C u r t a i n L M 0 5 1
9 3 3 4 5 6 7 F r a n k C u r t a i n L M 0 5 1
9 3 8 3 7 1 5 T h o m a s H e a l y L M 0 6 8
9 3 4 7 2 9 2 T o n y O ‘ B r i a n L M 0 5 1
9 3 7 8 8 1 1 B i l l y D o w n e s L M 0 2 1
EOF
StudentRecord
StudentID StudentName Course.
9 3 3 4 5 6 7 F r a n k C u r t a i n L M 0 5 1
Students.Dat
9 3 3 4 5 6 7 F r a n k C u r t a i n L M 0 5 1
EOF
FILES
'00' -- Success
'10' -- end of file
'35' -- DD statement missing or DDNAME Misspelled
'39' -- file attribute mismatch
'41' -- Trying to open a opened file
'42' -- Trying to close a file which is not opened
'43' -- Using REWRITE but no prior READ
'46' -- Reading beyond end of file (After file status 10)
'47' -- Using READ but open mode is not compatible.
Other than INPUT/I-O
'48' -- Using WRITE but open mode is not compatible.
Other than OUTPUT/I-O/EXTEND
'49' -- Using REWRITE but open mode is not compatible.
Other than I-O
IDENTIFICATION DIVISION.
PROGRAM-ID. READ1.
A SAMPLE FILES PROGRAM
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE ASSIGN TO INFILE
FILE STATUS IS WS-STATUS.
SELECT OUT-FILE ASSIGN TO OUTFILE.
DATA DIVISION.
FILE SECTION.
FD IN-FILE.
01 IN-REC.
05 IN-EMP-NUM PIC X(04).
05 IN-EMP-NAME PIC X(10).
05 IN-EMP-DOJ PIC 9(08).
05 IN-EMP-STATUS PIC X(01).
05 IN-EMP-SAL PIC 9(08)V99.
FD OUT-FILE.
01 OUT-REC.
05 OUT-EMP-NUM PIC X(04).
05 OUT-EMP-NAME PIC X(10).
05 OUT-EMP-DOJ PIC 9(08).
05 OUT-EMP-STATUS PIC X(01).
05 OUT-EMP-SAL PIC 9(08)V99.
WORKING-STORAGE SECTION.
01 WS-STATUS PIC X(2) VALUE SPACES.
A SAMPLE FILES PROGRAM
PROCEDURE DIVISION.
OPEN INPUT IN-FILE
OUTPUT OUT-FILE.
PERFORM UNTIL WS-STATUS = '10'
READ IN-FILE
IF WS-STATUS = '00'
IF IN-EMP-STATUS = 'A'
MOVE IN-REC TO OUT-REC
WRITE OUT-REC
END-IF
END-IF
END-PERFORM
CLOSE IN-FILE
OUT-FILE.
STOP RUN.
IN JCL
//STEP1 EXEC PGM=READ1
//INFILE DD DSN=USERID.FILE1,DISP=SHR
//OUTFILE DD DSN=USERID.FILE2,
// DISP=(NEW,CATLG,DELETE)
// UNIT=SYSDA,
// SPACE=(CYL,(5,2),RLSE),
// DCB=(RECFM=FB,LRECL=33,BLKSIZE=0)
INITIALIZE
Ex: 01 WS-DATE1.
05 CCYY PIC 9(4) VALUE 2020.
05 FILLER PIC X(1) VALUE '/'.
05 MM PIC 9(2) VALUE 09.
05 FILLER PIC X(1) VALUE '/'.
05 DD PIC 9(2) VALUE 24.
INITIALIZE
PROCEDURE DIVISION.
DISPLAY ‘BEFORE INITIALIZE : ’WS-DATE1.
INITIALIZE WS-DATE1.
DISPLAY ‘AFTER INITIALIZE : ‘ WS-DATE1.
STOP RUN.
01 DETAIL-RECORD.
05 FILLER PIC X(10) VALUE 'AGENT NUM'.
05 FILLER PIC X(5) VALUE SPACES.
05 FILLER PIC X(10) VALUE 'SALES DATE'.
05 FILLER PIC X(5) VALUE SPACES.
05 FILLER PIC X(15) VALUE 'SALES AMOUNT'.
FILLER
In file:
XXXX XXXXXXXXXXXXXXXXXXXX 99999999 99999999
XXXX XXXXXXXXXXXXXXXXXXXX 99999999 99999999
XXXX XXXXXXXXXXXXXXXXXXXX 99999999 99999999
FILLER
3. It is used at the bottom of the file layout for the future
expansion of the file. When a new field is to be added to the file
then include that new field in the end by reducing the filler
length.
The Programs that use this new field are impacted so we will
recompile those programs only. And, Programs that are not using
this new field are not impacted so no need to touch them. Ex: 01
EMP-REC.
05 EMP-NUM PIC X(5).
05 EMP-NAME PIC X(20).
05 EMP-DOJ PIC X(8).
05 EMP-DOB PIC X(8).
05 EMP-PHNUM PIC 9(10).
05 EMP-ZIPCODE PIC 9(6).
05 FILLER PIC X(4).
REDEFINES
The STRING moves characters from the source string into the
destination string from left to right until it encounter the
delimiter or the destination field becomes full.
When a WITH POINTER statement is used then it set the
starting position for insertion into the destination string.
The ON OVERFLOW statements executes if there is string
truncation in the destination string.
Syntax:
MOVE 6 TO PTR.
STRING FIRST-NAME DELIMITED BY SIZE,
MIDDLE-NAME DELIMITED BY SIZE,
LAST-NAME DELIMITED BY SIZE
INTO NAME-OUT WITH POINTER PTR.
1 9 s t o p 0 5 s t o p 8 0
ACCEPT DateStr.
UNSTRING DateStr DELIMITED BY "stop"
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
How the UNSTRING works
The INSPECT scans the Source String from left to right for counting
or replacing character/sub-string under the control of the TALLYING,
REPLACING or CONVERTING statements.
OUTPUT: 'JOHN.JOHNSON@CNO.CO.IN'
INSPECT
Calling Program 1
IDENTIFICATION DIVISION.
Identification Division. PROGRAM-ID. SUB1
. .
. .
Call ‘SUB1’ GO BACK
.
. 2
Call ‘SUB2’
.
.
4
.
3
IDENTIFICATION DIVISION.
PROGRAM-ID. SUB2
1 Program SUB1 is executed .
.
2 Control returns to calling program GO BACK
3 Program SUB2 is executed
•Static Calls
•Dynamic Calls
STATIC CALLS
1. Load module of sub program will be added to main program
during compilation stage and will be part of main program
always.
DYNAMIC CALLS
General Syntax:
COPY COPYBOOKNAME.
Ex: COPY EMPFILE
IDENTIFICATION DIVISION.
PROGRAM-ID. COPYEG1.
AUTHOR. Michael Coughlan.
01 StudentRec.
ENVIRONMENT DIVISION.
FILE-CONTROL.
88 EndOfSF
SELECT StudentFile ASSIGN TO "STUDENTS.DAT" VALUE HIGH-VALUES.
ORGANIZATION IS LINE SEQUENTIAL.
02 StudentNumber PIC 9(7).
DATA DIVISION.
FILE SECTION. 02 StudentName PIC X(60).
FD StudentFile.
COPY COPYFILE1. 02 CourseCode PIC X(4).
By using the Inline Perform we can search for any element in the
table manually but it may consume the time so by using the
Search & SearchAll we can search the element very efficiently
and quickly.
But Search & Searchall could be used only for Indexed Tables
whereas Inline perform logic is used for Subscript Tables.
Searching a Table
A B C D E F G H I J K L
1 2 3 4 5 6 7 8 9 10 11 12
01 LetterTable.
02 TableValues.
03 FILLER PIC X(13)
VALUE "ABCDEFGHIJKLM".
03 FILLER PIC X(13)
VALUE "NOPQRSTUVWXYZ".
02 FILLER REDEFINES TableValues.
03 Letter PIC X OCCURS 26 TIMES
INDEXED BY LetterIdx.
SET LetterIdx TO 1.
SEARCH Letter
AT END DISPLAY "Letter not found!"
WHEN Letter(LetterIdx) = LetterIn
DISPLAY LetterIn, "is in position ", Idx
END-SEARCH.
Using the Search All
A B C D E F G H I J K L
1 2 3 4 5 6 7 8 9 10 11 12
01 LetterTable.
02 TableValues.
03 FILLER PIC X(13)
VALUE "ABCDEFGHIJKLM".
03 FILLER PIC X(13)
VALUE "NOPQRSTUVWXYZ".
02 FILLER REDEFINES TableValues.
03 Letter PIC X OCCURS 26 TIMES
ASCENDING KEY IS Letter
INDEXED BY LetterIdx.
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
1 26 13 = M
ALGORITHM.
Middle = (Lower + Upper) / 2
CASE TRUE
WHEN Letter(Middle) < "Q" THEN Lower = Middle + 1
WHEN Letter(Middle) > "Q" THEN Upper = Middle -1
WHEN Letter(Middle) = "Q" THEN SET ItemFound TO TRUE
WHEN Lower > Upper THEN ItemNotInTable TO TRUE
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
14 26 13 = M
ALGORITHM.
Middle = (Lower + Upper) / 2
CASE TRUE
WHEN Letter(Middle) < "Q" THEN Lower = Middle + 1
WHEN Letter(Middle) > "Q" THEN Upper = Middle -1
WHEN Letter(Middle) = "Q" THEN SET ItemFound TO TRUE
WHEN Lower > Upper THEN ItemNotInTable TO TRUE
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
14 26 20 = T
ALGORITHM.
Middle = (Lower + Upper) / 2
CASE TRUE
WHEN Letter(Middle) < "Q" THEN Lower = Middle + 1
WHEN Letter(Middle) > "Q" THEN Upper = Middle -1
WHEN Letter(Middle) = "Q" THEN SET ItemFound TO TRUE
WHEN Lower > Upper THEN ItemNotInTable TO TRUE
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
14 19 20 = T
ALGORITHM.
Middle = (Lower + Upper) / 2
CASE TRUE
WHEN Letter(Middle) < "Q" THEN Lower = Middle + 1
WHEN Letter(Middle) > "Q" THEN Upper = Middle -1
WHEN Letter(Middle) = "Q" THEN SET ItemFound TO TRUE
WHEN Lower > Upper THEN ItemNotInTable TO TRUE
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
14 19 16 = P
ALGORITHM.
Middle = (Lower + Upper) / 2
CASE TRUE
WHEN Letter(Middle) < "Q" THEN Lower = Middle + 1
WHEN Letter(Middle) > "Q" THEN Upper = Middle -1
WHEN Letter(Middle) = "Q" THEN SET ItemFound TO TRUE
WHEN Lower > Upper THEN ItemNotInTable TO TRUE
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
17 19 16 = P
ALGORITHM.
Middle = (Lower + Upper) / 2
CASE TRUE
WHEN Letter(Middle) < "Q" THEN Lower = Middle + 1
WHEN Letter(Middle) > "Q" THEN Upper = Middle -1
WHEN Letter(Middle) = "Q" THEN SET ItemFound TO TRUE
WHEN Lower > Upper THEN ItemNotInTable TO TRUE
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
17 19 18 = R
ALGORITHM.
Middle = (Lower + Upper) / 2
CASE TRUE
WHEN Letter(Middle) < "Q" THEN Lower = Middle + 1
WHEN Letter(Middle) > "Q" THEN Upper = Middle -1
WHEN Letter(Middle) = "Q" THEN SET ItemFound TO TRUE
WHEN Lower > Upper THEN ItemNotInTable TO TRUE
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
17 17 18 = R
ALGORITHM.
Middle = (Lower + Upper) / 2
CASE TRUE
WHEN Letter(Middle) < "Q" THEN Lower = Middle + 1
WHEN Letter(Middle) > "Q" THEN Upper = Middle -1
WHEN Letter(Middle) = "Q" THEN SET ItemFound TO TRUE
WHEN Lower > Upper THEN ItemNotInTable TO TRUE
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
17 17 18 = Q
ALGORITHM.
Middle = (Lower + Upper) / 2
CASE TRUE
WHEN Letter(Middle) < "Q" THEN Lower = Middle + 1
WHEN Letter(Middle) > "Q" THEN Upper = Middle -1
WHEN Letter(Middle) = "Q" THEN SET ItemFound TO TRUE
WHEN Lower > Upper THEN ItemNotInTable TO TRUE
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
17 17 18 = Q
ALGORITHM.
Middle = (Lower + Upper) / 2
CASE TRUE
WHEN Letter(Middle) < "Q" THEN Lower = Middle + 1
WHEN Letter(Middle) > "Q" THEN Upper = Middle -1
WHEN Letter(Middle) = "Q" THEN SET ItemFound TO TRUE
WHEN Lower > Upper THEN ItemNotInTable TO TRUE
Dynamic OCCURS
U4038 --> It is generated for all file status codes except for
00 and 10. File status code is converted into user abend
U4038 by IBM.
. U4038 has more information than file status.
S122 --> JES cancelled the job because the job is trying to
use system resource which are not authorized to use.
S322 --> Time out. Job is takes excess time to execute the
program. Then need to increase the time.