IBM-Mainframes: by Mani Kumar

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 129
At a glance
Powered by AI
The key takeaways are that COBOL was designed as a business-oriented language in 1959 and its structure is hierarchical with multiple divisions, sections, paragraphs and statements. It also discusses dynamic OCCURS clauses.

The four divisions of a COBOL program are IDENTIFICATION, ENVIRONMENT, DATA and PROCEDURE.

The ENVIRONMENT division has CONFIGURATION and INPUT-OUTPUT sections. The DATA division has FILE, WORKING-STORAGE and LINKAGE sections.

IBM-Mainframes

By Mani Kumar
Background and History

 COBOL is an acronym for:

Common Business Oriented Language


 COBOL was developed in 1959 by the Conference on Data
Systems Languages (CODASYL).
 COBOL was designed for Business Applications to solve
business problems
 COBOL was designed to be English-like language so that even a
non-programmer could understand what going-on in the program,
simply by reading the code.
 The Only drawback is Lengthy programming.
Structure of COBOL Program

 The organization of a COBOL program is hierarchical. It's


consolidated with multiple Divisions, Sections, Paragraphs as
described below.
.. Program
.. Division
.. Section
.. Paragraph
.. Sentence
.. Statement
.. Clause / Phrase / VERBS
.. Word (.. Character)
COBOL Program Organization (Division)

 We have four divisions in every COBOL program


IDENTIFICATION, ENVIRONMENT, DATA and PROCEDURE.

 IDENTIFICATION DIVISION The first division of COBOL program;


used for identification purpose. It gives the information about the
program to others who may read or use the program.
 It contains the following paragraphs.
 PROGRAM-ID is used to name the program. This is the only
paragraph that is required in the ID DIVISION. Max 8 characters.
 AUTHOR used to code the programmer's name

 DATE-WRITTEN & DATE-COMPILED used to code the date the


program was written & compiled
Division’s Contd..

 ENVIRONMENT DIVISION Provides information of the source and files


used in the program and machine-dependent Division in Program.
 Two sections: CONFIGURATION and INPUT-OUTPUT Section
 CONFIGURATION SECTION optional section is used to specify the
type of computer equipment
SOURCE-COMPUTER used to compile the program.
OBJECT-COMPUTER used to execute the program.
INPUT-OUTPUT SECTION used to specify the input/output files used in
the program.
FILE-CONTROL It contains information about the files which are using
in the program. It also contains the organization, access mode, record key
and file status of the file. It is required only if the files are used in this
program.
Division’s Contd..

 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..

 PROCEDURE DIVISION This contains the instruction to be


executed in the program
 This division consists of a series of paragraphs, each paragraph
is designed to perform a specific function.
 Paragraph Consists of a paragraph name and a series/set of
statements designed to perform a specific function.
 Sentence : A statement or series of statement ending with a
period (.)
 Statement : An instruction.
 STOP RUN : Specifies end of the program.
COBOL Layout/COBOL Code Sheet (1 - 80)

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

01 Logical-Rec PIC X(n).  File layout

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

 Hello world! Program


IDENTIFICATION DIVISION. ( ID DIVISION )
PROGRAM-ID. HELLOPGM. -  Max 8 character
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCRDURE DIVISION.
DISPLAY ‘HELLO WORLD!’.
STOP RUN.
This Program gives HELLO WORLD! As Output.
My Sample COBOL Program

The below is the one more sample 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.

* It will display the Result into output.


Variable Declaration:-

 In COBOL, the data would be handled in 2 ways.


 Data  1. Variables 2. Constants
 Variables: Variable is just a name given to address/locate the memory.
Ex: WS-VAR, A, LS-NUM, FULL-NAME…
 Constants: Value given at the time of Variable Declaration. Again it sub-
divided into 2 types.
a) Literal b) Figurative Constants
Numeric || Non Numeric Space, Spaces
18 digits || 160 characters Zero, Zeros, Zeroes
18 digits || 128 MB  new versions of COBOL
ex: 100 || 'Y'
Variable Declaration:- Conti..

 In COBOL, the below is Syntax for Variable Decleration.


 Syntax:-
 Level-no. Variable-name PIC Datatype [Length] Value Period
Sign
Usage
Justified
 Rules to code a COBOL/Variable word:-
 1. Maximum length of a COBOL word is 30 characters.
 2. It can have Alphabets, numeric, national characters and hyphen (-).
 3. It should have at least one alphabet.
Variable Declaration:- Conti..

 Level-Number:- Used to maintain/expand the structure of the record.


And there are 01-49, 66, 77, 88 existing in COBOL.
 Level numbers in the down side should have incremental level number.
 01 is used to declare record item, group item or pure elementary item.
 02 - 48 cannot be coded without 01 level number. And these can be
used for both elementary items or group items declarations.
 49 is mainly used to handle DB2 Varchar field in COBOL.
 66 is called renames clause. It is used to regroup the elementary items
into another group item.
 77 is used to declare pure elementary items.
 88 is called as Condition names condition. It is used to give
meaningful name to the value of a field.
Variable Declaration:- Conti..

 PIC clause:- It is used to specify the attributes(data-type & data-length)


of the data-item. It is mandatory on all elementary items.
 Data type:- In COBOL, there are 3 types of Data types as below.
Data type Denoted Default Max length Allowed Data
by Values allowed Characters Justificatio
n
Alphabetic A SPACES 128MB/160 A-Z Left to
Characters Right

Numeric 9 ZEROS 18 Digits 0-9 Right to


Left
Alphanume X SPACES 128MB/160 A-Z, 0-9, Left to
ric Characters Special Right
Characters
Assumed V, S Decimal,
Sign
Variable Declaration:- Conti..

 The below are the examples of Variable declaration.


01 WS-FIELD1 PIC A(01) OR A.
01 WS-FIELD2 PIC AAA.
01 WS-FIELD3 PIC A(3).
01 WS-FIELD1 PIC 9(01).
01 WS-FIELD2 PIC 9(03)V9(2).
01 WS-FIELD3 PIC 999.
01 WS-FIELD4 PIC 9(3).
01 WS-FIELD1 PIC XXX.
01 WS-FIELD3 PIC X(3).
Variable Declaration:- Conti..

 Few more examples.


01 EMP-REC.
02 EMP-GEN-DTLS.
03 EMP-NUM PIC X(4).
03 EMP-NAME.
04 FIRST-NAME PIC X(10).
04 LAST-NAME PIC X(10).
03 EMP-DOJ.
04 EMP-DOJ-CCYY PIC 9(4).
04 EMP-DOJ-MMDD PIC 9(4).
02 EMP-SALARY-DTLS.
03 EMP-SALARY PIC 9(7)V99.
03 EMP-HRA PIC 9(3)V99.
03 EMP-ALLOWANCE PIC 9(3)V99.
Value Clause:

 It is used to supply Initial or a default value at the time of Variable


Declaration.
 For A and X type the value is required in Quotes.
 01 WS-IND PIC X(1) VALUE 'N'.
 01 CITY-NAME PIC X(10) VALUE 'HYDERABAD'.
 For data type 9 the value is coded without Quotes.
 01 BONUS-PERCENT PIC 9(3) VALUE 666.
 01 BONUS-PERCENT PIC 9(3)V99 VALUE 33.5.
 01 WS-DATE VALUE '20120628'.
05 CCYY PIC 9(4).
05 MM PIC 9(4).
Justified Clause:- Used for A, X only

 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.

01 A PIC X(4). 01 B PIC X(4) JUSTIFIED


RIGHT.
MOVE 'ABC' TO A MOVE 'ABC' TO B
Output: ABC_ Output: _ABC
MOVE 'ABCDE' TO A MOVE 'ABCDE' TO B
Output: ABCD Output: BCDE
Usage Clause:- Used only for 9

 It is used on numeric items to store the numbers in different internal


formats(Zoned Decimal, Binary & Packed Decimal)
 Types of Usage clause
 1. Usage Display  default, 1 digit in 1 byte(Zoned Decimal)
 2. Usage Comp  0, 1(Binary format)
 3. Usage Comp-1
 4. Usage Comp-2
 5. Usage Comp-3  2 digits in 1 byte(Packed Decimal)
Syntax to Declare:-
 01 ws-count pic 9(4) usage display.
 01 ws-count pic 9(4) usage COMP.
 01 ws-total pic 9(7)v99 usage COMP-3.
Terms used for memory

 Bit : single bit. possible value is 0 or 1


 Nibble :-4 Bits is one nibble.
 In packed decimal, each nibble stores one digit.
 Byte :-8 Bits is one byte.
 By default, every character is stored in one byte.
 Half word :-16 bits or 2 bytes is one half word.
 Full word :-32 bits or 4 bytes is one full word.
 Double word :-64 bits or 8 bytes is one double word.
Difference B/w COMP & COMP-3
COMP COMP-3
The Data representation is Binary The Data representation is Packed
format Decimal
The memory allocation depends on The memory allocation depends on
the memory boundaries as shown the length of the data(Even/Odd)
below For even:- (N/2)+1
S9(01) – S9(04)  2 bytes Ex: 9(4)  (4/2)+1 = 3 bytes
S9(05) – S9(09)  4 bytes For odd:- (N+1)/2
S9(10) – S9(18)  8 bytes Ex: 9(9)  (9+1)/2 = 5 bytes
Sign will be stored in lest most Sign will be stored in the 2nd nibble
significant bit of right most significant byte
Data will be stored in 0’s & 1’s 2 digits would be stored in 1 byte
Allowed clauses are S & 9 only Allowed clauses are S, V & 9
Positive  0 Positive  C
Negative  1 Negative  D
Unsigned  F
Sign Clause:- Used only for 9

 Used to store the numeric value along with Sign(+/-) in memory.


 It is applicable when the picture clause contains 'S'.
 Where the sign is stored in the numeric items?
In Zoned decimal by default it is stored in the first nibble of the last
byte. In Packed decimal it is stored in the 2nd nibble of the right most
byte. Whereas in Binary items it is stored in the left most significant bit.
 In all the cases it is not going occupy any extra byte. So the length is not
going to be changed if sign is used. When it is required show in the
report then it has to be converted into edited numeric.
 Syntax:
 SIGN IS TRAILING/LEADING (SEPARATE CHARACTER).
 Default is TRAILING (WITH NO SEPARATE CHARACTER).
So 'S' doesn't take any extra space to store the sign.
Numeric Editing Characters: Used for only 9

 As we know the numeric data will be stored in different formats like


Compressed, assumed decimal and assumed sign which are not
understandable but saves the memory.
 But if we need to generate any reports then the data should be in
readable format for that reason we have to edit the data by using the
Editing characters. Each Editing character occupies 1 byte of memory.
 Editing characters are Z, +, -, /, (,), (.), CR, DB, $….
 Below are the situations we go for Editing characters.
 Suppress leading Zeros
 To Display sign, decimal separately
 To insert ,(comma), .(period) for values and amounts
 To insert /(slashes) for dates
Numeric Editing Characters: Used for only 9
Accept:- Same as like ScanF in C language

 It is used to Accept the data from JCL instream (SYSIN DD *) to the


program variables declared in Working-Storage Section.
 Syntax: ACCEPT VAR-NAME.

In program In JCL
WORKING-STORAGE SEC.

//STEP1 EXEC PGM=PGM1


01 WS-GROUP.
//SYSOUT DD SYSOUT =*
05 WS-COUNTRY PIC X(3).
//SYSIN DD *
05 WS-DATE PIC 9(8).
IND20120305

PROCEDURE DIVISION. OR
IND
DISPLAY: Same as like PrintF in C Language

 It is used to Display the data(statements that Coded in COBOL) into the


Output(Screen).
 Syntax: DISPLAY Literal/Variable.
 EX:
DISPLAY ‘Hello World‘.
DISPLAY ‘the entered details are : ‘ WS-GROUP.

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.

MULTIPLY 10 BY A GIVING RESULT.


COMPUTE:-

 Arithmetic operations can also be done by using the statement


COMPUTE.
 Syntax: COMPUTE Var3 = Var1 (+ or – or * or /) Var2
 EX:
COMPUTE C = A + B
COMPUTE C = B – A
COMPUTE C = A * B
COMPUTE C = B / A
COMPUTE C = A + 6
MOVE

 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

 Elementary move with reference modification: It moves partial data


into receiving field. Other part of the receiving field is unchanged.
 Ex: 01 IN-REC PIC X(10) VALUE ‘HYD@123456’.
01 OUT-REC PIC X(20) VALUE ‘SECUNDERABAD@111’.
MOVE IN-REC(1:6) TO OUT-REC(14:6)
 Here first 6 chars of in-rec are moved at 14 position and replaces 6 chars
of out-rec and the first 13 chars of out-rec are unchanged.
 Group move: It is same as like elementary move but it moves the data
from 1 group to another group.
 Ex: 01 WS-DATE1.
05 WS-DATE-CCYY PIC X(4).
01 WS-DATE2.
05 WS-DATE-CCYY PIC 9(4).
MOVE WS-DATE1 TO WS-DATE2.
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.

 MOVE CORR IN-REC TO DETAIL-REC.

 Total-salary is not having a matching name in the sending structure


then it is untouched in the receiving structure
Conditional Statements

 IF and Evaluate are conditional Statements. These are used to execute


the statements conditionally(by checking the condition).
 General Syntax of IF statement:
IF CONDITION
STATEMENT1/CONTINUE/NEXT SENTENCE
ELSE
STATEMENT2
END-IF
 Continue/Next Sentence:
 Both are used when IF is empty. Without statement in the IF, ELSE
cannot be coded so with that reason we can use above 2 statements.
 To fulfill the empty IF statement, Continue or next sentence are used.
Continue executes next logical statement after END(Explicit Scope
Terminator) where Next sentence executes statement after period(.
Implicit Scope Terminator).
Types of Conditions

Relation condition Sign condition

=, >, <, >=, <=, NOT = Is Positive


Is Negative
Is zeros

It is used to check the sign of


a data item. it is used only on numeric items.

Syntax: IF Var is POSITIVE


IF Var is NEGATIVE IF Var is ZEROES
Types of Conditions

Class condition Condition name


Is Numeric 88 Level(condition names)
Is Alphabetic
Is Alphanumeric It is used to give a
meaningful name to the
It is used to check the values of a field.
content of data item against It increases business
pre-defined range of values. meaning for a field value.
Ex: (OR)
IF Var IS NUMERIC It is used to check the
IF Var IS ALPHABETIC variable with
IF Var IS ALPHANUMERIC multiple conditions. That
means we can assign multple
values to a variable.
Types of Conditions
01 InputChar PIC X.
88 Vowel VALUE "A","E","I","O","U".
88 Consonant VALUE "B" THRU "D", "F","G","H"
"J" THRU "N", "P" THRU "T"
"V" THRU "Z".
88 Digit VALUE "0" THRU "9".
88 LowerCase VALUE "a" THRU "z".
88 ValidChar VALUE "A" THRU "Z","0" THRU "9".
IF ValidChar
DISPLAY "Input OK." Input Char
END-IF
IF LowerCase E
DISPLAY "Not Upper Case" Vowel TRUE
END-IF Consonant FALSE
IF Vowel Digit FALSE
Display "Vowel entered." LowerCaseFALSE
END-IF ValidChar TRUE
METHOD1 METHOD2
IF condition1 IF condition1
Types of Conditions
statement1 statement1
ELSE ELSE
IF condition2 IF condition2
statement2 statement2
ELSE ELSE
IF condition3 IF condition3
statement3 statement3
ELSE ELSE
statement4 statement4.
END-IF

END-IF

END-IF

It is recommended to code the equivalent END-


Ifs for all IF conditions.
The IF, ELSE and its corresponding END-IF
EVALUATE TRUE
WHEN condition
statement
Types of Conditions
END-EVALUATE
Method1 Method2
------- -------
EVALUATE Varaible EVALUATE TRUE

WHEN value1 WHEN condition1


Statement1 Statement1
WHEN value2 WHEN condition1
Statement2 Statement2
WHEN value3 WHEN condition1
Statement3 Statement3
WHEN OTHER END-EVALUATE
Statement4

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

1. Allocation(In Environment Division we have to allocate the


Logical file name to the physical file of JCL.
Here by using the SELECT clause we can assign the logical
filename to the physical file. And also we can specify the
Organization, Access mode, Record Key(VSAM) and File-
status to the file.
Note: Organization and Access mode are optional for sequential
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

2. Definition & Declaration(In Data Division we have to


define the attributes and declare the logical file layout.
Here, we can define the attributes(Recording mode, Record
contains, Block contains parameters list) of the file.
Note: File attributes are not mandatory but in JCL we must
mention all the attributes of the file and those should be
compatible with the file structure in the program.
Syntax:
DATA DIVISION.
FILE SECTION.
FD TRXN-FILE
RECORDING MODE IS F
RECORD CONTAINS 80 CHARACTERS
BLOCK CONTAINS 800 CHARACTERS.
01 EMP-REC PIC X(80).
FILES

3. Open(In Procedure Division we have to Open the file before


performing Read(Input)-Write(Output) operations).
Syntax:
OPEN mode Logical filename.
In 4 different modes we can open the files in COBOL as below.

Open mode  Purpose


Input  to Read the data from the file
Output  to Write the data into file
Extend  to Append the new data to old data
I-O  to Rewrite the old data in the file

In most real time applications, sequential files are mostly used to


read and write the data.
It is not recommended to rewrite(update) the data.
FILES

 4. I-O Operations(In Procedure Division after opening the


file we can perform Read, Write, Delete, Update operations).
Read Statement: It reads the physical records of the file
and maps to the logical file layout that mentioned in Data
Division.
Each Read statement reads only 1 record from physical file to
logical record.
Syntax: Read Logical-file

Write Statement: Write statement writes records into a


sequential file in a sequential order.
To WRITE data to a file move the data into the logical
record layout(declared in the FD entry) and then WRITE
the content into the file.
Syntax: Write Logical-record
How the READ works

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

PERFORM UNTIL file-status = ‘10’


READ StudentRecords
END-PERFORM.
How the WRITE works

OPEN OUTPUT StudentFile.


MOVE "9334567Frank Curtain LM051" TO StudentDetails.
WRITE StudentDetails.
MOVE "9383715Thomas Healy LM068" TO StudentDetails.
WRITE StudentDetails.
CLOSE StudentFile.
STOP RUN.

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

5. Close(In Procedure Division after performing all the


functions we should close the files before ending up the program.

Relation between Logical file and Physical File

1. DDNAME of COBOL and DDNAME of JCL should be same


2. File organization of logical file in Select statement and
organization of physical file should be same
3. File attributes of FD entry in COBOL and DCB parameters of
JCL should be same RECFM, LRECL, BLKSIZE
4. OPEN statement in COBOL and DISP parameter in JCL
should be compatible
The compatible DISP values for Sequential open modes are
shown in below.
FILES

OPEN in COBOL VS DISP in JCL on Sequential files

INPUT  SHR  File should be existing and other can read


it simultaneously
OLD  File should be existing and other jobs
cannot read it

OUPUT  NEW  File should not be existing, It will create


MOD  File may or may not be existing
If file not existing then it will create
If file is existing then it will append data
SHR  File should be existing. Deletes existing
data and writes new data
OLD  File should be existing. Deletes existing
data and writes new data
FILES

OPEN in COBOL VS DISP in JCL on Sequential files

EXTEND  New  File should not be existing


MOD  File may or may not existing
If file not existing then it will create
If file is existing then it will append data

SHR  File should be existing. It appends data.


OLD  File should be existing. It appends data.

I-O  SHR  File should be existing and records can


be updated
OLD  File should be existing and records can
be updated
File Status Codes

'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

 Initialize is used to initialize the variables to its initial values in the


procedure division.
 VALUE clause is used to initialize the data items in the declaration in
Data Division but Initialize will use in Procedure Division.

Syntax: INITIALIZE IDENTIFIER-1/VARIABLE

 INITIALIZE sets the alphabetic, alphanumeric to SPACES and


numeric items to ZEROES. INITIALIZE statement doesn’t shows any
affect on FILLER.

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.

In OUTPUT  BEFORE INITIALIZE : 2020/09/24


AFTER INITIALIZE : 0000/00/00
FILLER

FILLER is just some Empty Volume that we could use for


future purpose.
In real time applications, Filler is used in 3 different situations.
 1. If we don’t want to modify the constant which is declared
under a structure.
Ex: Headers and Trailers in reports.

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

2. When a spacing is required in between the fields of a file for


better readability.
Ex: 01 OUT-RECORD.
05 AGENT-NUM PIC X(5).
05 FILLER PIC X(2).
05 AGENT-NAME PIC X(20).
05 FILLER PIC X(2).
05 AGENT-DOJ PIC X(8).
05 FILLER PIC X(2).
05 AGENT-DOB PIC X(8).

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

Redefines is the concept of reusing the existing memory of one


variable/structure to another variable/structure.
Redefines is a facility where we can point out two or more
data-items to the same memory location.
In real time, we use Redefines in 3 different scenarios.
1. To see the same variable with different sub fields.
01 WS-DATE PIC X(8) VALUE ‘20200915’.
01 WS-DATE1 REDEFINES WS-DATE.
05 CCYY PIC 9(4).
05 MM PIC 9(2).
05 DD PIC 9(2).

A. Redefined item should follow original item.


B. Redefining should be done on the same level.
C. Same item can be redefined more than one time.
REDEFINES

2. If you want to use Alphanumeric items in arithmetic operations


then redefine alphanumeric item into numeric item and use the
numeric item in the arithmetic operations
Ex: 01 WS-DATE PIC X(8) VALUE ‘20200915’.
01 WS-DATE1 REDEFINES WS-DATE PIC 9(8).

Now WS-DATE1 can be used in the arithmetic operations.


COMPUTE NEW-DATE = 99999999 – WS-DATE1
ADD +1 TO WS-DATE1
REDEFINES

3. On mutually exclusive conditions. In this case the memory can


be saved.
Ex:
Lets say Agent details file has two different layouts.
1. With Individual Agent details.
2. With Company details in the file.
But at a time we will get only 1 type of details(either Individual
Agent or Company).
An Individual have First-Name and Last-Name but Company will
have a company name.
For the above case we can code the layout without Redefine and
with Redefines as shown in below slides.
And will see how the memory could be saved by using Redefines.
REDEFINES

In layout1, there is a separate memory for both INDIVIDUAL-


AGENT-NAME and COMPANY-NAME. Either of these fields will
have spaces in every record. So memory is wasted in every
record.
01 AGENT-REC.
05 AGENT-ID PIC X(05).
05 INDIVIDUAL-AGENT-NAME.
10 FIRST-NAME PIC X(20).
10 LAST-NAME PIC X(10).
05 COMPANY-NAME PIC X(30).
05 AGENT-TYPE PIC X(1).
88 INDIVIDUAL-AGENT VALUE 'I'.
88 CORPORATE-AGENT VALUE 'C'.
05 DOJ PIC X(8).
05 LAST-TRX-DATE PIC X(8).

AAA01STEPHEN FLEMING I2008010220101031


AAC05MICHEAL JOHN I2006010220111031
AAC07 BERTRAM FINANCIAL CO C2009010320111031
AYM05 PROCTER & GAMBLE C2007010520191010
REDEFINES

In layout2, the same memory will be utilized for both the


INDIVIDUAL-AGENT-NAME and COMPANY-NAME as shown
below.
The memory will be saved in this technique.
01 IN-REC.
05 AGENT-ID PIC X(05).
05 INDIVIDUAL-AGENT-NAME.
10 FIRST-NAME PIC X(20).
10 LAST-NAME PIC X(10).
05 COMPANY-NAME REDEFINES INDIVIDUAL-AGENT-NAME PIC X(30).
05 IN-AGENT-TYPE PIC X(1).
88 INDIVIDUAL-AGENT VALUE 'I'.
88 CORPORATE-AGENT VALUE 'C'.
05 DOJ PIC X(8).
05 LAST-TRX-DATE PIC X(8).

AAA01STEPHEN FLEMING I2008010220111031


AAC05MICHEAL JOHN I2006010220111030
AAC07BERTRAM FINANCIAL CO C2009010320110031
AYM05PROCTER & GAMBLE C2007010520111029
STRING MANIPULATION

String is a collection of characters. In String manipulation we


will do the following operations.

1. Concatenating multiple strings into single string and splitting


single into multiple strings by using the statements STRING
& UNSTRING correspondingly.

2. Finding or Counting for a particular character/sub-string in a


String by using INSPECT & TALLYING statements.

3. Replacing or Converting particular character/sub-string in a


String with another set of characters by using REPLACING &
CONVERTING statements. These two statements would be
used along with INSPECT statement.
How the STRING Works

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:

STRING identifier-1 DELIMITED BY identifier-2/literal/SIZE


…….INTO identifier-n
[ON OVERFLOW Imperative statement ]
[WITH POINTER pointer-variable]
01 DayStr PIC XX. 5
01 MonthStr PIC X(9). J U N E-----
01 YearStr PIC X(10). 1 9 9 4------
01 DateStr PIC X(15) VALUE ALL "-".
5 , J U N E , 1 9 9 4 - - - -

STRING DayStr DELIMITED BY SPACES


", " DELIMITED BY SIZE
MonthStr DELIMITED BY SPACES
", " DELIMITED BY SIZE
YearStr DELIMITED BY SIZE
INTO DateStr
END-STRING.
01 LAST-NAME PIC X(10) VALUE 'EDISON'.
01 MIDDLE-NAME PIC X(10) VALUE 'ALVA'.
01 FIRST-NAME PIC X(10) VALUE 'THOMAS'.
01 NAME-OUT PIC X(40) VALUE SPACES.
01 PTR PIC 9(1) VALUE ZERO.

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.

OUTPUT: ‘----THOMAS ALVA EDISON -----'


How the UNSTRING works

 The UNSTRING copies the data from the Source String to


the Destination String until a Delimiter is encountered in the
Source String or the Destination String becomes full.
Characters are copied from the Source String to the Destination
Strings according to the rules of Alphanumeric moves.
The [WITH POINTER identifier] and [ON OVERFLOW
imperative-statement] clauses may be used in the same as like
we use for STRING.
Syntax :
UNSTRING IDENTIFIER-1 DELIMITED BY
IDENTIFIER/LITERAL INTO IDENTIFIER-3…IDENTIFIER-n
[ ON OVERFLOW IMPERETIVE STATEMEN]
[TALLYING IN Counter ]
[ WITH POINTER Pointer-variable]
How the UNSTRING works

01 DayStr PIC XX. 1 9


01 MonthStr PIC XX. 0 5
01 YearStr PIC XX. 8 0
01 DateStr PIC X(8).

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

01 FULL-NAME PIC X(30) VALUE 'THOMAS ALVA EDISON'


01 FIRST-NAME PIC X(10).
01 MIDDLE-NAME PIC X(10).
01 LAST-NAME PIC X(10).
Ex: UNSTRING FULL-NAME DELIMITED BY ALL SPACES
INTO FIRST-NAME, MIDDLE-NAME, LAST-NAME
TALLYING IN WS-NAMES-RECEIVED
OUTPUT: FIRST-NAME = 'THOMAS '
MIDDLE-NAME = 'ALVA '
LAST-NAME = 'EDISON '
WS-NAMES-RECEIVED = 3
INSPECT

 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.

 INSPECT- FOR TALLYING


It is used to count the occurrence of a single character or sub-string in
string.
Syntax:
INSPECT IDENTIFIER-1 TALLYING IDENTIFIER-2 FOR
{ALL/LEADING IDENTIFIER-3/CHARACTERS [BEFORE/AFTER
INITIAL IDENTIFIER-4/CHARACTER]}

Ex: ZIP-CODE - '132-330-1450'

INSPECT ZIP-CODE TALLYING WS-COUNT FOR ALL '-'.


RESULT: VALUE IN WS-COUNT IS 2.
INSPECT

 INSPECT- FOR REPLACING


REPLACING option changes a particular character or sub-string with
another character or sub-string in a String.
Syntax:
INSPECT IDENTIFIER-1 REPLACING
{ALL|LEADING CHARACTERS/IDENTIFIER-2 BY
IDENTIFIER-3/CHARACTERS [BEFORE/AFTER INITIAL IDENTIFIER-
4/CHARACTER]}...

Ex: EMAIL-ID 'JOHN.JOHNSON@CNO.COM'

INSPECT EMAIL-ID REPLACING '.COM ' BY '.CO.IN' AFTER 'CNO'

OUTPUT: 'JOHN.JOHNSON@CNO.CO.IN'
INSPECT

 INSPECT- FOR CONVERTING


CONVERTING Option replaces a characters of the FROM set with its
corresponding characters in the TO set.
Syntax:
INSPECT IDENTIFIER-1 CONVERTING
{CHARACTERS/IDENTIFIER-2} TO {CHARACTERS/IDENTIFIER-3}...
[BEFORE|AFTER INITIAL IDENTIFIER-4|CHARACTER]

In the below example small case is converted to upper case.

Ex: CUSTADDRESS: “abcdefghijklmnopqrstuvwxyz”

INSPECT CUSTADDRESS CONVERTING "abcdefghijklmnopqrstuvwxyz"


TO "ABCDEFGHIJKLMNOPQRSTUVWXYZ".

Output: CUSTADDRESS: “ABCDEFGHIJKLMNOPQRSTUVWXYZ”


SUBPROGRAM

 Sub-program is a program that would be executed from


another main program by using CALL statement.
 Sub-program is always executed from main program.
When a specific functionality need to be performed in more
than one program then will put that functionality in a sub-
program and call it from any program by using the CALL
statement. Sub-program is not self executable.
The Main program that calls the sub-program is referred as
Calling program. The sub-program that is linked and executed
to main-program is referred as Called program.
 Syntax:
CALL SUB-VAR by using WS-VARs  Dynamic Call
or
CALL ‘SUB-PGM’ by using WS-VARs  Static Call
SUBPROGRAM

 Steps to execute the sub-program from a Main-program


1. Write a subprogram & Compile it
2. Write main-program with a CALL statement to call sub-
program and compile main-program.
Supply Subprogram loadlib in the compilation of main-
program if it is statically called.
Supply the sub-program loadlib in the JOBLIB if the sub-
program is called dynamic.
3. Execute Main-program.

Number of variables, data type, length and position of


sending and receiving variables should be same. And names
could be different.
Example of CALL Called programs

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

4 Control returns to calling program


STATIC & DYNAMIC CALLS

•Static Calls

CALL 'PGMNAME' USING arguments

•Dynamic Calls

01 WS-SUBPGM PIC X(8) VALUE 'PGMNAME1'.

CALL WS-SUBPGM USING arguments


STATIC & 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.

2. Any change to sub program, all the programs which are


calling this sub program needs to be re-compiled.
3. Static calls are faster as compared to dynamic calls. As the
load modules are part of the main load.
4. Static calls uses more memory compared to Dynamic calls.
STATIC & DYNAMIC CALLS

DYNAMIC CALLS

1. Load module of sub program will be loaded to main memory


during Execution time.

2. Any change to sub program, Only the sub program needs to


be re-compiled.
3. Dynamic calls are slower compared to static calls. As the
load modules to be searched & loaded to the main memory.
4. Dynamic calls uses less memory compared to Static calls .
 Master files are used in multiple programs so their
record layout is common for all those programs. So their
layouts can be placed in one COPYBOOK and can place
them in any program wherever we use those files.
 By doing like this we can maintain the program
standards because all the programs share the same layout
and same data-names.

 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).

02 FeesOwed PIC 9(4).


PROCEDURE DIVISION.
BeginProg. 02 AmountPaid PIC 9(4)V99.
OPEN INPUT StudentFile
READ StudentFile
AT END SET EndOfSF TO TRUE
END-READ
PERFORM UNTIL EndOfSF
DISPLAY StudentNumber SPACE StudentName
END-PERFORM
Occurs/Arrays/Table handling

 When a set variables are repeated in a continuous memory


location on a same purpose then occurs can be used to declare
the structure of variables.
 It is similar to Arrays in other languages.
 General Syntax:
(level-number 02-49) identifier OCCURS N TIMES
{ASCENDING/DESCENDING KEY IS data-name-1}
{INDEXED BY index-name-1}
Ex:
02 STD-DLTS OCCURS N TIMES
OR
02 STD-DLS OCCURS N TIMES INDEXED BY INDX
OR
02 STD-DLTS OCCURS N TIMES ASCENDING KEY IS STD-ID
INDEXED BY INDX
Occurs/Arrays/Table handling

01 is used to declare the record level and Occurs cannot be


declared at 01 level because it is to repeat fields with in the
record not to repeat the record itself.
Method1: Declaring without Occurs Method2: Declaring with Occurs
----------------------------------- --------------------------------
05 prod-details1.
10 prod-code1 pic x(4) 05 prod-details OCCURS 10 TIMES.
10 prod-price1 pic s9(5)v99. 10 prod-code PIC X(4).
10 prod-price pic S9(5)V99.
05 prod-details2.
10 prod-code2 pic x(4) Both Method1 and Method2
10 prod-price2 pic s9(5)v99. occupies the same memory.
.. Occurs is used to reduce
05 prod-details10.
10 prod-code10 pic x(4) number of variable but not to
10 prod-price10 pic s9(5)v99. reduce memory.
How to Load and Display the elements of table

 To handle Occurs we will use INLINE Perform.


To load the data into the table

PERFORM VARYING ws-ctr FROM 1 BY 1 UNTIL ws-ctr > 10


ACCEPT prod-details(ws-ctr)
or
MOVE details to prod-details(ws-ctr)
END-PERFORM

To display the data into the table

PERFORM VARYING ws-ctr FROM 1 BY 1 UNTIL ws-ctr > 10


DISPLAY prod-details(ws-ctr)
END-PERFORM
How to search an element Manually/Automatically

 MOVE 'N' TO STOP-LOOP


PERFORM VARYING WS-COUNT FROM 1 BY 1 UNTIL
WS-COUNT > 10 OR STOP- LOOP = 'Y'
IF IN-PCODE = prod-code(ws-count)
MOVE prod-price(ws-count) TO WS-PRICE
MOVE 'Y' TO STOP-LOOP
END-IF
END-PERFORM

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.

SEARCH ALL Letter


AT END DISPLAY “Letter not found!”
WHEN Letter(LetterIdx) = LetterIn
DISPLAY LetterIn, "is in position ", Idx
END-SEARCH.
How the Search All works.

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

Lower Upper Middle Letter(Middle)

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

Lower Upper Middle Letter(Middle)

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

Lower Upper Middle Letter(Middle)

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

Lower Upper Middle Letter(Middle)

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

Lower Upper Middle Letter(Middle)

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

Lower Upper Middle Letter(Middle)

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

Lower Upper Middle Letter(Middle)

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

Lower Upper Middle Letter(Middle)

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

Lower Upper Middle Letter(Middle)

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

Lower Upper Middle Letter(Middle)

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

Dynamic array is also an array whose size can be decided


with the value of another field at the time of program
execution dynamically.
How to declare dynamic occurs
01 EMP-REC.
05 EMP-NUM PIC X(4).
05 EMP-NAME PIC X(20).
05 EMP-DOJ PIC X(8).
05 EMP-CHILD-STATUS PIC X(1).
05 EMP-CHILDREN-NUM PIC 9(1).
05 EMP-CHILDREN-INFO OCCURS 0 TO 5
TIMES DEPENDING ON EMP-CHILDLREN-NUM.
10 CHILD-NAME PIC X(20).
10 CHILD-AGE PIC 9(2).
10 CHILD-DOB PIC X(8).
Dynamic OCCURS

 Here number of EMP-CHILDREN-INFO buckets are created


based on the value on the field EMP-CHILDLREN-NUM.
This record becomes a variable length record because each
record length varies its size basing on the number of children.

Length of the record = 4 bytes used for variable length +


Fixed record area +
value in depending field *
record Size in the occurs

Minimum length of the record = 4 + 34 + (0 * 30) = 38


Maximum length of the record = 4 + 34 + (5 * 30) = 188
ABENDS

S0C1 --> DDname missing or misspelled.


Need to cross-check the DDname of COBOL against
with JCL and correct it.

 S0C4 --> Below are the few situations to get S0C4.


. Program is compiled with SSRANGE and Using an
occurs beyond its maximum occurrence.
. Number of passing variables in the main program and
receiving variables in subprograms are not matching.
. Trying to open an empty VSAM file either in input or
I-O mode. File should not be empty.
. Trying to execute subprogram by using PGM=subpgm.
. Organization mismatch between logical file in COBOL and
Physical file in JCL.
ABENDS

 S0C7 --> Common situations to get S0C7.


. Using non-numeric variables in arithmetic operation.
. Using editing numeric variables in arithmetic
operations.
. Trying to move non-numeric data to COMP/COMP-3.

 S0CB --> Dividing by zero. If you divide a variable by zero


then it will through syntactical error.
Compute A = B / 0 -- This causes syntactical error.

 S804 --> Insufficient region to execute program. i.e. Less


memory coded in Region parameter of Job card.

 S806 --> Load module is not found in joblib or steplib.


ABENDS

 S013 --> Member not found.


. Compile JCL is submitted but the program not found
in source library.
. Control card given in runjcl not found.

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.

SB37 --> Primary and secondary spaces are coded but


space is not sufficient.

 SD37 --> Primary space is coded but secondary space is not


coded and coded primary space is not sufficient.
ABENDS

 SE37 --> Space abend on PDS.

 S122 --> JES cancelled the job because the job is trying to
use system resource which are not authorized to use.

 S222 --> Job is cancelled by user or operator.

S322 --> Time out. Job is takes excess time to execute the
program. Then need to increase the time.

S522 --> Job max execution time is exceeded.

You might also like