cobol tutorials-mainframe230.blogspot.com full

97
www.mainframe230.blogspot.com COBOL Training Material

Upload: rangaramakrishna

Post on 28-Nov-2014

331 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Cobol Tutorials-Mainframe230.Blogspot.com Full

www.mainframe230.blogspot.com

COBOL

Training Material

Page 2: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

Table of Contents

1. OBJECTIVES 6

2. INTRODUCTION TO COBOL 72.1. LANGUAGE CONSTRUCTION 72.2. COBOL CHARACTER SET 82.3. COBOL WORDS 82.4. COBOL CODING FORM 82.5. DIVISIONS OF COBOL 8

3. IDENTIFICATION DIVISION 93.1. SYNTAX OF IDENTIFICATION DIVISION 9

4. ENVIRONMENT DIVISION 104.1. CONFIGURATION SECTION 10

4.1.1. SOURCE-COMPUTER 104.1.2. OBJECT COMPUTER 104.1.3. SPECIAL-NAMES 10

4.2. INPUT-OUTPUT SECTION 114.2.1. FILE-CONTROL 114.2.2. I-O CONTROL 11

5. DATA DIVISION 125.1. LITERALS AND FIGURATIVE CONSTANTS 13

5.1.1. NUMERIC LITERAL 135.1.2. NON-NUMERIC LITERAL 135.1.3. LIST OF FIGURATIVE CONSTANTS 14

5.2. Data Description 145.2.1. LEVEL NUMBERS 145.2.2. Data Names 155.2.3. CLAUSE 165.2.4. CONDITION NAMES 21

6. PROCEDURE DIVISION 226.1. STATEMENTS IN PROCEDURE DIVISION 236.2. Data Movement 23

6.2.1. MOVE 236.3. ARITHMETIC VERBS 25

6.3.1. ADD 256.3.2. SUBTRACT 266.3.3. MULTIPLY 266.3.4. DIVIDE 266.3.5. ROUNDED OPTION 266.3.6. ON SIZE ERROR 276.3.7. COMPUTE 276.3.8. ARITHMETIC OPERATORS 27

6.4. Miscellaneous 276.4.1. ACCEPT 27

www.mainframe230.blogspot.com Page 2 date:16.05.2010

Page 3: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

6.4.2. DISPLAY 286.4.3. CONTINUE 286.4.4. STOP RUN 286.4.5. EXIT 286.4.6. INITIALIZE 28

6.5. CONDITIONAL AND UNCONDITIONAL TRANSFER 296.5.1. SIMPLE IF STATEMENT 296.5.2. NESTED IF 296.5.3. EVALUATE STATEMENT 306.5.4. GO TO STATEMENT 316.5.5. GO TO WITH DEPENDING 316.5.6. ALTER 31

6.6. PERFORM STATEMENTS 326.6.1. Basic Perform 326.6.2. PERFORM with TIMES phrase 326.6.3. PERFORM with UNTIL phrase 326.6.4. PERFORM with VARYING phrase 33

6.7. Conditional expressions 336.7.1. RELATIONAL CONDITION 336.7.2. SIGN CONDITION 346.7.3. CLASS CONDITION 346.7.4. CONDITION-NAME CONDITION 346.7.5. NEGATED SIMPLE CONDITION 346.7.6. COMPOUND CONDITION 35

7. FILE HANDLING 367.1. Sequential Files 367.2. Indexed Files 367.3. Direct Access Files 367.4.STEPS INVOLVED IN FILE HANDLING 36

7.4.1. EXAMPLE 377.5. SELECT Statement 38

7.5.1. ORGANIZATION CLAUSE 387.5.2. ACCESS MODE CLAUSE 387.5.3. RECORD KEY PHRASE 397.5.4. ALTERNATE KEY PHRASE 39

7.6. FD entry 397.6.1. BLOCK CONTAINS / RECORD CONTAINS PHRASE 397.6.2. RECORDING MODE CLAUSE 39

7.7. INPUT / OUTPUT STATEMENTS 407.8. VSAM DATA SET 41

7.8.1. VSAM DATASETS AND ACCESS MODES 427.8.2. ENVIRONMENT DIVISION ENTRIES FOR VSAM FILES 427.8.3. COBOL STATEMENTS USABLE WITH SEQUENTIAL FILES 437.8.4. COBOL STATEMENTS WITH INDEXED FILES (KSDS) AND RELATIVE (RRDS) 447.8.5. PROCEDURE DIVISION STATEMENTS TO UPDATE VSAM FILES 457.8.6. ERROR PROCESSING FOR VSAM 467.8.7. APPLICATIONS BEST SUITED TO VSAM DATASETS 46

8. SUBPROGRAMS 478.1. LINKAGE SECTION 48

8.1.1. STATIC / DYNAMIC CALLS 488.1.2. PASSING RETURN CODE INFORMATION 49

www.mainframe230.blogspot.com Page 3 date:16.05.2010

Page 4: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

9. STRING, UNSTRING AND INSPECT STATEMENTS 509.1. STRING STATEMENT 509.2. UNSTRING STATEMENT 519.3. INSPECT STATEMENT 529.4. DECLARATIVES 53

10. SORT AND MERGE STATEMENTS 54

11. INTRINSIC FUNCTIONS 5611.1. Number Handling 5611.2. Date / Time 5611.3. Finance 5611.4. Mathematics & Statistics 57

12. COMPILER OPTIONS 58

13. APPENDIX 6013.1. ERROR CODES 6013.2. EXERCISES 61

13.2.1. BASIC PROBLEM SOLVING 6113.2.2. REPORT PREPARATION 6213.2.3. ARRAY HANDLING 6213.2.4. FILE HANDLING 62

13.3. JCL 63

www.mainframe230.blogspot.com Page 4 date:16.05.2010

Page 5: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

1. OBJECTIVES

The main objective after the completion of this course, is that the students should be able to:

Understand the standard format of the COBOL program.

Write COBOL programs which uses VSAM or Non VSAM files.

Submit jobs to Compile and Link-edit COBOL source codes.

Write programs using modular design techniques.

www.mainframe230.blogspot.com Page 5 date:16.05.2010

Page 6: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

2. INTRODUCTION TO COBOL

COBOL => COMMON BUSINESS ORIENTED LANGUAGE

Cobol can be referred to as:

High-Level Programming language oriented towards business applications.

A procedure oriented language. A Procedure is a module having its own functionality. A Problem may be segmented into multiple sub-functionalities. Each sub-functionality is written as a Paragraph in Procedure Division and executed in a logical sequence as mentioned.

Problem Oriented and Machine independent

History of COBOL Developed in 1959 as standard language to meet needs of business

Committee to develop language convened by Department of Defense

Included representatives from academia, business, computer manufacturers

Standard Versions of COBOL

1960s - wide variations in COBOL compilers

1968 - first COBOL standard set by American National Standards Institute (ANSI)

1974 - second ANSI standard to make COBOL more efficient, standardized

1985 - this ANSI standard incorporated structured programming techniques

COBOL - Some Characteristics

COBOL is self-documenting

COBOL is simple

COBOL is non-proprietary (portable)

COBOL is Maintainable2.1. LANGUAGE CONSTRUCTIONCOBOL is like an English language.

Character is the lowest component.

Word is made up of one or more characters.

Clause consists of word and characters. It specifies an attribute for an entry

Statement is syntactically valid combination of words and clauses

Sentence is a sequence of one or more statements terminated by period

Paragraph consists of one or more sentences

Section consists of one or more paragraphs

Division consists of one or more paragraphs or sections

Program is made up of divisions

www.mainframe230.blogspot.com Page 6 date:16.05.2010

Page 7: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

2.2. COBOL CHARACTER SETSet of 51 characters

0,..9 DIGITSA,..Z LETTERSb SPACE/BLANK CHARACTER+,-,*,/,(,),= SPECIAL SYMBOLS$,;,“,>,<,.

2.3. COBOL WORDS

User defined words

Words declared by programmer to coin identifiers, paragraph names, file names and section names.

Reserved words

Words that are having specific meaning to the compiler.

2.4. COBOL CODING FORMColumns

1- 6 Sequence numbers identifying pages or lines of a program

7 Continuation, comment or starting of a new page

8 - 72 COBOL program statements

73-80 Remarks

Column 7 * (asterisk) designates entire line as comment

/ (slash) forces page break when printing source listing

- (dash) to indicate continuation of nonnumeric literal

Columns 8-72 divided into two areas

Area A - columns 8 to 11

Area B - columns 12 to 72

Division, section and paragraph-names must all begin in Area A and end with a period.

2.5. DIVISIONS OF COBOL

Every COBOL program contains up to four separate divisions in the following order:

IDENTIFICATION DIVISION

ENVIRONMENT DIVISION

DATA DIVISION

PROCEDURE DIVISION

www.mainframe230.blogspot.com Page 7 date:16.05.2010

Page 8: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

3. IDENTIFICATION DIVISIONThe Identification Division is:

First division of a Cobol program.

Identifies program name to operating system.

Provides documentation about program.

Paragraph ‘PROGRAM-ID’ followed by a user-defined name is compulsory. Other paragraphs are optional but essential for documentation.

Should begin in Area A.

Length of PROGRAM-ID differs from compiler to compiler.

For example, in the case of IBM COBOL, it is 8 characters.

3.1. SYNTAX OF IDENTIFICATION DIVISION

IDENTIFICATION DIVISION.PROGRAM-ID. PROGRAM NAME.AUTHOR.COMMENT ENTRY.INSTALLATION.COMMENT ENTRY.DATE-WRITTEN.COMMENT ENTRY.DATE-COMPILED.COMMENT ENTRY.SECURITY.COMMENT ENTRY.

Security here does not pertain to the operating system security, but the information that is passed to the user of the program about the Security features of the program.

www.mainframe230.blogspot.com Page 8 date:16.05.2010

Page 9: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

4. ENVIRONMENT DIVISION

It is a machine dependent division.

The Division itself is optional but for file handling it is essential.

Should begin in Area A.

Division is terminated by a period.

Describes the computer used, peripheral devices, etc.

Contains two sections namely, CONFIGURATION and INPUT-OUTPUT.

4.1. CONFIGURATION SECTION Mentions the name of the source-computer, object-computer, special-names, symbolic-names and

classes defined by programmer.

The paragraphs of configuration section are SOURCE-COMPUTER, OBJECT-COMPUTER, and SPECIAL-NAMES.

4.1.1. SOURCE-COMPUTER Used to specify the name of the Source Computer compiling the COBOL program.

The “With Debugging Mode” option can be mentioned to enable the Compiler to include all the lines with ‘D’ in the 7th column as syntax (Otherwise it is taken as comment). It can also include the debug Declaratives in PROCEDURE DIVISION.

4.1.2. OBJECT COMPUTER Used to specify the target system.

The program collating sequence can also be specified in this paragraph for SORTING and STRING COMPARISON. By default, EBCDIC code is followed in MVS. We can change it to ASCII, or any other order if we define SPECIAL-NAMES.

4.1.3. SPECIAL-NAMES Specifies collating sequence.

Maps IBM specified environment names to user defined mnemonic names.

Substitutes character for currency sign.

Indicates that ‘ , ‘ and ‘ . ‘ are to be interchanged in the PIC clause.

We can also specify symbolic characters, such as, PERCENTAGE-SIGN IS 38.

Regardless of the number of entries in this paragraph, there should be only one period at the end.

www.mainframe230.blogspot.com Page 9 date:16.05.2010

Page 10: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

4.2. INPUT-OUTPUT SECTION Contains information regarding files to be used in the program.

Consists of two paragraphs FILE-CONTROL and I-O CONTROL.

4.2.1. FILE-CONTROLAssociates each file used in a COBOL program with an external ddname. Physical dataset is assigned in DD statement of JCL at execution time.

Three formats for the FILE-CONTROL paragraph are:

Sequential file entries

Indexed file entries

Relative file entries

Coding rules for FILE-CONTROL paragraph.

SELECT clause must appear first

Other clauses may appear in any order

Each clause must start in Area B

4.2.2. I-O CONTROL Optional Paragraph.

Specifies when check points are to be taken.

Specifies the storage areas to be shared by different files.

The key word I-O control must begin in Area A.

Example:

IDENTIFICATION DIVISION.PROGRAM-ID. EDS001.AUTHOR. A343.ENVIRONMENT DIVISION.CONFIGURATION SECTION.SOURCE-COMPUTER. IBM-390 WITH DEBUGGING MODE. OBJECT-COMPUTER. IBM-390PROGRAM COLLATING SEQUENCE IS A01.SPECIAL-NAMES. ALPHABET A01 IS "A" ALSO "a" "B" ALSO "b" ALPHABET A IS STANDARD-1 SYSIN IS PROGIN CURRENCY IS "@" DECIMAL-POINT IS COMMA.INPUT-OUTPUT SECTION.FILE-CONTROL

SELECT OUTFILE ASSIGN TO DD1ORGANIZATION IS SEQUENTIALACCESS MODE SEQUENTIALFILE STATUS IS WS-FST.

www.mainframe230.blogspot.com Page 10 date:16.05.2010

Page 11: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

5. DATA DIVISIONData division is used to define data needed to be accessed by the program.

Describes input and output format of data in files.Defines any constants and work areas. The three sections of this division are:FILE SECTIONDescribes Record Structure of Files

WORKING-STORAGE SECTION

Succeeds file section

Used for intermediate storage

Common practice to group related items together.

LINKAGE SECTION

Describes data made available from another program.

FILE SECTIONFILE SECTION

File Description Entries

Each file described with an FD (File Descriptor) sentence

One FD for each SELECT statement in ENVIRONMENT DIVISION

Syntax:

FD file-name

RECORD IS OMITTED

LABEL

RECORDS ARE STANDARD

RECORD CONTAINS integer-1 CHARACTERS

BLOCK CONTAINS integer-2 RECORDS.

File Description clauses

LABEL RECORDS clause - Used if labels stored in file

BLOCKS CONTAINS clause - Indicates blocking factor for disk files

Both labels and blocking usually handled by operating systems so clauses will be omitted

RECORD CONTAINS clause

- Indicates size of each record (Optional but recommended since it provides check on record size)

Defining a Record

Each FD followed by record description entries for the file

Record-name defined at the 01 level -Considered highest level of data Fields within a record defined at subordinate level with level numbers from 02 to 49 Example: 01

www.mainframe230.blogspot.com Page 11 date:16.05.2010

Page 12: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

Employee-Rec-In. 05 Name-In … 05 Annual-Salary-In … 05 Job-Description-In …WORKING-STORAGE SECTIONFollows FILE

SECTION

Starts in Area A, ends with period

All items must be defined at 01 level or in entries subordinate to 01 level entry

Rules for user-defined data-names apply

Elementary items:

Must include PICTURE clause

May be assigned initial value with VALUE clause

5.1. LITERALS AND FIGURATIVE CONSTANTS Literal is a constant representing a number or Non number (Alpha numeric) like 45.3 /

‘MANAGER’.

Literals are storable in a memory Location having a user defined name called Data Name or Identifier or Variable or Field

Figurative Constant is a COBOL RESERVED WORD representing frequently used constants like ZEROS / SPACES.

Figurative constants are used in the program as such for better readability as.

MOVE SPACES TO WS-REC.

Literals are classified in to NUMERIC and NON-NUMERIC literals.

5.1.1. NUMERIC LITERAL Must contain at least one digit

+ or - Sign, if used must be the left-most character

Not more than one decimal point

Decimal may not be the right most character

Maximum of 18 digits.

05 TOTAL-MARK PIC 9(3) VALUE 100.

Here 100 is a numeric constant assigned in identifier TOTAL-MARK.

5.1.2. NON-NUMERIC LITERAL May consist of any character in the character set

Must be enclosed in quotes

A numeric literal in quotes is non-numeric

Maximum length of 120 chars

05 COMPANY-NAME PIC X(3) VALUE ‘EDS’.

Here ‘EDS’ is an alphanumeric constant assigned in identifier COMPANY-NAME,

www.mainframe230.blogspot.com Page 12 date:16.05.2010

Page 13: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

5.1.3. LIST OF FIGURATIVE CONSTANTS

ZERO / ZEROES / ZEROS ONE OR MORE ZEROS

QUOTE / QUOTES ONE OR MORE “

SPACE / SPACES ONE OR MORE SPACE

HIGH-VALUE / HIGH-VALUES HIGHEST VALUE IN COLLATING SEQUENCE

X’FF’ FOR EBCDIC

LOW-VALUE / LOW-VALUES LOWEST VALUE IN COLLATING SEQUENCE

ALL literal ONE or MORE OCCURENCES OF literal

5.2. DATA DESCRIPTION The general format of the data description is

Level-number data-name / FILLER clauses

5.2.1. LEVEL NUMBERS A Record is a Collection of all the fields pertaining to an Item/person/Transaction.

A record structure is a structure of all the fields of a record. It may be available in the FILE SECTION/ WORKING-STORAGE SECTION/ LINKAGE SECTION.

The level-number specifies the hierarchy of data within a record, and identifies special-purpose data entries. A level-number begins a data description entry, a renamed or redefined item, or a condition-name entry.

A level-number has a value taken from the set of integers between 1 and 49, or from one of the special level-numbers, 66, 77, or 88.

01 This level - number specifies the record itself.

A level - 01 entry may be either a group item or an elementary item.

It must begin in Area A.

02-49 These level numbers specify group and elementary items within a record.

They may begin in Area A or B

Group Level Data Names Will not have Picture Clause.

01 EMPLOYEE-REC. 02 EMP-NO PIC XXXX. 02 EMP-NAME.

03 FIRST-NAME PIC A(30).03 INITIAL-1 PIC A.03 INITIAL-2 PIC A.03 INITIAL-3 PIC A.

5.2.1.1. SPECIAL LEVEL NUMBERS 66 Identifies items that contain the RENAMES clause.

www.mainframe230.blogspot.com Page 13 date:16.05.2010

Page 14: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

77 Identifies independent elementary data item.

88 Identifies any condition name that is associated with a particular value of conditional variable

Level Number 01 can appear as a Group Level Number as well as Elementary data item level number.

It can appear only as an elementary Data Item Level Number.

Example

01 TOTAL PIC 9(5).

77 WS-FLAG PIC XX.

01 RECORD─I. 05 DN─1... . │<───────────────RECORD─I───────────────>│ 05 DN─2... . ┌──────┬──────────┬───────────┬──────────┐ 05 DN─3... . │ DN─1 │ DN─2 │ DN─3 │ DN─4 │ 05 DN─4... . └──────┴──────────┴───────────┴──────────┘66 DN─6 RENAMES DN─1 THROUGH DN─3. │<───────────DN─6────────────>│

WORKING-STORAGE SECTION. 01 SWITCHES. 02 LOADDATA-EOF-SWITCH PIC X(1) VALUE "N". 88 LOADDATA-EOF VALUE "Y".

5.2.2. DATA NAMES Identifier name/ Data name should not exceed 30 Characters.

There should be at least one Alphabet anywhere in the name.

Only special Character allowed is – ( Hyphen) but should not be at the Beginning or End

Examples of data names

EMPLOYEE-NAME , COMPANY, 0001AB, 999-EMPLOYEE-RECORD

Data name must be unique within a Record

If ‘Name’ is duplicated across Records, it has to be qualified as Identifier-name of Record Name.

DISPLAY BALANCE OF STOCK-RECORD

Here balance is an identifier declared in Stock-record.

Identify the valid data-names

1.Date-Of-Birth

2. Amount$Out

3. Section

4. My-Section

5. -First-Name

6. Last--Name

7. 98-6

8. Time out

www.mainframe230.blogspot.com Page 14 date:16.05.2010

Page 15: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

5.2.2.1. FILLERWhenever there is no need for specific reference to fields in a record structure, we name it as

FILLER or F. This Data name is used wherever there is fixed information like SPACES Or Headings

Are stored.

Example

01 Detail-record.

05 FILLER PIC X(10) VALUE SPACES.

05 NAME PIC X(30) .

05 FILLER PIC X(10) VALUE SPACES. 05 BASIC PIC 9(05) .

05 FILLER PIC X(10) VALUE SPACES. 05 DA PIC 9(04) .

05 FILLER PIC X(10) VALUE SPACES. 05 GROSS PIC 9(6).

5.2.3. CLAUSEA Clause specifies certain characteristics of the data item being described. The detail of each Clause is explained below.

5.2.3.1. PICTURE CLAUSEDescribes the class (numeric, alphabetic, alphanumeric, edited numeric), sign(signed/unsigned), decimal point location and size characteristics of a data item.

Specified for every elementary item

Format of Picture Clause.

TYPE CHARACTER STRING REMARK

Numeric 9 Used for arithmetic operations

V Assumed Decimal Point

S Data item is Signed

P Position of Assumed decimal point when the point lies outside the data item

Alphabetic A Data Item contains only a Letter Or Space

B Blank insertion character

Alpha Numeric

X Data item contains any allowable character from the COBOL character set.

Examples

VAR1 PIC A(04) (Defines 4 alphabetic characters.)

VAR2 PIC 9(4) (Defines 4 Numeric digits)

www.mainframe230.blogspot.com Page 15 date:16.05.2010

Page 16: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

5.2.3.1.1. EDITED PICTURE CLAUSESUsed to display Numeric and other data in the human readable form.

Displaying the number as 77,419.56 is easier to read instead of 7741958.

5.2.3.1.2. EDIT TYPES - NUMERIC DATAZ ZERO SUPPRESSION

* ASTERISK

$ CURRENCY SIGN

- MINUS SIGN

+ PLUS SIGN

CR DB CREDIT DEBIT SIGN

PERIOD( . ) COMMA( , ) BLANK( ‘B‘ ) ZERO( 0 ) SLASH( / )

BLANK WHEN ZERO (Inserts blanks when data value is zero)

5.2.3.1.3 EDIT TYPES - ALPHABETICUsed To Insert Blank within Alphabetic Characters.

Example: 10 VAR1 PIC A(2)BBBA(3).

MOVE “xyabc” TO VAR1

DISPLAY VAR1 /* displays xy abc */

5.2.3.1.4. EDIT TYPES - ALPHANUMERIC

BLANK, ZERO and SLASH INSERTION

PIC OF THE FIELD NUMERIC VALUE EDITED VALUE

ZZZV99

* * 999

$ * *999

-ZZZV99

+999

+999

9999+

ZZ,Z99

ZZZZ.ZZ

$$$$9.99

99B99B99

09990

999/999/99

38^4

00052

985

-46^52

-382

382

-382

2456

5

342

46

456

3254

b3840

* * 052

$**985

-b4652

-382

+382

0382-

b2,456

bbbb.05

b$342.00

00b00b46

04560

000/032/54

BLANK WHEN ZERO

ZZZ.99 BLANK WHEN ZERO 2 5 bb2.50

www.mainframe230.blogspot.com Page 16 date:16.05.2010

Page 17: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

999.99 BLANK WHEN ZERO

0

bbbbbb

5.2.3.1.5. EDIT COMBINATION+, -, CR, DB Are Mutually Exclusive

$, +, -, Z, * Are Mutually Exclusive.

$ can appear at first place and * as Floating($****.**)

V and . Are Mutually Exclusive

5.2.3.2. VALUE CLAUSEAssigns initial value to a data item

VALUE IS literal

Literal can be Numeric, Non-Numeric(in Quotes) or Figurative Constant

Not for items whose size is variable

Value of item should not exceed picture size

Consistent with class of PIC clause

Examples

01 WS-REC.

05 WS-NAME PIC X(30) VALUE ‘ABC COMPANY ‘.

05 FILLER PIC X(20) VALUE ALL ‘ - ‘.

01 WS-NUM1 PIC 9(5) VALUE ZERO.

01 WS-CHAR2 PIC X(5) VALUE ZERO.

5.2.3.3. USAGE CLAUSE Specifies how a data item is represented internally.

DISPLAY

COMPUTATIONAL

SYNTAX

USAGE IS [DISPLAY ]

[COMPUTATIONAL] [COMP-1] [COMP-2] [COMP-3]

5.2.3.3.1. DISPLAY USAGE.Each character of the data is represented in one byte .

The number of bytes required is equal to the size of the item.

DISPLAY is the default usage

5.2.3.3.2. COMPUTATIONAL(COMP) USAGE Maintained in binary

www.mainframe230.blogspot.com Page 17 date:16.05.2010

Page 18: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

Only integral numbers

Depending on the size of the data item, it can be stored either in half word or full word

Should be numeric only

NUMBER OF digits IN PIC LENGTH OF ITEM IN BYTES

1 TO 4 2

5 TO 9 4

10 TO 18 8

01 WS - NUM PIC S9(004) USAGE COMP.

COMP-1 USAGE .

One word in floating point form

The number is represented in hexadecimal

The picture clause cannot be specified

Suitable for arithmetic operations

WS-NUM USAGE COMP-1.

COMP-2 USAGE.

Same as COMP 1 except that data is represented internally in two words

Increases the precision of the data

COMP-3 USAGE.

Internal Representation In Packed Decimal Form

Each digit and sign occupy 1/2 a byte

The Hexadecimal number C or F denotes a positive sign

The Hexadecimal number D denotes a negative sign

Maximum precision is 18

EXAMPLE FOR USAGE COMP - 3

10 A PIC S9(3) USAGE COMP-3 VALUE 123.

Internally stored as

00010010 0011 1111

1 2 3 F

10 A PIC S9(04) USAGE COMP-3 VALUE 123.

is stored as

00000000 0001 0010 0011 1111

(Extra Byte)

5.2.3.4. SIGN CLAUSE [SIGN IS] [LEADING] [SEPARATE CHAR ] [TRAILING]

www.mainframe230.blogspot.com Page 18 date:16.05.2010

Page 19: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

Specifies the position and mode of representation of sign

Only for numeric elementary items

Picture string should contain ‘S’

Usage should be DISPLAY

Default is TRAILING without separate character

EXAMPLE

PIC VALUE SIGN REPRESENTATION

S9(3) -243 LEADING K 4 3

S9(3) -243 TRAILING 2 4 L

5.2.3.5. OCCURS CLAUSEThere may be a need to keep a table of values in the memory for calculations. We use OCCURS clause to allocate physically contiguous memory locations to store the table values and access them with subscripts.

Example

01 WS-TABLE.

10 WS-NAME OCCURS 10 TIMES PIC X(20).

01 WS-TABLE.

10 WS-REC OCCURS 10 TIMES.

15 X PIC X(5).

15 Y PIC X(8).

WS - REC (1) = 13 BYTES (i.e. X(1) = 5 bytes, Y(1) = 8 bytes)

Hence, 10 such group items (WS-REC) are allocated memory space contiguously.

5.2.3.6. REDEFINES CLAUSEThis clause allows the same area of memory to be referenced by more than one data-name with different formats and sizes.

SYNTAX AND RULES

< Lvl.no > <Data-item-1> REDEFINES <Data-item-2>.

Redefines clause must immediately follow <data-item-1>

Level nos of <data-item-1> and <data-item-2> must be identical and not 66 or 88.

5.2.3.6.1.Explicit Use OF REDEFINESREDEFINES must be used for re-mapping record areas outside the FILE-SECTION, or within the FILE-SECTION if a part of a record is to be remapped.

Example:

FD CARDIN.01 D-TYPE-1

05 CARD-CODE PIC 9.05 NAME-ADD.

10 NAME PIC X(20).

www.mainframe230.blogspot.com Page 19 date:16.05.2010

Page 20: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

10 ADD-LN-1 PIC X(40).05 NAME-DD REDEFINES NAME-ADD.

10 ADD-LN-2 PIC X(60).

5.2.3.6.2. RESTRICTIONS ON USE OF REDEFINESRedefinition ceases whenever a level number is less than or equal to that of <data-item-1> or <data-item-2>

Multiple redefinition’s of the same area is possible provided there are no new storage areas defined in between < data-item - 1 > and <data-item-2>

Value clauses are allowed only in condition names in a redefined data-item.

At 01 level, OCCURS and REDEFINES cannot be combined. <data-name-2> may not have an OCCURS clause.

5.2.3.7. RENAMESRegrouping of elementary data items in a Record.

01 W-RESPONSE05 W-CHAR-123 PIC XXX.05 W-CHAR-4 PIC X.05 W-CHAR-56 PIC XX.

66 ADD-RESPONSE RENAMES W-CHAR-123. 66 VIEW-RESPONSES RENAMES W-CHAR-123 THRU W-CHAR-4. 66 DELETE-RESPONSE RENAMES W-CHAR-123 THRU W-CHAR-56.

5.2.3.8. JUSTIFIED CLAUSEOverrides standard positioning rules for a receiving stem of the alphabetic or alphanumeric categories.

01 WS-CHAR PIC X(05) JUSTIFIED RIGHT.Default is left justified

Justified clause does not alter initial settings as determined by the value clause

Justified clause must not be specified with level 66(RENAMES) and level 88(condition-names) clauses.

The question of justification comes only when you move a smaller data item to a larger data item. Example would be a PIC X(5) field to a PIC X(20)

5.2.4. CONDITION NAMES Allow users to assign acceptable values for data names

Are used as an abbreviation for condition checking

01 W-PERSON-AGE PIC 9(02).

88 C-PERSON-MINOR VALUE O THRU 20

88 C-PERSON-ADULT VALUE 21 THRU 99

www.mainframe230.blogspot.com Page 20 date:16.05.2010

Page 21: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

6. PROCEDURE DIVISION Last division in a COBOL program

Contains statements which specify the operations to be performed

Contains the following structure

Division:

A division is a block of code, usually containing one or more sections, that starts where the division name is encountered and ends with the beginning of the next division or with the end of the program text.

Section:

A section is a block of code usually containing one or more paragraphs. A section begins with the section name and ends where the next section name is encountered or where the program text ends.

Example:

SelectUnpaidBills SECTION.FILE SECTION.

Paragraphs:

A paragraph is a block of code made up of one or more sentences. A paragraph begins with the paragraph name and ends with the next paragraph or section name or the end of the program text.

Example:

PrintFinalTotals. PROGRAM-ID.

Sentences and statements:

A sentence consists of one or more statements and is terminated by a period.Example: MOVE .21 TO VatRate

MOVE 1235.76 TO ProductCostCOMPUTE VatAmount = ProductCost * VatRate.

A statement consists of a COBOL verb and an operand or operands. Example:

SUBTRACT Tax FROM GrossPay GIVING NetPay

PROCEDURE DIVISION GUIDELINES:

www.mainframe230.blogspot.com Page 21 date:16.05.2010

Page 22: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

Paragraph names and section names should start in Area A

Statements and sentences should start in Area B

Section names and paragraph names are user-defined

Section names must be unique and must be different from paragraph names

Section names should be followed by a space and the word SECTION with a period at the end

Paragraph names must be unique within a section

If paragraph name is duplicated across sections, it has to be qualified as paragraph-name { of / in } section name

6.1. STATEMENTS IN PROCEDURE DIVISIONProcedure division statements can be broadly classified as

Imperative Statements

Statement which directs the program to take a specific action during execution

Examples MOVE, ADD, GOTO, EXIT.

Conditional Statements

A conditional statement specifies that the truth value of a condition is to be determined, and that the subsequent action of the object program is dependent on this truth value.

Examples: IF , EVALUATE , ADD...ON SIZE ERROR, ADD...NOT ON SIZE ERROR

COMPUTE...ON SIZE ERROR, COMPUTE...NOT ON SIZE ERROR

Compiler Directives

A Compiler-Directing Statement is a statement, beginning with a compiler directing verb, that causes the compiler to take a specific action during compilation.

Example : Use, Copy

Explicit Scope Terminator

An EXPLICIT SCOPE TERMINATOR terminates the scope of conditional statements. This includes statements which contain conditional expression (like IF statement) and imperative statements (like ADD)

Examples

END-ADD, END-COMPUTE, END-SUBTRACT, END-MULTIPLY , END-DELETE, END-READ, END-IF,

END-EVALUATE, END-CALL,

Example with ADD

ADD 1 TO TWODIGITYEAR ON SIZE ERROR PERFORM CENTURYWINDOWOVERFLOW END-ADD

Implicit Scope Terminator

At the end of any sentence, an IMPLICIT SCOPE TERMINATOR is a separator period that terminates the scope of all previous statements not yet terminated.

www.mainframe230.blogspot.com Page 22 date:16.05.2010

Page 23: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

6.2. DATA MOVEMENT

6.2.1. MOVE To move data into a place in memory.

Format,

MOVE {IDENTIFIER-1/LITERAL-1} TO IDENTIFIER-2[,IDENTIFIER-3],..

6.2.1.1. DATA MOVEMENT RULES Only one sending field, one or more receiving fields.

Value of the sending field remains unaltered after the statement execution.

6.2.1.2. NUMERIC DATA TRANSFER RULES When sending field is numeric or numeric-edited, the Data Movement is called Numeric.

The dominant factor in the numeric data transfer is the alignment of decimal points of the two fields.

If the decimal point is not explicitly indicated, the decimal point is assumed to be at the right of the rightmost digit.

If the receiving field is not large enough to hold the data received, truncation can take place at either ends or at both ends.

If the significant integral positions are likely to be lost, a warning to that effect is issued by the compiler.

If the receiving field is larger than the sending field, the unused positions will be filled with zeros, which is known as ‘ zero fill ‘.

6.2.1.3. ALPHANUMERIC DATA TRANSFER When both the sending and receiving fields are alphabetic, alphanumeric or alphanumeric edited, the

Data Movement is called as Alphanumeric.

In the case of alphanumeric data transfer, receiving area is filled from left to right.

When the receiving field is smaller, truncation occurs from the right and the compiler gives a warning to that effect

If the receiving field is larger than the field, the unused positions will be filled with spaces, which is called as ‘ space fill ‘

Examples:

A defined as PIC 9999

B defined as PIC ZZZ9

MOVE 15 TO A.

MOVE A TO B

After execution A will contain 0015 and B will contain bb15.

6.2.1.4. GROUP MOVESWhen at Least One Of The Fields is a Group item, it is called A Group Move

www.mainframe230.blogspot.com Page 23 date:16.05.2010

Page 24: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

Example:

01 REC-1.05 A1 PIC 9999.05 A2 PIC AA.05 A3 PIC XXXX.

01 REC-205 B1 PIC 9999.05 B2 PIC AA.05 B3 PIC XXXX.

MOVE REC-1 TO REC-2

6.2.1.5. MOVE CORRESPONDINGWhen the names of the corresponding data item of two groups are the same, this statement can be used to substitute for a set of move statements

MOVE CORRESPONDING IDENTIFIER-1 TO IDENTIFIER-2.

Source and destination groups can include data names that are not common

Only the fields having identical names in the two records will take part in the data movements

The remaining data items in the destination group will remain unchanged

Example:

01 PAY-REC.05 EMP-NO. PIC 9(5)05 EMP-NAME PIC X(30).05 EMP-BASIC PIC 9(5)V99.

01 PRINT-REC.05 FILLER PIC X(5).05 EMP-NO PIC 9(5).05 FILLER PIC X(5).05 EMP-NAME PIC X(30).05 FILLER PIC X(5).05 EMP-SAL PIC 9(6).99.

MOVE CORRESPONDING PAY-REC TO PRINT-REC.

6.3. ARITHMETIC VERBS

6.3.1. ADD This verb is used to find the sum of two or more numbers and to store the resultant sum

<──────────────────┐ <───────────────────────────┐ >>──ADD────┬─identifier-1─┬─┴──TO────identifier-2──┬─────────┬─┴─────> └─literal-1────┘ └─ROUNDED─┘ >──┬────────────────────────────────────────────┬────────────────────> └─┬────┬──SIZE ERROR──imperative-statement-1─┘ └─ON─┘ >──┬─────────────────────────────────────────────────┬───────────────> └─NOT──┬────┬──SIZE ERROR──imperative-statement-2─┘ └─ON─┘ >──┬─────────┬──────────────────────────────────────────────────────>< └─END-ADD─┘

All identifiers or literals preceding the key word TO are added together, and this sum is added to and stored in identifier-2. This process is repeated for each successive occurrence of identifier-2, in the left-to-right order in which identifier-2 is specified.

www.mainframe230.blogspot.com Page 24 date:16.05.2010

Page 25: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

<──────────────────┐ >>──ADD────┬─identifier-1─┬─┴──┬────┬──┬─identifier-2─┬──────────────> └─literal-1────┘ └─TO─┘ └─literal-2────┘ <───────────────────────────┐ >──GIVING────identifier-3──┬─────────┬─┴─────────────────────────────> └─ROUNDED─┘ >──┬────────────────────────────────────────────┬────────────────────> └─┬────┬──SIZE ERROR──imperative-statement-1─┘ └─ON─┘ >──┬─────────────────────────────────────────────────┬───────────────> └─NOT──┬────┬──SIZE ERROR──imperative-statement-2─┘ └─ON─┘ >──┬─────────┬──────────────────────────────────────────────────────>< └─END-ADD─┘

The values of the operands preceding the word GIVING are added together, and the sum is stored as the new value of each data item referenced by identifier-3.

>>──ADD──┬─CORRESPONDING─┬──identifier-1──TO──identifier-2───────────> └─CORR──────────┘ >──┬─────────┬──┬────────────────────────────────────────────┬───────> └─ROUNDED─┘ └─┬────┬──SIZE ERROR──imperative-statement-1─┘ └─ON─┘ >──┬─────────────────────────────────────────────────┬───────────────> └─NOT──┬────┬──SIZE ERROR──imperative-statement-2─┘ └─ON─┘ >──┬─────────┬──────────────────────────────────────────────────────>< └─END-ADD─┘

Elementary data items within identifier-1 are added to and stored in the corresponding elementary items within identifier-2.

Example:

ADD A TO BADD A B GIVING C.ADD CORR OLD-REC TO NEW-REC.

In the case of TO option, the previous value of the last named operand takes part in the summation and then this value is replaced by the result.

Whereas in the case of GIVING option, the value of the last named operand does not take part in the summation and only the result is stored there.

In the group add, numeric elementary items in the group referred to by first identifier are added to and stored in the corresponding elementary items of the second group.

Data items in identifier-1 and identifier-2 take part in the summation, only if they have the Same data name and same qualifiers

Corresponding items can have different locations within the group and the field sizes Can also be different.

6.3.2. SUBTRACT

<──────────────────┐ >>──SUBTRACT────┬─identifier-1─┬─┴──FROM─────────────────────────────>

www.mainframe230.blogspot.com Page 25 date:16.05.2010

Page 26: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

└─literal-1────┘ <───────────────────────────┐ >────identifier-2──┬─────────┬─┴─────────────────────────────────────> └─ROUNDED─┘ >──┬────────────────────────────────────────────┬────────────────────> └─┬────┬──SIZE ERROR──imperative-statement-1─┘ └─ON─┘ >──┬─────────────────────────────────────────────────┬───────────────> └─NOT──┬────┬──SIZE ERROR──imperative-statement-2─┘ └─ON─┘ >──┬──────────────┬─────────────────────────────────────────────────>< └─END-SUBTRACT─┘

All identifiers or literals preceding the key word FROM are added together and this sum is subtracted from and stored immediately in identifier-2. This process is repeated for each successive occurrence of identifier-2, in the left-to-right order in which identifier-2 is specified.

<──────────────────┐ >>──SUBTRACT────┬─identifier-1─┬─┴──FROM──┬─identifier-2─┬───────────> └─literal-1────┘ └─literal-2────┘ <───────────────────────────┐ >──GIVING────identifier-3──┬─────────┬─┴─────────────────────────────> └─ROUNDED─┘ >──┬────────────────────────────────────────────┬────────────────────> └─┬────┬──SIZE ERROR──imperative-statement-1─┘ └─ON─┘ >──┬─────────────────────────────────────────────────┬───────────────> └─NOT──┬────┬──SIZE ERROR──imperative-statement-2─┘ └─ON─┘ >──┬──────────────┬─────────────────────────────────────────────────>< └─END-SUBTRACT─┘

All identifiers or literals preceding the key word FROM are added together and this sum is subtracted from identifier-2 or literal-2. The result of the subtraction is stored as the new value of each data item referenced by identifier-3.

>>──SUBTRACT──┬─CORRESPONDING─┬──identifier-1──FROM──────────────────> └─CORR──────────┘ >──identifier-2──┬─────────┬─────────────────────────────────────────> └─ROUNDED─┘ >──┬────────────────────────────────────────────┬────────────────────> └─┬────┬──SIZE ERROR──imperative-statement-1─┘ └─ON─┘ >──┬─────────────────────────────────────────────────┬───────────────> └─NOT──┬────┬──SIZE ERROR──imperative-statement-2─┘ └─ON─┘ >──┬──────────────┬─────────────────────────────────────────────────>< └─END-SUBTRACT─┘

www.mainframe230.blogspot.com Page 26 date:16.05.2010

Page 27: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

Elementary data items within identifier-1 are subtracted from, and the results are stored in, the corresponding elementary data items within identifier-2.

Example:

SUBTRACT A FROM B.

SUBTRACT A B FROM C.

SUBTRACT A B FROM C D.

SUBTRACT 10 FROM A.

6.3.3. MULTIPLY <───────────────────────────┐ >>──MULTIPLY──┬─identifier-1─┬──BY────identifier-2──┬─────────┬─┴────> └─literal-1────┘ └─ROUNDED─┘ >──┬────────────────────────────────────────────┬────────────────────> └─┬────┬──SIZE ERROR──imperative-statement-1─┘ └─ON─┘ >──┬─────────────────────────────────────────────────┬───────────────> └─NOT──┬────┬──SIZE ERROR──imperative-statement-2─┘ └─ON─┘ >──┬──────────────┬─────────────────────────────────────────────────>< └─END-MULTIPLY─┘

The value of identifier-1 or literal-1 is multiplied by the value of identifier-2; the product is then placed in identifier-2. For each successive occurrence of identifier-2, the multiplication takes place in the left-to-right order in which identifier-2 is specified. >>──MULTIPLY──┬─identifier-1─┬──BY──┬─identifier-2─┬─────────────────> └─literal-1────┘ └─literal-2────┘ <───────────────────────────┐ >──GIVING────identifier-3──┬─────────┬─┴─────────────────────────────> └─ROUNDED─┘ >──┬────────────────────────────────────────────┬────────────────────> └─┬────┬──SIZE ERROR──imperative-statement-1─┘ └─ON─┘ >──┬─────────────────────────────────────────────────┬───────────────> └─NOT──┬────┬──SIZE ERROR──imperative-statement-2─┘ └─ON─┘ >──┬──────────────┬─────────────────────────────────────────────────>< └─END-MULTIPLY─┘

The value of identifier-1 or literal-1 is multiplied by the value of identifier-2 or literal-2. The product is then stored in the data item(s) referenced by identifier-3.

Example:

MULTIPLY A BY B.

www.mainframe230.blogspot.com Page 27 date:16.05.2010

Page 28: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

MULTIPLY A BY B C D.

MULTIPLY A BY B GIVING C.

MULTIPLY 0.5 BY B.

6.3.4. DIVIDE <───────────────────────────┐ >>──DIVIDE──┬─identifier-1─┬──INTO────identifier-2──┬─────────┬─┴────> └─literal-1────┘ └─ROUNDED─┘ >──┬────────────────────────────────────────────┬────────────────────> └─┬────┬──SIZE ERROR──imperative-statement-1─┘ └─ON─┘ >──┬─────────────────────────────────────────────────┬───────────────> └─NOT──┬────┬──SIZE ERROR──imperative-statement-2─┘ └─ON─┘ >──┬────────────┬───────────────────────────────────────────────────>< └─END-DIVIDE─┘ In Format 1, the value of identifier-1 or literal-1 is divided into the value of identifier-2, and the quotient is then stored in identifier-2. For each successive occurrence of identifier-2, the division takes place in the left-to-right order in which identifier-2 is specified. >>──DIVIDE──┬─identifier-1─┬──INTO──┬─identifier-2─┬─────────────────> └─literal-1────┘ └─literal-2────┘ <───────────────────────────┐ >──GIVING────identifier-3──┬─────────┬─┴─────────────────────────────> └─ROUNDED─┘ >──┬────────────────────────────────────────────┬────────────────────> └─┬────┬──SIZE ERROR──imperative-statement-1─┘ └─ON─┘ >──┬─────────────────────────────────────────────────┬───────────────> └─NOT──┬────┬──SIZE ERROR──imperative-statement-2─┘ └─ON─┘ >──┬────────────┬───────────────────────────────────────────────────>< └─END-DIVIDE─┘ In Format 2, the value of identifier-1 or literal-1 is divided into the value of identifier-2 or literal-2. The value of the quotient is stored in each data item referenced by identifier-3. >>──DIVIDE──┬─identifier-1─┬──BY──┬─identifier-2─┬───────────────────> └─literal-1────┘ └─literal-2────┘ <───────────────────────────┐ >──GIVING────identifier-3──┬─────────┬─┴─────────────────────────────> └─ROUNDED─┘ >──┬────────────────────────────────────────────┬────────────────────> └─┬────┬──SIZE ERROR──imperative-statement-1─┘ └─ON─┘

www.mainframe230.blogspot.com Page 28 date:16.05.2010

Page 29: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

>──┬─────────────────────────────────────────────────┬───────────────> └─NOT──┬────┬──SIZE ERROR──imperative-statement-2─┘ └─ON─┘ >──┬────────────┬───────────────────────────────────────────────────>< └─END-DIVIDE─┘ In Format 3, the value of identifier-1 or literal-1 is divided by the value of identifier-2 or literal-2. The value of the quotient is stored in each data item referenced by identifier-3. >>──DIVIDE──┬─identifier-1─┬──INTO──┬─identifier-2─┬─────────────────> └─literal-1────┘ └─literal-2────┘ >──GIVING──identifier-3──┬─────────┬──REMAINDER──identifier-4────────> └─ROUNDED─┘ >──┬────────────────────────────────────────────┬────────────────────> └─┬────┬──SIZE ERROR──imperative-statement-1─┘ └─ON─┘ >──┬─────────────────────────────────────────────────┬───────────────> └─NOT──┬────┬──SIZE ERROR──imperative-statement-2─┘ └─ON─┘ >──┬────────────┬───────────────────────────────────────────────────>< └─END-DIVIDE─┘ In Format 4, the value of identifier-1 or literal-1 is divided into identifier-2 or literal-2. The value of the quotient is stored in identifier-3, and the value of the remainder is stored in identifier-4. >>──DIVIDE──┬─identifier-1─┬──BY──┬─identifier-2─┬───────────────────> └─literal-1────┘ └─literal-2────┘ >──GIVING──identifier-3──┬─────────┬──REMAINDER──identifier-4────────> └─ROUNDED─┘ >──┬────────────────────────────────────────────┬────────────────────> └─┬────┬──SIZE ERROR──imperative-statement-1─┘ └─ON─┘ >──┬─────────────────────────────────────────────────┬───────────────> └─NOT──┬────┬──SIZE ERROR──imperative-statement-2─┘ └─ON─┘ >──┬────────────┬───────────────────────────────────────────────────>< └─END-DIVIDE─┘ In Format 5, the value of identifier-1 or literal-1 is divided by identifier-2 or literal-2. The value of the quotient is stored in identifier-3, and the value of the remainder is stored in identifier-4.

Example:

DIVIDE 5 INTO A ..DIVIDE A INTO B GIVING C.DIVIDE A BY B GIVING C D.DIVIDE A BY B GIVING C REMAINDER D.

www.mainframe230.blogspot.com Page 29 date:16.05.2010

Page 30: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

6.3.5. ROUNDED OPTIONThis can be used with any arithmetic verb. It will round the number to the picture clause.

ADD A B GIVING C ROUNDED.This phrase cannot be specified for the identifier that receives the remainder in a divide operation.

6.3.6. ON SIZE ERRORIf after an arithmetic operation, the result exceeds the largest value that can be accommodated in the result field, the error is called a “size error”.

Can be used with any arithmetic verb

When a size error occurs, the contents of the resultant field after the operation is unpredictable.

When a size error occurs, the processing is not terminated and the next statement will be taken up for further execution.

When this phrase is used, the statement becomes a conditional statement.

Example:

ADD A TO B ON SIZE ERROR GO TO ERROR-PARA.

The imperative statements can be delimited by END-ADD

6.3.7. COMPUTE <───────────────────────────┐ >>──COMPUTE────identifier-1──┬─────────┬─┴──┬─=───────┬──────────────> └─ROUNDED─┘ └─ EQUAL ─┘ >──arithmetic-expression─────────────────────────────────────────────> >──┬────────────────────────────────────────────┬────────────────────> └─┬────┬──SIZE ERROR──imperative-statement-1─┘ └─ON─┘ >──┬─────────────────────────────────────────────────┬───────────────> └─NOT──┬────┬──SIZE ERROR──imperative-statement-2─┘ └─ON─┘ >──┬─────────────┬──────────────────────────────────────────────────>< └─END-COMPUTE─┘

This verb is very powerful and is used as a substitute for any of the other arithmetic verbs like add, subtract, multiply, divide.

6.3.8. ARITHMETIC OPERATORSArithmetic expression is formed with numeric operands, numeric LITERALS and numeric operators..

Arithmetic expression always assumes a numeric value.

Arithmetic operators permitted in COBOL are:

** => EXPONENTIATION./ => DIVISION.* => MULTIPLICATION..

www.mainframe230.blogspot.com Page 30 date:16.05.2010

Page 31: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

- => SUBTRACTION.+ => ADDITION.

6.4. MISCELLANEOUS

6.4.1. ACCEPTAccept statement can transfer data from an input/output device, or system information contained in the specified conceptual data items DATE, DAY, DAY-OF-WEEK, or TIME

Format:

ACCEPT IDENTIFIER [ FROM mnemonic nameDATEDAY TIME DAY-OF-WEEK}]

When ‘FROM’ is omitted, the data is read from the operator’s terminal.

At the time of execution, program is suspended until the operator enters the data.

Mnemonic name option is implementation dependent and has to be defined in SPECIAL-NAMES paragraph.

The date option returns six digit [9(6)] current date in YYMMDD format.

The day option returns five digit [9(5)] current date in YYDDD format.

The time option returns eight digit [9(8)] time in HHMMSSTT format

The day-of-week option returns a single digit [9(1)] value as follows:

1 - Monday, 2 - Tuesday, 3 - Wednesday and so on.

ACCEPT THIS-DATE FROM DATE.

6.4.2. DISPLAYDisplay statement is used to display data on the terminal.

Format

DISPLAY {identifier-1 identifier-2

literal-1} , literal - 2

[upon mnemonic-name]

Example

DISPLAY “RESULT IS :“,THE-RESULT

6.4.3. CONTINUEThe CONTINUE statement allows you to specify a no operation statement.

CONTINUE indicates that no executable instruction is present.

www.mainframe230.blogspot.com Page 31 date:16.05.2010

Page 32: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

6.4.4. STOP RUNThis verb is used to terminate the execution of the program.

6.4.5. EXITThe EXIT statement provides a common end point for a series of paragraphs.

This statement indicates a no operation and when executed, no action takes place

6.4.6. INITIALIZEThis statement is used to initialize value of either an elementary or group item.

<──────────────┐ >>──INITIALIZE────identifier-1─┴─────────────────────────────────────────────────────> >──┬────────────────────────────────────────────────────────────────────────┬───────>< │ <─────────────────────────────────────────────────────────┐ │ └─REPLACING────┬─ALPHABETIC──────────┬──┬──────┬──BY──┬─identifier-2─┬─┴─┘ ├─ALPHANUMERIC────────┤ └─DATA─┘ └─literal-1────┘ ├─NUMERIC─────────────┤ ├─ALPHANUMERIC-EDITED─┤ └─NUMERIC-EDITED──────┘

Example: INITIALIZE WS - REC.

01 WS - REC.05 A PIC 9(4).05 B PIC A(4).05 C PIC X(4).

DATAITEM PIC BEFORE AFTEREXECUTION EXECUTION

A 9(4) 1000 0000B A(4) LIFE bbbbC X(4) A2BC bbbb

INITIALIZE BREPLACING NUMERIC DATA BY 105REPLACING ALPHANUMERIC DATA BY “ LOVE “REPLACING NUMERIC-EDITED DATA BY 1500.23.

6.5. CONDITIONAL AND UNCONDITIONAL TRANSFER

6.5.1. SIMPLE IF STATEMENT

IF condition [THEN]statement-1

ELSEstatement-2

END-IF.

The condition can be any one of the conditions mentioned before.

www.mainframe230.blogspot.com Page 32 date:16.05.2010

Page 33: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

Statement-1 represents one or more COBOL statements If the condition is found to be true, the statements represented in the THEN part will be executed. Statement-1 and Statement-2 represents one or more COBOL statements.If the condition is found to be FALSE, the statements represented in the ELSE part will be executedAfter execution of this statement control will be implicitly transferred to the next sentence following the IF statement.Period (.) is placed only at the end of the IF structure. i.e. after the connective END-IF.

Example:IF AMOUNT > 5000 THEN

ADD AMOUNT TO TOTAL-1ELSE

ADD AMOUNT TO TOTAL-2END-IF.

NEXT SENTENCE will be used to make either the THEN or ELSE part dummy.

6.5.2. NESTED IFInclusion of one or more IF statements within its scope is called as Nesting. The most inclusive IF statement must have terminating period.

IF condition-1 [THEN]IF condition-2 [THEN ]

statement-1ELSE

statement-2END-IF

END-IF.Indent nested ‘if‘ s properly to improve readability and maintainability.

6.5.3. EVALUATE STATEMENTUsed for decisions and can be used when many branches are there Can be used as a substitute for nested if“s

EVALUATE SUBJECT-1 [ALSO SUBJECT-2]...{WHEN OBJECT-1 [ALSO OBJECT-2}...}[WHEN OTHER {IMPERATIVE-STATEMENT-2}...]

[END-EVALUATE]

A list of subjects and several lists of objects are associated with an Evaluate.

A list of subjects is specified between the word EVALUATE and the first appearance of WHEN

Subject can be an Identifier / Literal /Expression or key words True / False

Each WHEN specifies a list of objects

Number of subjects and number of objects should tally

Subjects and their corresponding objects should be comparable

WHEN phrases are taken up for a “ match “ in the order they appear.

Imperative statements corresponding to the matching WHEN will be executed.

WHEN OTHER phrase is selected only if none of the previous WHEN phrases are selected.

Objects can be Numeric value , Non-numeric value, Range of Numeric values, Range of non-numeric values, Conditional Values, keywords

www.mainframe230.blogspot.com Page 33 date:16.05.2010

Page 34: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

Example1:EVALUATE TRUE

WHEN MONTH = 4 OR 6 OR 9 OR 11MOVE 30 TO NO - OF - DAYS

WHEN MONTH = 2MOVE 28 TO NO - OF - DAYS

WHEN OTHERMOVE 31 TO NO - OF - DAYS

END-EVALUATE.

Example:

EVALUATE PRODUCT-TYPE ALSOCUSTOMER-TYPE

WHEN 1 ALSO ANYMOVE 0 TO COMMISSION

WHEN 2 ALSO 1 THRU 5MOVE 10 TO COMMISSION

WHEN OTHERMOVE 20 TO COMMISSION

END-EVALUATE.

6.5.4. GO TO STATEMENT >>──GO──┬────┬──procedure-name-1────────────────────────────────────>< └─TO─┘

It is an unconditional transferring control to the specified paragraph .

There is no any comparison being made by the system before branching off .

Example GO TO DISPLAY-PARA.

Note: It is advised to avoid GO TO Statements as the structured programming technique does not support unconditional transfer statements.

6.5.5. GO TO WITH DEPENDINGThis verb is used to conditionally transfer the control to elsewhere in the program.

Depending on whether the value of the identifier is 1,2 . . . n , the control is transferred to procedure-name-1, procedure-name-2, . . . procedure-name-n respectively.

If the value of the identifier is anything other than the specified range of 1, 2 . n, the said go to is ineffective and the control is transferred to the next statement.

GO TO PROCEDURE-NAME-1 [ , PROCEDURE-NAME-2 ]...PROCEDURE-NAME-N

DEPENDING ON identifier

The statement transfers control to one of the procedures named in the statement depending on the value of the identifier

The identifier specified in the statement must be a numeric, integral, elementary item.

Example:-

GO TO RECEIPT-PARA, ISSUE-PARA, ADJUSTMENT-PARADEPENDING ON TRANSACTION-TYPE.

www.mainframe230.blogspot.com Page 34 date:16.05.2010

Page 35: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

6.5.6. ALTERThe ALTER statement changes the transfer point specified in a GO TO statement. The ALTER statement encourages the use of unstructured programming practices; the EVALUATE statement provides the same function as the ALTER statement and helps to ensure that your program will be well-structured.

>>──ALTER────────────────────────────────────────────────────────────> <────────────────────────────────────────────────────────┐ >────procedure-name-1──TO──┬────────────┬──procedure-name-2─┴───────>< └─PROCEED TO─┘

The ALTER statement modifies the GO TO statement in the paragraph named by procedure-name-1. Subsequent executions of the modified GO TO statement(s) transfer control to procedure-name-2. procedure-name-1 Must name a Procedure Division paragraph that contains only one sentence: a GO TO statement without the DEPENDING ON phrase. procedure-name-2 Must name a Procedure Division section or paragraph. Before the ALTER statement is executed, when control reaches the paragraph specified in procedure-name-1, the GO TO statement transfers control to the paragraph specified in the GO TO statement. After execution of the ALTER statement, however, the next time control reaches the paragraph specified in procedure-name-1, the GO TO statement transfers control to the paragraph specified in procedure-name-2. The ALTER statement acts as a program switch, allowing, for example, one sequence of execution during initialization and another sequence during the bulk of file processing.

6.6. PERFORM STATEMENTSA PERFORM statement is used to execute a group of consecutive statements specified elsewhere in the program, under a paragraph.

6.6.1. BASIC PERFORM

PERFORM PARA-name-1.

Para-name-1 specifies the range, which contains statements to be executed

Upon execution of this statement the control is transferred to the beginning of the paragraph and executes all the statements in that paragraph till the last statement.

After executing the said range, the control is implicitly, transferred to the next line after the PERFORM statement

Example:

100-MAIN-PARA.PERFORM 1000-ACCEPT-PARA.PERFORM 2000-COMPUTE-PARA.

www.mainframe230.blogspot.com Page 35 date:16.05.2010

Page 36: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

PERFORM para-name-1 thru para-name-2.

This statement executes all the statements beginning in the para-name-1 till the last statement in the para-name-2

If there are any other paragraphs placed in between these two paragraphs, those are also executed

After executing the said range, the control is implicitly, transferred to the next line after the PERFORM statement

Example:

PERFORM BEGIN-PARA THRU END-PARA.

6.6.2. PERFORM WITH TIMES PHRASE

PERFORM para-name-1 [THRU para-name-2] {identifier-1/ integer} TIMES.

This is a looping statement that executes the specified range of statements a fixed no. of times.

Example:

PERFORM READ-RTN 10 TIMES.

This statement executes all the stmts. in READ-RTN 10 times without testing any condition.

6.6.3. PERFORM WITH UNTIL PHRASEPERFORM para-name-1 [THRU para-name-2] UNTIL condition.

This statement executes a series of instructions in para-name-1 repeatedly till the condition becomes TRUE.

Initially the condition is expected to be false.

The condition should be made true, within the paragraph being performed.

If the condition is true initially, the range is not executed at all

Example:

MOVE 1 TO I. PERFORM 000-CALC-PARA UNTIL I>10

000-CALC-PARA. --

ADD 1 TO I.

Here 000-CALC-PARA is performed 10 TIMES.

www.mainframe230.blogspot.com Page 36 date:16.05.2010

Page 37: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

6.6.4. PERFORM WITH VARYING PHRASE

┌─── Format 4 ─────────────────────────────────────────────────────────────────────────────────────┐│ ││ >>──PERFORM────────────────────────────────────────────────────────────────────────────────────> ││ ││ >──┬─procedure-name-1──┬───────────────────────────────┬──┤ phrase 1 ├──┬──────────────┬─┬────>< ││ │ └─┬─THROUGH─┬──procedure-name-2─┘ └─┤ phrase 2 ├─┘ │ ││ │ └─THRU────┘ │ ││ │ (1) │ ││ └─┤ phrase 1 ├──imperative-statement-1─────END-PERFORM────────────────────────────────┘ ││ ││ phrase 1: ││ ├──┬────────────────────────────┬──VARYING──┬─identifier-2─┬──FROM──┬─identifier-3─┬──BY───────> ││ └─┬──────┬──TEST──┬─BEFORE─┬─┘ └─index-name-1─┘ ├─index-name-2─┤ ││ └─WITH─┘ └─AFTER──┘ └─literal-1────┘ ││ ││ >──┬─identifier-4─┬──UNTIL──condition-1────────────────────────────────────────────────────────┤ ││ └─literal-2────┘ ││ ││ phrase 2: ││ <───────────────────────────────────────────────────────────────┐ ││ ├────AFTER──┬─identifier-5─┬──FROM──┬─identifier-6─┬──┤ phrase 3 ├─┴───────────────────────────┤ ││ └─index-name-3─┘ ├─index-name-4─┤ ││ └─literal-3────┘ ││ ││ phrase 3: ││ ├──BY──┬─identifier-7─┬──UNTIL──condition-2────────────────────────────────────────────────────┤ ││ └─literal-4────┘ ││ ││ Note: ││ (1) Imperative-statement-1 is optional as an IBM extension. ││ │└──────────────────────────────────────────────────────────────────────────────────────────────────┘

This is also a looping statement which executes imperative statement for all possible values of identifier-1 and identifier-3 until Condition-1 and Condition-2 are TRUE.

Example:

PERFORM VARYING I FROM 1 BY 1 UNTIL I > 3AFTER J FROM 1 BY 1 UNTIL J > 4 Display SALE ( I , J )

END-PERFORM.

The above example will take I and j vales as

1,1 1,2 1,3 1,4 2,1 2,2 2,3 2,4 3,1 3,2 3,3 3,4

6.7. CONDITIONAL EXPRESSIONSIn COBOL there are various types of conditions as follows:

RELATIONAL CONDITIONSIGN CONDITIONCLASS CONDITIONCONDITION - NAME CONDITIONNEGATED SIMPLE CONDITIONCOMPOUND CONDITION

www.mainframe230.blogspot.com Page 37 date:16.05.2010

Page 38: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

6.7.1. RELATIONAL CONDITIONA relation condition compares two operands, either of which can be an identifier, literal, arithmetic expression, or index-name. A non-numeric literal can be enclosed in parentheses within a relation condition.

The relational operators are < <= > >=

NOT GREATER THAN / LESSER THAN / EQUAL TO

IF A > BDISPLAY “ A IS GREATER THAN B “

ELSEDISPLAY “ A IS LESS THAN OR EQUAL TO B “

END-IF.

6.7.2. SIGN CONDITIONThe sign condition determines whether or not the algebraic value of a numeric operand is greater than, less than, or equal to zero. The Possible SIGN conditions are POSITIVE / NEGATIVE / ZERO.

Example:

IF CURR-STOCK-ISS-QTY IS NEGATIVE DISPLAY “ ISSUE CANNOT BE MADE”GO TO NO-STOCK-PARA

END-IF.

6.7.3. CLASS CONDITIONThe class condition determines whether the content of a data item is alphabetic,

Alphabetic-lower, alphabetic-upper, numeric, or contains only the characters in the set of characters specified by the CLASS clause as defined in the SPECIAL-NAMES paragraph of the Environment Division.

The possible CLASS Conditions are ALPHABETIC / NUMERIC / ALPHANUMERIC

Example:

IF A IS NUMERICADD 1 TO A

ELSEDISPLAY “ NON - NUMERIC DATA FOR A “GO TO ERROR-PARA

END-IF.

6.7.4. CONDITION-NAME CONDITIONA condition-name condition tests a conditional variable to determine whether its value is equal to any value(s) associated with the condition-name.

Condition names are not memory locations but names assigned to data ranges .

Comparisons can be simplified.

Example:

01 WS-MARITAL-STATUS PIC 9.88 SINGLE VALUE IS 0.

www.mainframe230.blogspot.com Page 38 date:16.05.2010

Page 39: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

88 MARRIED VALUE IS 1.IF SINGLE

ADD 1 TO SINGLE-COUNT.

6.7.5. NEGATED SIMPLE CONDITIONA simple condition is negated through the use of the logical operator NOT.

Example:

IF NOT MARRIEDDISPLAY “ELIGIBLE FOR RS 1000 ONLY “

END-IF.

6.7.6. COMPOUND CONDITIONTwo or more conditions can be logically connected to form a compound condition.

The possible Compound Conditions are AND OR NOT .

Example:

IF AGE IS LESS THAN 18 AND MARRIED DISPLAY “EARLY MARRIAGE “

END-IF.

www.mainframe230.blogspot.com Page 39 date:16.05.2010

Page 40: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

7. FILE HANDLINGA data file is a collection of relevant records of an application.

A record is a collection of relevant fields pertaining to an item/case/ account/transaction .

File is organized in storage media in one of the following ways

SEQUENTIAL

INDEXED

RELATIVE

7.1. SEQUENTIAL FILESIf a file organization is Sequential then

Records can be accessed in the order in which they appear in the file

Records can be appended at the end of the file but cannot be inserted.

Records cannot be deleted

Records can be updated

7.2. INDEXED FILESIndexed File Organization is to do random Processing or sequential processing.

In this Organization

Record can be accessed in any order by key called Primary Key.

There can be more than one key called Alternate Keys to access records

Records can be inserted added, deleted and updated.

7.3. DIRECT ACCESS FILESThis record is best suited if there are records which can be accessed on record number basis rather than based on a field of the record.

Each record has a unique address and is identified by a relative record number (8 digits numeric character).

7.4.STEPS INVOLVED IN FILE HANDLINGTo handle a file the following operations are to done.

Step 1.

All files handled in a program should be assigned a logical name chosen by the programmer as per the syntax similar to Cobol identifier. The organization of the file ,DDNAME assigned for the dataset and its primary and alternate key, if any, have to be specified in the File-Control para of Environment Division. This is specified using SELECT statement of File-Control para.

Step2.

www.mainframe230.blogspot.com Page 40 date:16.05.2010

Page 41: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

Describe the File with detail like record name ,record structure in the File Section of DATA DIVISION .Also specify as how many records are grouped as a BLOCK for Input Output Operation.

Step3.

All files described as explained above should be first opened to do read/write operations. Open statement is used to connect dataset to the Cobol program. These statements are coded in procedure division. Files can be opened for reading, writing, appending.

Step4.

If we have to read a file READ statement is used in procedure Division.

SYNTAX : Read File -name.

If we have to write in to a file WRITE command is used in procedure Division. The record attached to the concerned file alone can be used to write into the file.

SYNTAX : WRITE Record -name.

Step5.

After read / write operations are over the file must be closed to disconnect the file from the Cobol program.

SYNTAX : CLOSE File-name.

7.4.1. EXAMPLE

IDENTIFICATION DIVISION. PROGRAM-ID. F1. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO DD1 ORGANIZATION IS INDEXED ACCESS MODE IS SEQUENTIAL RECORD KEY IS CUST-NO FILE STATUS IS WS-FST1. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE RECORD CONTAINS 80 CHARACTERS DATA RECORD IS CUSTOMER-RECORD. 01 CUSTOMER-RECORD. 05 CUST-NO PIC X(5). 05 CUST-DEPT PIC X(5). 05 CUST-DTLS PIC X(70). WORKING-STORAGE SECTION. 01 WS-FST1 PIC X(2). PROCEDURE DIVISION. 000-MAIN-PARA. OPEN OUTPUT CUSTOMER-FILE PERFORM 100-WRITE-PARA PERFORM 200-CLOSE-PARA. 100-WRITE-PARA. ACCEPT CUST-NO ACCEPT CUST-DEPT ACCEPT CUST-DTLS WRITE CUSTOMER-RECORD

www.mainframe230.blogspot.com Page 41 date:16.05.2010

Page 42: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

DISPLAY 'WRITE FILE STATUS:-' WS-FST1. 200-CLOSE-PARA. CLOSE CUSTOMER-FILE DISPLAY 'CLOSE FILE STATUS:-' WS-FST1 STOP RUN.

www.mainframe230.blogspot.com Page 42 date:16.05.2010

Page 43: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

7.5. SELECT STATEMENT S equential files :

>>──SELECT──┬──────────┬──file-name-1──ASSIGN───┬────┬────assignment-name-1────────> └─OPTIONAL─┘ └─TO─┘ >──┬──────────────────────────────────────┬───────────────────> └─┬──────────────────────┬──SEQUENTIAL─┘ └─ORGANIZATION──┬────┬─┘ └─IS─┘ >──┬──────────────────────────────────────┬────────────────────────────────────────> └─ACCESS──┬──────┬──┬────┬──SEQUENTIAL─┘ └─MODE─┘ └─IS─┘ >──┬────────────────────────────────────────────────────────┬─────────────────────>< └─┬──────┬──STATUS──┬────┬──data-name-1──────────────────┘ └─FILE─┘ └─IS─┘

Indexed File >>──SELECT──┬──────────┬──file-name-1──ASSIGN───┬────┬────assignment-name-1─────────────> └─OPTIONAL─┘ └─TO─┘ >──┬──────────────────────┬──INDEXED──────────────────────────> └─ORGANIZATION──┬────┬─┘ └─IS─┘ >──┬──────────────────────────────────────────┬─────────────────────────────────────────> └─ACCESS──┬──────┬──┬────┬──┬─SEQUENTIAL─┬─┘ └─MODE─┘ └─IS─┘ ├─RANDOM─────┤ └─DYNAMIC────┘ >──RECORD──┬─────┬──┬────┬──data-name-2──┬─────────────────────────────────────┬────────> └─KEY─┘ └─IS─┘ └─ PASSWORD ──┬──────┬── data-name-6 ─┘ └─ IS ─┘ <─────────────────┐ >────┬─────────────┬─┴──┬────────────────────────────────────────────────────────┬──────>< └─┤ entry 1 ├─┘ └─┬──────┬──STATUS──┬────┬──data-name-1──────────────────┘ └─FILE─┘ └─IS─┘ entry 1: (3) ├──ALTERNATE RECORD─────┬─────┬──┬────┬──data-name-3──┬──────────────────────┬───────────> └─KEY─┘ └─IS─┘ └─┬──────┬──DUPLICATES─┘ └─WITH─┘

www.mainframe230.blogspot.com Page 43 date:16.05.2010

Page 44: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

Relative files >>──SELECT──┬──────────┬──file-name-1──ASSIGN───┬────┬────assignment-name-1─────────────> └─OPTIONAL─┘ └─TO─┘

>───┬──────────────────────┬──RELATIVE─────────────────────────> └─ORGANIZATION──┬────┬─┘ └─IS─┘ >─┬──────────────────────────────────────────────────────────────────────────────────┬─> └─ACCESS─┬──────┬─┬────┬─┬─SEQUENTIAL──┬───────────────────────────────────────┬─┬─┘ └─MODE─┘ └─IS─┘ │ └─RELATIVE──┬─────┬─┬────┬──data-name-4─┘ │ │ └─KEY─┘ └─IS─┘ │ └─┬─RANDOM──┬──RELATIVE──┬─────┬─┬────┬──data-name-4────┘ └─DYNAMIC─┘ └─KEY─┘ └─IS─┘ >──┬───────────────────────────────────────────────────────┬──────────────────────────>< └─┬──────┬──STATUS──┬────┬──data-name-1─────────────────┘ └─FILE─┘ └─IS─┘

Assignment Name ties the select statement to the DDNAME in the JCL

QSAM file >>────┬─────┬──name──────────────────────────────────────>< └─S- ─┘

VSAM Sequential File >>────AS- ──name─────────────────────────────────────────><

VSAM Indexed or Relative File >>────name───────────────────────────────────────────────><

7.5.1. ORGANIZATION CLAUSEThis clause should appear in Environment division.

We can choose File Organization

SEQUENTIAL -This Option is to handle a PS data set or VSAM ESDS .

Once the file is organized as Sequential records can be placed as first come first served basis.

INDEXED - This Option is to handle a VSAM KSDS .

There should be a key field to choose this operating mode. This is called primary key and is to be unique like ROLLNO, EMPLYEENO.

RELATIVE - This Option is to handle a VSAM RRDS . Record Numbers are identifying the records

to access.

7.5.2. ACCESS MODE CLAUSEThis clause should appear in the access mode statement of Environment division.

Files can be accessed in different modes based on the type of Organization.

www.mainframe230.blogspot.com Page 44 date:16.05.2010

Page 45: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

We can choose access mode depending on the order in which we like it to be accessed.

Sequential Access Mode will facilitate to access any file records in sequential order.

Random Access Mode will facilitate to access a particular record of an Indexed File / Relative File by supplying the Record key before read / write operation.

Dynamic Access Mode will facilitate to access a particular record or in sequential order from an Indexed File / Relative File

We give below all possible access modes on organization.

Seq. access Random access Dynamic access

Organization

Sequential Order of write Invalid Invalid

Relative Ascending rel rec. no Value of rel key Seq. or random

Indexed Ascending key value Value of rel key Seq. or random

7.5.3. RECORD KEY PHRASEThis Phrase is to be defined in the Environment Division and essential when the file is an Indexed File.

This key is to be Unique and is to be a part of the Indexed record.

7.5.4. ALTERNATE KEY PHRASEThis Phrase is to be defined in the Environment Division when we want to access record in the order of some other Key apart from primary key.

This key need not be unique but to be a part of the record.

7.6. FD ENTRYSyntax Sequential Files

>>──FD──file-name-1─────────────────────────────────────────────────────────> >──┬───────────────────────────────────────────────────────────────────┬────> └─BLOCK──┬──────────┬──┬───────────────┬──integer-2──┬─CHARACTERS─┬─┘ └─CONTAINS─┘ └─integer-1──TO─┘ └─RECORDS────┘ >──┬────────────────────────────────────────────────────────────────────┬───> └─RECORD──┬─┬──────────┬──integer-3──┬────────────┬────────────────┬─┘ │ └─CONTAINS─┘ └─CHARACTERS─┘ │ ├─┬──────────┬──integer-4──TO──integer-5──┬────────────┬─┤ │ └─CONTAINS─┘ └─CHARACTERS─┘ │ └─┤ clause 1 ├──┬────────────────────────────────┬───────┘ └─DEPENDING──┬────┬──data-name-1─┘ └─ON─┘ >──┬──────────────────────────────────────────────────────┬────────────────> └─LABEL───RECORD──┬────┬─────┬─STANDARD──────────────┬─┘ └─IS─┘ └─OMITTED───────────────┘ >──┬─────────────────────────────────────────────┬───────────────────────────> │ <─────────────┐ │ └─DATA──┬─RECORD──┬────┬───┬────data-name-4─┴─┘ │ └─IS─┘ │ └─RECORDS──┬─────┬─┘

www.mainframe230.blogspot.com Page 45 date:16.05.2010

Page 46: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

└─ARE─┘ >──┬───────────────────────────────────────────┬─────────────────────────────> └─ RECORDING ──┬────────┬──┬──────┬── mode ─┘ └─ MODE ─┘ └─ IS ─┘ clause 1: ├──┬────┬──VARYING──┬────┬──┬──────┬──┬─────────────────────┬────────────────> └─IS─┘ └─IN─┘ └─SIZE─┘ └─┬──────┬──integer-6─┘ └─FROM─┘ >──┬───────────────┬──┬────────────┬───────────────────────────────────────── └─TO──integer-7─┘ └─CHARACTERS─┘

Indexed and Relative files >>──FD──file-name-1─────────────────────────────────────────────────────────> >──┬───────────────────────────────────────────────────────────────────┬────> └─BLOCK──┬──────────┬──┬───────────────┬──integer-2──┬─CHARACTERS─┬─┘ └─CONTAINS─┘ └─integer-1──TO─┘ └─RECORDS────┘ >──┬────────────────────────────────────────────────────────────────────┬───> └─RECORD──┬─┬──────────┬──integer-3──┬────────────┬────────────────┬─┘ │ └─CONTAINS─┘ └─CHARACTERS─┘ │ ├─┬──────────┬──integer-4──TO──integer-5──┬────────────┬─┤ │ └─CONTAINS─┘ └─CHARACTERS─┘ │ └─┤ clause 1 ├──┬────────────────────────────────┬───────┘ └─DEPENDING──┬────┬──data-name-1─┘ └─ON─┘ >──┬───────────────────────────────────────────┬────────────────────────────> └─LABEL──┬─RECORD──┬────┬───┬──┬─STANDARD─┬─┘ │ └─IS─┘ │ └─OMITTED──┘ └─RECORDS──┬─────┬─┘ └─ARE─┘ >──┬─────────────────────────────────────────────┬──.──────────────────────>< │ <─────────────┐ │ └─DATA──┬─RECORD──┬────┬───┬────data-name-4─┴─┘ │ └─IS─┘ │ └─RECORDS──┬─────┬─┘ └─ARE─┘ clause 1: ├──┬────┬──VARYING──┬────┬──┬──────┬──┬─────────────────────┬───────────────> └─IS─┘ └─IN─┘ └─SIZE─┘ └─┬──────┬──integer-6─┘ └─FROM─┘ >──┬───────────────┬──┬────────────┬────────────────────────────────────────┤ └─TO──integer-7─┘ └─CHARACTERS─┘

7.6.1. BLOCK CONTAINS / RECORD CONTAINS PHRASEWe can specify the length of the file record in this Clause in terms of Characters this is to be specified in Data Division. We can specify the number of characters / records contains in a Block.

www.mainframe230.blogspot.com Page 46 date:16.05.2010

Page 47: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

7.6.2. RECORDING MODE CLAUSEThis Phrase is to be defined in the File section Data division. When the file is with Fixed length record is chosen as F.

This key represents s only the record Number.

RECORDING MODE [F] [V].

F => Fixed which Indicates that every record of same length

Example:

FD file-name BLOCK CONTAINS 500 CHARACTERS. RECORD CONTAINS 50 CHARACTERS. DATA-RECORD IS EMP-RECORD.EMP-RECORD01 EMPNO PIC X(6).02 EMP-NAME PIC X(30).02 BASIC PIC 9(5).02 HRA PIC 9(5).02 DA PIC 9(4).The above record is of length 50 characters.

V => Variable which Indicates that record length may vary from I1 to I2.

Example:

FD BLOCK CONTAINS 320 THRU 640 CHARACTERS. RECORD CONTAINS 32 THRU 64 CHARACTERS.DATA RECORD IS MARK-RECORD.01 MARK-RECORD02 P1 PIC 99.02 MARKS OCCURS 10 THRU 20 Depending P1

03 SCORE PIC 999.

The above record length is based on P1.

7.7. INPUT / OUTPUT STATEMENTSOpen statement can be used for opening the file with different mode.

OPEN OUTPUT file-name

connects data set to your COBOL program to write records when the file is even with out a single record..

OPEN INPUT file-name

connects data set to your COBOL program to records from the first .

OPEN EXTEND file-name

connects data set to your COBOL program for writing new records in an existing non empty file.

OPEN I-O file-name

connects data set to your COBOL program for processing to do read/write /Rewrite.

www.mainframe230.blogspot.com Page 47 date:16.05.2010

Page 48: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

WRITE

>>──WRITE──record-name-1──┬────────────────────┬────────────────> │ │ └─FROM──identifier-1─┘ ├──┬────────────────────────────────────────────────┬───────────────┤ └─ INVALID ──┬───────┬── imperative-statement-1 ─┘ └─ KEY ─┘ ├──┬────────────────────────────────────────────────────┬───────────┤ └─ NOT INVALID ──┬───────┬── imperative-statement-2 ─┘ └─ KEY ─┘

Add records to a file or load a file . When this command is used in an Indexed file key must be newer one always.

START - Establish the current location in the cluster for a READ NEXT statement

>>──START──file-name-1───────────────────────────────────────────────────────────> >──┬────────────────────────────────────────────────────────────────────────┬────> └─KEY──┬────┬──┬─EQUAL──┬────┬────────────────────────────┬──data-name-1─┘ └─IS─┘ │ └─TO─┘ │ ├─=────────────────────────────────────────┤ ├─ LESS ──┬────────┬───────────────────────┤ │ └─ THAN ─┘ │ ├─ < ──────────────────────────────────────┤ ├─GREATER──┬──────┬────────────────────────┤ │ └─THAN─┘ │ ├─>────────────────────────────────────────┤ ├─NOT LESS──┬──────┬───────────────────────┤ │ └─THAN─┘ │ ├─NOT <────────────────────────────────────┤ ├─ NOT GREATER ──┬────────┬────────────────┤ │ └─ THAN ─┘ │ ├─ NOT > ──────────────────────────────────┤ ├─ LESS ──┬────────┬── OR EQUAL ──┬──────┬─┤ │ └─ THAN ─┘ └─ TO ─┘ │ ├─ <= ─────────────────────────────────────┤ ├─GREATER──┬──────┬──OR EQUAL──┬────┬──────┤ │ └─THAN─┘ └─TO─┘ │ └─>=───────────────────────────────────────┘ >──┬──────────────────────────────────────────┬──────────────────────────────────> └─INVALID──┬─────┬──imperative-statement-1─┘ └─KEY─┘ >──┬──────────────────────────────────────────────┬──┬───────────┬───────────────>< └─NOT INVALID──┬─────┬──imperative-statement-2─┘ └─END-START─┘ └─KEY─┘ START does not retrieve a record, it positions the current record pointer.

READ

Sequential Retreival >>──READ──file-name-1──┬───────────────┬──┬────────┬─────────────────> └─NEXT──────────┘ └─RECORD─┘ >──┬────────────────────┬────────────────────────────────────────────>

www.mainframe230.blogspot.com Page 48 date:16.05.2010

Page 49: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

└─INTO──identifier-1─┘ >──┬─────────────────────────────────────┬───────────────────────────> └─┬────┬──END──imperative-statement-1─┘ └─AT─┘ >──┬──────────────────────────────────────────┬──┬──────────┬───────>< └─NOT──┬────┬──END──imperative-statement-2─┘ └─END-READ─┘ └─AT─┘

Random Retreival >>──READ──file-name-1──┬────────┬──┬────────────────────┬────────────> └─RECORD─┘ └─INTO──identifier-1─┘ >──┬──────────────────────────┬──────────────────────────────────────> └─KEY──┬────┬──data-name-1─┘ └─IS─┘ >──┬──────────────────────────────────────────┬──────────────────────> └─INVALID──┬─────┬──imperative-statement-3─┘ └─KEY─┘ >──┬──────────────────────────────────────────────┬──┬──────────┬───>< └─NOT INVALID──┬─────┬──imperative-statement-4─┘ └─END-READ─┘ └─KEY─┘

The READ statement is used to read a record from a file. If it is used to read a sequential file it will read the next record always. If it used to read from an Indexed/Relative file it would read the corresponding key record, the key field must have a value before read.

If we have read sequentially from an Indexed file READ NEXT RECORD will retrieve records from a file in sequence

REWRITE

>>──REWRITE──record-name-1──┬────────────────────┬───────────────────> └─FROM──identifier-1─┘ >──┬──────────────────────────────────────────┬──────────────────────> └─INVALID──┬─────┬──imperative-statement-1─┘ └─KEY─┘ >──┬──────────────────────────────────────────────┬──────────────────> └─NOT INVALID──┬─────┬──imperative-statement-2─┘ └─KEY─┘ >──┬─────────────┬──────────────────────────────────────────────────>< └─END-REWRITE─┘

Used for Updating records. Files must be opened in I-O mode.

DELETE

>>──DELETE──file-name-1──┬────────┬──────────────────────────────────> └─RECORD─┘ >──┬──────────────────────────────────────────┬──────────────────────>

www.mainframe230.blogspot.com Page 49 date:16.05.2010

Page 50: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

└─INVALID──┬─────┬──imperative-statement-1─┘ └─KEY─┘ >──┬──────────────────────────────────────────────┬──────────────────> └─NOT INVALID──┬─────┬──imperative-statement-2─┘ └─KEY─┘ >──┬────────────┬───────────────────────────────────────────────────>< └─END-DELETE─┘

Remove records from indexed and relative files only. Not permitted for ESDS and PS files. File must be opened in I-O mode.

CLOSE Disconnect the VSAM data set from your program

>>──CLOSE────file-name-1────────><

www.mainframe230.blogspot.com Page 50 date:16.05.2010

Page 51: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

7.8. VSAM DATA SETVSAM – Virtual Storage Access Method

VSAM is high performance access method used to organize, store, catalog, retrieve and delete datasets.

VSAM supports 5 dataset organizations.

Key-Sequenced Data Set (KSDS)

Entry-Sequenced Data Set (ESDS)

Fixed-Length Relative Record Data Set (RRDS)

Variable-Length RRDS (VRRDS)

Linear Data Set (LDS)

The Primary difference of the above types is the way in which their records are stored and retrieved.

COMPARISON ON VSAM DATASETS

Entry - sequenced Key - sequenced Relative - Record

Records are in order in which they are written

Records are in collating sequence by key field

Records are in relative record number order

Access is sequential Access is by key through an index

Access is by relative record number, which is treated like a key

May have one or more alternate indexes

May have one or more alternate indexes.

May not have alternate indexes

A record’s RBA cannot change A record RBA can change A record relative record number cannot change

Space at the end of the data set is used for adding records

Distributed free space is used for inserting record and changing their lengths in place

for fixed length RRDS empty slots in the data set are used for adding records. For variable - length RRDS, distributed free space is used for adding records and changing their lengths in place

A record cannot be deleted but you can reuse its space for a record of the same length

Space given up by a deleted or shortened record is automatically reclaimed within a control interval

Space given up by a deleted record can be reused

Can have spanned records can have spanned records Can be reused as a work file

Can be reused as a work file unless it has an alternate index associated with key ranges or exceeds 123 extents per volume

can be reused as a work file unless it has an alternate index associated with key ranges or exceeds 123 extents per volume

can be reused as work file

7.8.1. VSAM DATASETS AND ACCESS MODES

www.mainframe230.blogspot.com Page 51 date:16.05.2010

File Organization Seq. Access

Ran. Access

Dyna. Access

Fixed Length

Variable Length

VSAM sequential (ESDS)

Yes No No Yes Yes

VSAM indexed (KSDS)

Yes Yes Yes Yes Yes

VSAM Relative (RRDS)

Yes Yes Yes Yes NO

Page 52: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

7.8.1.1. ORGANIZATION CLAUSEORGANIZATION IS SEQUENTIAL - for VSAM sequential files (ESDS)

ORGANIZATION IS INDEXED - for VSAM indexed files (KSDS)

ORGANIZATION IS RELATIVE - for VSAM relative files (RRDS).

7.8.2. ENVIRONMENT DIVISION ENTRIES FOR VSAM FILESExample :VSAM Sequential File

SELECT S-FILE

ASSIGN TO AS-INFILE

ORGANIZATION IS SEQUENTIAL

ACCESS IS SEQUENTIAL

FILE STATUS KEY IS FSTAT-CODE VSTAT-CODE

Note the prefix of AS- for a VSAM ESDS file

Example :VSAM Indexed FileSELECT I-FILE

ASSIGN TO INFILE

ORGANIZATION IS INDEXED

ACCESS IS DYNAMIC

RECORD KEY IS IFILE-RECORD-KEY

ALTERNATE RECORD KEY IS IFILE-ALTREC-KEY

FILE STATUS KEY IS FSTAT-CODE VSTAT-CODE

Example:- VSAM Relative FileSELECT R-FILE

ASSIGN TO INFILE

ORGANIZATION IS RELATIVE

ACCESS IS RANDOM

RELATIVE KEY IS RFILE-RELATIVE-KEY

FILE STATUS KEY IS FSTAT-CODE VSTAT-CODE

7.8.3. COBOL STATEMENTS USABLE WITH SEQUENTIAL FILES

www.mainframe230.blogspot.com Page 52 date:16.05.2010

Page 53: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

7.8.4. COBOL STATEMENTS WITH INDEXED FILES (KSDS) AND RELATIVE (RRDS)

www.mainframe230.blogspot.com Page 53 date:16.05.2010

Access Mode

COBOL / 370

Statement

OPEN

INPUT

OPEN

OUTPUT

OPEN

I-O

OPEN

EXTEND

Sequential OPEN X X X X

WRITE X X

START

READ X X

REWRITE X

DELETE

CLOSE X X X X

Access Mode

COBOL/370Statement

OPEN

INPUT

OPEN

OUTPUT

OPEN

I - O

OPEN

EXTEND

Sequential OPEN X X X X

WRITE X X

START x x

READ X X

REWRITE X

DELETE x

CLOSE X X X X

Random OPEN X X X

WRITE X X

START

READ X X

REWRITE X

DELETE X

CLOSE X X X

Dynamic OPEN X X X

WRITE X X

START X X

READ X X

REWRITE X

DELETE X

CLOSE X X X

Page 54: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

www.mainframe230.blogspot.com Page 54 date:16.05.2010

Page 55: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

7.8.5. PROCEDURE DIVISION STATEMENTS TO UPDATE VSAM FILESESDS KSDS RRDS

ACCESS IS SEQUENTIAL ACCESS IS SEQUENTIAL ACCESS IS SEQUENTIAL

OPEN EXTEND OPEN EXTEND OPEN EXTEND

WRITE WRITE WRITE

CLOSE CLOSE CLOSE

OPEN I-O OPEN I-O OPEN I-O

READ READ READ

REWRITE REWRITE REWRITE

DELETE DELETE

CLOSE CLOSE CLOSE

ACCESS IS RANDOM ACCESS IS RANDOM ACCESS IS RANDOM

OPEN I-O OPEN I-O

READ READ

Not Applicable WRITE WRITE

REWRITE REWRITE

DELETE DELETE

CLOSE CLOSE

ACCESS IS DYNAMIC ACCESS IS DYNAMIC ACCESS IS DYNAMIC

Sequential processing Sequential processing Sequential processing

OPEN I-O OPEN I-O

READ NEXT READ NEXT

Not Applicable WRITE WRITE

REWRITE REWRITE

DELETE DELETE

CLOSE CLOSE

ACCESS IS DYNAMIC ACCESS IS DYNAMIC ACCESS IS DYNAMIC

Random processing Random processing Random processing

OPEN I - O OPEN I - O

READ NEXT READ NEXT

Not Applicable WRITE WRITE

REWRITE REWRITE

DELETE DELETE

CLOSE CLOSE

www.mainframe230.blogspot.com Page 55 date:16.05.2010

Page 56: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

7.8.6. ERROR PROCESSING FOR VSAMThe end - of - file - phrase (AT END) available.

While file is read sequentially AT END CLAUSE is to be used to know whether a record is read.

The EXCEPTION / ERROR declarative statement can be used to trap the errors in any I/O handling.

The FILE STATUS clause (file status key and VSAM return code) can be used in ENVIRONMENT DIVISION to trap standard errors in all FILE Handling

The INVALID KEY phrase can be used to trap the errors while COBOL reads a record at random.

7.8.7. APPLICATIONS BEST SUITED TO VSAM DATASETS

7.8.7.1. ESDS Applications that require sequential access only.

Example : Payroll Processing.

7.8.7.2. KSDSApplications that require that each record have a key field and require both direct and sequential access.

Applications that use High Level Languages which do not support RBA (Relative Byte Address)

Applications that require ALTERNATE KEY to access any record .

Example : BANK ACCOUNT TRANSACTIONS.

7.8.7.3. RRDSApplications that require only Direct Access.

Applications that have one to one correspondence between records.

Applications that require that each record have a key field.

www.mainframe230.blogspot.com Page 56 date:16.05.2010

Page 57: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

8. SUBPROGRAMS Compiled, debugged, stored in library to be called when needed.

The program that calls another program is referred to as the Calling program and the program it calls is referred to as the Called program (subprogram).

Called program may itself call yet another program.

When the called program is completed, the program can either transfer control back to the calling program or end the run unit.

No specific source code statements or options identify a COBOL program to be a main program or a subprogram.

Typical subprograms include:

Edit routines

Error control checks

Standard calculations

Summary, total printing

Advantages of Subprograms Avoids duplication of effort

Improves programmer productivity

Provides greater flexibility

Changes to called program can be made without modifying calling program

Results in greater standardization

Calling and Called Programs

www.mainframe230.blogspot.com Page 57 date:16.05.2010

Page 58: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

1. Subprogram Sub1 called and executed in its entirety

2. Control returns to calling program

3. Subprogram Sub2 called and executed in its entirety

4. Control returns to calling program

5. You can use GOBACK instead of EXIT PROGRAM.

8.1. LINKAGE SECTIONDescribes data made available from another program

Storage for data items within the program is not reserved because the data area exists elsewhere.

Value clause may not be specified for items other than level - 88 items

Example:

IDENTIFICATION DIVISIONPROGRAM-ID. XYZLINKAGE SECTION01 PARAM1 PIC X(05)01 PARAM2 PIC 9(5)PROCEDURE DIVISION USING PARAM1 PARAM2....EXIT PROGRAM.

IDENTIFICATION DIVISIONPROGRAM-ID. ABC......PROCEDURE DIVISION

.CALL XYZ USING A BSTOP RUN.

8.1.1. STATIC / DYNAMIC CALLSTo transfer control from one COBOL program to another COBOL program you can use one of these methods:

STATIC CALL

The Compiler treats the CALL Literal statement (Where literal is the name of a subprogram) as a static call when the NODYNAM compiler option is in effect.

DYNAMIC CALL

A dynamic call loads the subprograms at run time.

Note that the compiler option DYNAM can change a static call to a dynamic call.

8.1.1.1. CALL BY REFERENCE OR BY CONTENT.If you want to pass an identifier’s value only to a called program, specify

CALL . . . BY CONTENT identifier.

If you want to pass a literal value to a called program, specify :

CALL . . . BY CONTENT LENGTH OF Identifier.

www.mainframe230.blogspot.com Page 58 date:16.05.2010

Page 59: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

If you want to pass both item and its length to a subprogram, specify a combination of BY REFERENCE and BY CONTENT

Example:-

CALL ‘ ERRPROC’ USING BY REFERENCE A BY CONTENT LENGTH OF A

8.1.2. PASSING RETURN CODE INFORMATION

You can use the RETURN-CODE special register to pass and receive return codes between programs.

When a COBOL / 370 program returns to its caller, the contents of the RETURN-CODE special register contains the return code of the called program.

When control is returned to a COBOL program from a call, the contents of register 15 are stored into the calling program ‘s RETURN - CODE special register.

When control is returned from a COBOL program to the operating system, the special register contents are returned as a user return code.

www.mainframe230.blogspot.com Page 59 date:16.05.2010

Common Data Items in Subprogram Linkage

Calling Program Description Called Program Description

WORKING-STORAGE SECTION.

01 PARAM-LIST.

05 PARTCODE PIC A.

05 PARTNO PIC X(4).

05 U - SALES PIC 9(5).

PROCEDURE DIVISION

CALL CALLED–PROG USING

PARAM- LIST

LINKAGE SECTION.

01 L-PARAM.

10 PART-ID PIC X(5).

10 SALES PIC 9(5).

PROCEDURE DIVISION USING L-PARAM

In the calling program the code for parts (PARTCODE) and the number (PARTNO) are referred to separately

In the called program, the code for parts and the part number are combined into one data item (PART-ID ). In the called program a reference to PART-ID is the valid reference to them

Page 60: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

9. STRING, UNSTRING AND INSPECT STATEMENTS

9.1. STRING STATEMENTString statement strings together the partial or complete contents of two or more DATA ITEMS or LITERALS into one single data item.

FORMAT :

>>──STRING───────────────────────────────────────────────────────────> <───────────────────────────────────────────────────────────┐ <──────────────────┐ │ >──────┬─identifier-1─┬─┴──DELIMITED──┬────┬──┬─identifier-2─┬─┴─────> └─literal-1────┘ └─BY─┘ ├─literal-2────┤ └─SIZE─────────┘ >──INTO──identifier-3──┬─────────────────────────────────┬───────────> └─┬──────┬──POINTER──identifier-4─┘ └─WITH─┘ >──┬──────────────────────────────────────────┬──────────────────────> └─┬────┬──OVERFLOW──imperative-statement-1─┘ └─ON─┘ >──┬───────────────────────────────────────────────┬─────────────────> └─NOT──┬────┬──OVERFLOW──imperative-statement-2─┘ └─ON─┘ >──┬────────────┬───────────────────────────────────────────────────>< └─END-STRING─┘

Identifiers should be with USAGE clause DISPLAY should not be with picture clause

LITERALS should be non numeric literals / figurative constant except ALL (considers a character non numeric for figurative constant)

THRU (all) should not be with P picture

Identifiers 1 are sending fields, identifier 2 is the delimiting field and Identifiers 3 is the receiving field

DELIMITED BY phrase is used to delimit the pieces of data to be extracted from each input identifier and transferred

SIZE sends the complete sending area.

INTO represents the receiving field

POINTER represents pointer field which represents the last character position in the receiving field.

It should be an integer data item with enough picture clause to hold the count

It should be initialized to 1.

It will return the length of the received data plus 1.

www.mainframe230.blogspot.com Page 60 date:16.05.2010

Page 61: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

OVERFLOW executed when pointer is < 1 or > the picture length of the receiving field

IDENTIFICATION DIVISION. PROGRAM-ID. STRPGM. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 ID1 PIC X(5) VALUE "AB,CD". 01 ID2 PIC X(5) VALUE "E,FGH". 01 ID3 PIC X(5) VALUE "IJK#L". 01 ID4 PIC X(5) VALUE "MN#OP". 01 ID5 PIC X(100). 01 STR-COUNT PIC 999 VALUE 1. PROCEDURE DIVISION. PERFORM PROCESS-PARA PERFORM END-PARA. PROCESS-PARA. STRING ID1 "/" ID2 DELIMITED BY "," "/" ID3 "/" ID4 DELIMITED BY "#" INTO ID5 WITH POINTER STR-COUNT DISPLAY ID5 DISPLAY STR-COUNT. END-PARA. STOP RUN.

Output isAB/E/IJK/MN012

9.2. UNSTRING STATEMENT >>──UNSTRING──identifier-1─────────────────────────────────────────────────────────────────────> >──┬─────────────────────────────────────────────────────────────────────────────────────┬─────> └─DELIMITED──┬────┬──┬─────┬──┬─identifier-2─┬──┬───────────────────────────────────┬─┘ └─BY─┘ └─ALL─┘ └─literal-1────┘ │ <───────────────────────────────┐ │ └───OR──┬─────┬──┬─identifier-3─┬─┴─┘ └─ALL─┘ └─literal-2────┘ >──INTO────────────────────────────────────────────────────────────────────────────────────────> <────────────────────────────────────────────────────────────────────────────────────┐ >────identifier-4──┬─────────────────────────────────┬──┬─────────────────────────────┬─┴──────> └─DELIMITER──┬────┬──identifier-5─┘ └─COUNT──┬────┬──identifier-6─┘ └─IN─┘ └─IN─┘ >──┬─────────────────────────────────┬──┬────────────────────────────────┬─────────────────────> └─┬──────┬──POINTER──identifier-7─┘ └─TALLYING──┬────┬──identifier-8─┘ └─WITH─┘ └─IN─┘ >──┬──────────────────────────────────────────┬────────────────────────────────────────────────> └─┬────┬──OVERFLOW──imperative-statement-1─┘ └─ON─┘ >──┬───────────────────────────────────────────────┬──┬──────────────┬────────────────────────>< └─NOT──┬────┬──OVERFLOW──imperative-statement-2─┘ └─END-UNSTRING─┘ └─ON─┘

Unstring statement causes contiguous data in a Sending Field to be separated and placed into multiple receiving fields.

www.mainframe230.blogspot.com Page 61 date:16.05.2010

Page 62: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

How to interpret the above diagram:-

1)Identifier 1 is the sending field

2)Pieces of the identifier 1 delimited by either identifier 2 or 3 are extracted and sent to the receiving fields which are identifier 4 .

3)Each instance of identifier 4 holds a piece

4)For each instance of identifier 4 you can have a identifier 5 that is set with the delimiter.

5)For each instance of identifier 4 you can have a identifier 6 that is set with the count of bytes in identifier 4 except the delimiter.

6)The pointer identifier 7 is set to the number of bytes examined in the sending field plus 1

7)Identifier 8 is set to the number of receiving data areas that were acted upon.

RestrictionsIdentifiers should be with USAGE clause DISPLAY should not be with P picture

LITERALS should be non numeric literals/figurative constant except ALL

(considers a character non numeric for figurative constant)

Identifier1 must be an alphanumeric item

May not be reference modified. Represents the sending field.

DELIMITED BY phrase is used to limit the data to be transferred .

SIZE sends the complete sending area.

INTO represents the receiving field

POINTER contains a value that indicates a relative position in the sending field.

It should be initialized to 1

It will return the length of the received data plus 1.

All data receiving fields have been acted upon and the sending still contain the unexamined character.

OVER FLOW executed when pointer is < 1 or > the picture length of the receiving field

TALLYING in this count field is increased by the number of data receiving fields acted upon execution.

IDENTIFICATION DIVISION. PROGRAM-ID. STRPGM. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 ID1 PIC X(9) VALUE SPACES. 01 ID1-D PIC X VALUE ",". 01 ID1-C PIC 9 VALUE 0. 01 ID2 PIC X(9) VALUE SPACES. 01 ID2-D PIC X VALUE "#". 01 ID2-C PIC 9 VALUE 0. 01 ID3 PIC X(9) VALUE "IJK#L". 01 ID3-D PIC X VALUE "@". 01 ID3-C PIC 9 VALUE 0.

www.mainframe230.blogspot.com Page 62 date:16.05.2010

Page 63: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

01 ID4 PIC X(9) VALUE SPACES. 01 ID4-C PIC 9 VALUE 0. 01 ID-P PIC 999 VALUE 1. 01 ID-T PIC 999 VALUE 0. 01 DE-ID PIC X VALUE ",". LINKAGE SECTION. 01 JCL-PARM. 02 PARM-L PIC S9(4) COMP. 02 PARM-D PIC X(100). PROCEDURE DIVISION USING JCL-PARM. PERFORM PROCESS-PARA PERFORM END-PARA. PROCESS-PARA. UNSTRING PARM-D DELIMITED BY DE-ID OR ALL "*" INTO ID1 DELIMITER IN ID1-D COUNT IN ID1-C ID2 DELIMITER IN ID2-D COUNT IN ID2-C ID3 DELIMITER IN ID3-D COUNT IN ID3-C ID4 COUNT IN ID4-C WITH POINTER ID-P TALLYING IN ID-T ON OVERFLOW PERFORM FLOW-EXIT-PARA END-UNSTRING DISPLAY "ID1:-" ID1 "ID1-D:-" ID1-D "ID1-C:-" ID1-C DISPLAY "ID2:-" ID2 "ID2-D:-" ID2-D "ID2-C:-" ID2-C DISPLAY "ID3:-" ID3 "ID3-D:-" ID3-D "ID3-C:-" ID3-C DISPLAY "ID4:-" ID4 "ID4-C:-" ID4-C DISPLAY "ID-P:-" ID-P DISPLAY "ID-T:-" ID-T. FLOW-EXIT-PARA. DISPLAY "FLOW-EXIT-PARA EXECUTED". END-PARA. STOP RUN.

OUTPUTID1:-AB ID1-D:-,ID1-C:-2

ID2:-EFGHI ID2-D:-,ID2-C:-5

ID3:-PQRS ID3-D:-*ID3-C:-4

ID4:-GH ID4-C:-5

ID-P:-101

ID-T:-004

9.3. INSPECT STATEMENTThis statement specifies that characters or group of characters in a data item are to be counted or (tallied) or replaced or both.

www.mainframe230.blogspot.com Page 63 date:16.05.2010

Page 64: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

It will count the occurrences of a specific character (ALPHABETIC, NUMERIC OR SPECIAL CHARACTER)

It will fill all portions of data item with specified characters such as spaces or zeroes.

It will convert all occurrences of specific characters in a data item to user supplied replacement characters.

FORMAT1:

>>──INSPECT──identifier-1──TALLYING─────────────────────────────────────────────────────> <───────────────────────────────────────────────────────────────────────────────────┐ <────────────────────────────────────────────────────────────┐ │ <──────────────────┐ │ │ >────identifier-2──FOR────┬─CHARACTERS────┬──────────────┬─┴───────────────────────┬─┴─┴>< │ └─┤ phrase 1 ├─┘ │ │ <───────────────────────────────────────┐ │ │ <──────────────────┐ │ │ └─┬─ALL─────┬────┬─identifier-3─┬───┬──────────────┬─┴─┴─┘ └─LEADING─┘ └─literal-1────┘ └─┤ phrase 1 ├─┘ phrase 1: ├──┬─BEFORE─┬──┬─────────┬──┬─identifier-4─┬─────────────────────────────────────────────┤ └─AFTER──┘ └─INITIAL─┘ └─literal-2────┘

Example:

INSPECT ITEM-1 TALLYING C1 FOR ALL SPACES.INSPECT ITEM-1 TALLYING C2 FOR ALL CHARACTERS.INSPECT ITEM-1 TALLYING C3 FOR LEADING SPACES. DATA ITEM1 BB56BB will yield 4 ,2,1 respectively.

FORMAT 2:>>--INSPECT--identifier-1--REPLACING------------------------------------------------> <-----------------------------------------------------------------------------+ <------------------+ ¦ >------CHARACTERS BY----identifier-5------------------------------------------------>< ¦ +-literal-3----+ +-¦ phrase 1 +-+ ¦ ¦ <----------------------------------------------------------+ ¦ ¦ <--------------+ ¦ ¦ +---ALL------------identifier-3----BY----identifier-5-----------------------+ +-LEADING-¦ +-literal-1----+ +-literal-3----+ +-¦ phrase 1 +-+ +-FIRST---+ phrase 1: +----BEFORE-------------------identifier-4-------------------------------------------¦ +-AFTER--+ +-INITIAL-+ +-literal-2----+

Example:

INSPECT ITEM-4 REPLACING “-“ BY “/“.DATA ITEM1 10-07-97 OUTPUT 20/07/97

www.mainframe230.blogspot.com Page 64 date:16.05.2010

Page 65: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

9.4. DECLARATIVESIt is used for tracing the paragraph or procedures executed by the program as per the Instruction given by the programmer. DEBUG-NAME is a special register storing the name of the paragraph it is executing.

We have to specify the Option " WITH DEBUGGING MODE" option in the SOURCE-COMPUTER

The following rules should be followed.

It is the first Section To Be Defined in Procedure Division.

The rest of the paragraphs may be placed in User defined Section.

The Debug Declarative-paragraph should immediately follow the "USE" instruction.

IDENTIFICATION DIVISION. PROGRAM-ID. DECLRPGM. ENVIRONMENT DIVISION. CONFIGURATION SECTION. SOURCE-COMPUTER. IBM-390 WITH DEBUGGING MODE. OBJECT-COMPUTER. IBM-390. DATA DIVISION. WORKING-STORAGE SECTION. PROCEDURE DIVISION. DECLARATIVES. DEBUG-DECLARATIVES SECTION. USE FOR DEBUGGING ALL PROCEDURES. DEBUG-DECLARATIVES-PARA. DISPLAY "TRACING:-" DEBUG-NAME. END DECLARATIVES. MAIN SECTION. MAIN-PARA. PERFORM PARA-1 PERFORM PARA-2 PERFORM PARA-3 PERFORM END-PARA. PARA-1. DISPLAY "IN PARA-1". PARA-2. DISPLAY "IN PARA-2". PARA-3. DISPLAY "IN PARA-3". END-PARA. STOP RUN.

This declarative section of the program will be executed before the start of execution of each paragraph defined in the program. i.e. "TRACING paraname " will be displayed before the execution of each paragraph.

Make SURE you code PARM=”/DEBUG” in the EXEC JCL card before you run your program!.

www.mainframe230.blogspot.com Page 65 date:16.05.2010

Page 66: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

10. SORT AND MERGE STATEMENTSSORT statement accepts records from one or more files and arranges the records in the ascending /descending order and makes the sorted records available either through an OUTPUT PROCEDURE or in an output file. This statement can appear anywhere in the PROCEDURE DIVISION except in declarative section.

FORMAT 1:

<──────────────────────────────────────────────────┐ <─────────────┐ │ >>──SORT──file-name-1────┬────┬──┬─ASCENDING──┬──┬─────┬────data-name-1─┴─┴─────────> └─ON─┘ └─DESCENDING─┘ └─KEY─┘ >──┬─────────────────────────────────────────┬──────────────────────────────────────> └─┬──────┬──DUPLICATES──┬────┬──┬───────┬─┘ └─WITH─┘ └─IN─┘ └─ORDER─┘ >──┬──────────────────────────────────────────────────┬─────────────────────────────> └─┬───────────┬──SEQUENCE──┬────┬──alphabet-name-1─┘ └─COLLATING─┘ └─IS─┘ <─────────────┐ >──┬─USING────file-name-2─┴───────────────────────────────────────────────────────┬─> └─INPUT PROCEDURE──┬────┬──procedure-name-1──┬───────────────────────────────┬─┘ └─IS─┘ └─┬─THROUGH─┬──procedure-name-2─┘ └─THRU────┘ <─────────────┐ >──┬─GIVING────file-name-3─┴───────────────────────────────────────────────────────┬>< └─OUTPUT PROCEDURE──┬────┬──procedure-name-3──┬───────────────────────────────┬─┘ └─IS─┘ └─┬─THROUGH─┬──procedure-name-4─┘ └─THRU────┘

File-name -1 is the name given in the SD entry that describes the records to be sorted.

data-name-1 specifies a KEY data item on which the SORT statement will be based.

Each data name must identify a data item in a record associated with file-name -1

There can be any number of data names in file-name–1 and each can be either sorted in ascending or descending order.

The first key referred is taken as major key and the next referred is taken as the next significant key and so on

Sorting in ascending order will be done from lowest key value to highest key value.

Sorting in descending order will be done from highest key value to lowest key value.

Duplicates are specified to place the similar records in the order they are accessed.

Alphabet-name1 must be specified in the alphabet clause of SPECIAL-NAMES paragraph.

USING and INPUT-PROCEDURE are mutually exclusive. If USING is specified it means that the sort feature will automatically open file name 2 (s) and read the records as input. You specify an INPUT PROCEDURE when you want to process input records before they are RELEASED to the sort feature for sorting. If you do specify INPUT PROCEDURE it is your responsibility to open input file(s) and read the input records. You must then RELEASE the records via RELEASE to the SORT feature. You do this when the INPUT PROCEDURE is driven.

www.mainframe230.blogspot.com Page 66 date:16.05.2010

Page 67: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

File-name-2 is the actual file to be sorted. Multiple files can specified. You must define this file(s) in the standard way.

Input-Procedure is specified to Select or Modify the Input Record before Sort.

This Procedure can do any modifications in the record and copy Using RELEASE Statement.

The OUTPUT PROCEDURE is mutually exclusive with USING. If you specify USING file name 3 then the file name 3 is automatically opened by the sort feature and used as the sort output file. If you specify OUTPUT PROCEDURE, you must open the output file and write the sorted records when the OUTPUT PROCEDURE is driven. You must define file name 3 in the usual way.

Example with USING

IDENTIFICATION DIVISION. PROGRAM-ID. SORT1. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT SORT-WORK-ONE ASSIGN TO SORTFILE. SELECT INFILE ASSIGN TO INFILE. SELECT OUTFILE ASSIGN TO OUTFILE. DATA DIVISION. FILE SECTION. SD SORT-WORK-ONE RECORD CONTAINS 80 CHARACTERS DATA RECORD SORT-RECORD. 01 SORT-RECORD. 05 SORT-KEY1 PIC X(5). 05 SORT-KEY2 PIC X(5). 05 SORT-DATA PIC X(70). FD INFILE RECORD CONTAINS 80 CHARACTERS BLOCK 800 RECORDING MODE IS F DATA RECORD IS INFILE-RECORD. 01 INFILE-RECORD PIC X(80). FD OUTFILE RECORD CONTAINS 80 CHARACTERS BLOCK 80 RECORDING MODE IS F DATA RECORD IS OUTFILE-RECORD. 01 OUTFILE-RECORD PIC X(80). WORKING-STORAGE SECTION. PROCEDURE DIVISION. PERFORM MAIN-PARA DISPLAY "SORT RC " SORT-RETURN PERFORM END-PARA. MAIN-PARA. SORT SORT-WORK-ONE ON ASCENDING KEY SORT-KEY2 WITH DUPLICATES IN ORDER USING INFILE GIVING OUTFILE. END-PARA. STOP RUN.

Example 2 with INPUT and OUTPUT PROCEDURES

IDENTIFICATION DIVISION. PROGRAM-ID. SORT2. ENVIRONMENT DIVISION.

www.mainframe230.blogspot.com Page 67 date:16.05.2010

Page 68: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

CONFIGURATION SECTION. SOURCE-COMPUTER. IBM-390 WITH DEBUGGING MODE. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT SORTFILE ASSIGN TO SORTFILE. SELECT INFILE ASSIGN TO INFILE FILE STATUS IS INFILE-STAT. SELECT OUTFILE ASSIGN TO OUTFILE FILE STATUS IS OUTFILE-STAT. DATA DIVISION. FILE SECTION. SD SORTFILE RECORD CONTAINS 80 CHARACTERS DATA RECORD SORT-RECORD. 01 SORT-RECORD. 05 SORT-KEY1 PIC X(5). 05 SORT-KEY2 PIC X(5). 05 SORT-DATA PIC X(70). FD INFILE RECORD CONTAINS 80 CHARACTERS BLOCK 800 RECORDING MODE IS F DATA RECORD IS INFILE-RECORD. 01 INFILE-RECORD PIC X(80). FD OUTFILE RECORD CONTAINS 80 CHARACTERS BLOCK 80 RECORDING MODE IS F DATA RECORD IS OUTFILE-RECORD. 01 OUTFILE-RECORD PIC X(80). WORKING-STORAGE SECTION. 01 WS-VARS. 03 INFILE-STAT PIC X(2). 03 OUTFILE-STAT PIC X(2). 03 INFILE-EOF PIC X VALUE "N". 88 INFILEEOF VALUE "Y". 03 OUTFILE-END PIC X VALUE "N". 88 OUTFILEEND VALUE "Y". 03 WS-FLAG PIC X(1) VALUE "0". 03 WS-INFILE-BUFFER PIC X(80). 03 WS-OUTFILE-BUFFER PIC X(80). 03 WS-FLAG PIC X(1) VALUE "0". PROCEDURE DIVISION. PERFORM OPEN-PARA PERFORM MAIN-PARA DISPLAY "SORT RC " SORT-RETURN PERFORM CLOSE-PARA PERFORM END-PARA. OPEN-PARA. OPEN INPUT INFILE OUTPUT OUTFILE IF INFILE-STAT NOT = "00" DISPLAY "INFILE DID NOT OPEN :" INFILE-STAT STOP RUN END-IF IF OUTFILE-STAT NOT = "00" DISPLAY "OUTFILE DID NOT OPEN :" OUTFILE-STAT STOP RUN END-IF.

www.mainframe230.blogspot.com Page 68 date:16.05.2010

Page 69: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

CLOSE-PARA. CLOSE INFILE CLOSE OUTFILE. MAIN-PARA. SORT SORTFILE ON ASCENDING KEY SORT-KEY2 WITH DUPLICATES IN ORDER INPUT PROCEDURE SORT-INPROC OUTPUT PROCEDURE SORT-OUTPROC. SORT-INPROC. PERFORM SORT-READ UNTIL INFILEEOF. SORT-READ. PERFORM READ-INFILE IF NOT INFILEEOF RELEASE SORT-RECORD FROM WS-INFILE-BUFFER. READ-INFILE. D DISPLAY "IN READ" READ INFILE INTO WS-INFILE-BUFFER AT END MOVE "Y" TO INFILE-EOF. SORT-OUTPROC. D DISPLAY "IN OUTPROC" PERFORM SORT-WRITE UNTIL OUTFILEEND. SORT-WRITE. D DISPLAY "IN SORT-WRITE" PERFORM SORT-RETURN-PARA WRITE OUTFILE-RECORD FROM WS-OUTFILE-BUFFER. SORT-RETURN-PARA. D DISPLAY "IN SORT-RETURN" RETURN SORTFILE INTO WS-OUTFILE-BUFFER AT END MOVE "Y" TO OUTFILE-END. END-PARA. STOP RUN.

Notes:

In the above program the sort feature drives the SORT-INPROC (INPUT PROCEDURE) only once. When it is thus driven you read input records one after the other and issue the RELEASE verb to make these records available to sort. When you have no more input you return from the INPUT PROCEDURE SORT-INPROC. The sort feature will store input records you supply in sort work files until your INPUT PROCEDURE returns.

After your INPUT PROCEDURE returns, the sort feature will sort the stored input records. As each output record (sorted) is ready your OUTPUT PROCEDURE is driven. Notice that while the INPUT PROCEDURE is driven only once, the OUTPUT PROCEDURE is driven many times, once for each output record.

MERGE

The MERGE statement is a PROCEDURE DIVISION statement which merges two or more input files on some common key. The merged records are then written to an output file or made available to an output procedure.

www.mainframe230.blogspot.com Page 69 date:16.05.2010

Page 70: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

<──────────────────────────────────────────────────┐

<─────────────┐ │

>>──MERGE──file-name-1────┬────┬──┬─ASCENDING──┬──┬─────┬────data-name-1─┴─┴──────>

└─ON─┘ └─DESCENDING─┘ └─KEY─┘

>──┬──────────────────────────────────────────────────┬──USING──file-name-2───────>

└─┬───────────┬──SEQUENCE──┬────┬──alphabet-name-1─┘

└─COLLATING─┘ └─IS─┘

<─────────────┐

>────file-name-3─┴────────────────────────────────────────────────────────────────>

>──┬─OUTPUT PROCEDURE──┬────┬──procedure-name-1──┬───────────────────────────────┬─┬──><

│ └─IS─┘ └─┬─THROUGH─┬──procedure-name-2─┘ │

│ └─THRU────┘ │

│ <─────────────┐ │

└─GIVING────file-name-4─┴───────────────────────────────────────────────────────┘

File-name -1 is the name given in the SD entry that describes the records to be merged.

File-name-2 and file-name-3 are files to be merged. Multiple files can specified.

File-name-4 is the actual output merged file.

GIVING and POUTPUT-PROCEDURE are mutually exclusive

Other rules remain same as that of SORT statement.

Example

IDENTIFICATION DIVISION. PROGRAM-ID. MERGE1. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT MERGE-WORK-ONE ASSIGN TO MERGFILE. SELECT INFILE1 ASSIGN TO INFILE1. SELECT INFILE2 ASSIGN TO INFILE2. SELECT OUTFILE ASSIGN TO OUTFILE. DATA DIVISION. FILE SECTION. SD MERGE-WORK-ONE RECORD CONTAINS 80 CHARACTERS DATA RECORD MERGE-RECORD. 01 MERGE-RECORD. 05 MERGE-KEY1 PIC X(5). 05 MERGE-KEY2 PIC X(5). 05 MERGE-DATA PIC X(70). FD INFILE1 RECORD CONTAINS 80 CHARACTERS BLOCK 800 RECORDING MODE IS F DATA RECORD IS INFILE1-RECORD. 01 INFILE1-RECORD PIC X(80). FD INFILE2

www.mainframe230.blogspot.com Page 70 date:16.05.2010

Page 71: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

RECORD CONTAINS 80 CHARACTERS BLOCK 80 RECORDING MODE IS F DATA RECORD IS INFILE2-RECORD. 01 INFILE2-RECORD PIC X(80). FD OUTFILE RECORD CONTAINS 80 CHARACTERS BLOCK 800 RECORDING MODE IS F DATA RECORD IS OUTFILE-RECORD. 01 OUTFILE-RECORD PIC X(80). WORKING-STORAGE SECTION. PROCEDURE DIVISION. PERFORM MAIN-PARA DISPLAY "MERGE RC " SORT-RETURN PERFORM END-PARA. MAIN-PARA. MERGE MERGE-WORK-ONE ON ASCENDING KEY MERGE-KEY1 USING INFILE1 INFILE2 GIVING OUTFILE. END-PARA. STOP RUN.

www.mainframe230.blogspot.com Page 71 date:16.05.2010

Page 72: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

11. INTRINSIC FUNCTIONSFrequently Used functions are Listed here for reference.

11.1. NUMBER HANDLINGLENGTH - Returns the length of the Picture Clause

FUNCTION LENGTH(NAME) Will return the Picture Clause value of NAME.

MAX - Returns the content of the argument that contains the maximum value

Ex:- FUNCTION MAX(MARK)

MIN - Returns the content of the argument that contains the minimum value

Ex:- FUNCTION MIN (MARK)

NUMVAL - Returns the numeric value represented by the alphanumeric character string specified in the argument

NUMVAL-C - Returns the numeric value represented by the alphanumeric character string specified in the argument .Any optional Currency sign mentioned in argument 2 and decimal point are missed on conversion.

11.2. DATE / TIMECURRENT-DATE - Returns 21 Chars alphanumeric Value

1..4 Year in the Gregorian Calendar5, 6 month of the year7, 8 Day of the Month9,14 HHMMSS and so on.

DATE-OF-INTEGER - Converts a Date in the Gregorian Calendar from integer date form to standard date form yyyymmdd.

01-01-1601 =1 and 30,67,671=31-12-9999

DAY-OF-INTEGER - Converts a Date in the Gregorian Calendar from integer date form to standard date form yyyyddd

01-01-1601 =1 and 30,67,671=31-12-9999

INTEGER-OF-DATE - Reverse of DATE-OF-INTEGER

INTEGER-OF-DAY - Reverse of DAY-OF-INTEGER

11.3. FINANCEANNUITY FUNCTION ANNUITY(Argument1, Argument2)

Argument1= Rate Of Interest

Argument2 = No.of periods

Formula is Argument1/(1-(1-Argument1)**Argument2

Or 1/argument2 if Argument1=0

PRESENT – VALUE - Returns a Value that approximate the present value of a series of future period-end amounts specified by argument1 at a discount rate argument2

www.mainframe230.blogspot.com Page 72 date:16.05.2010

Page 73: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

11.4. MATHEMATICS & STATISTICSCOS FACTORIAL INTEGER MEAN MEDIAN MIDRANGE RANDOMRANGE STANDARD - DEVIATION

www.mainframe230.blogspot.com Page 73 date:16.05.2010

Page 74: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

12. COMPILER OPTIONSA compiler directing Statement is a statement beginning with a compile directing verb (CBL Process) that causes the compiler to take specific action during compilation.

Compiler options can be coded in the Program itself but the action is controlled in the JCL as follows.

ADV

Instructs the compiler to add one byte to the record length for the printer control character.

The option may be useful to programmers who use WRITE ADVANCING in their source files.

The first character of the record does not have to be explicitly reserved by the programmer

NOADV

Instructs the compiler not to adjust the record length for WRITE.ADVANCING.

The compiler uses the first character of the specified record area to place the printer control character.

The application programmer must ensure that the record description allows for this additional byte.

Default Value is Yes.

LIB

It indicates that the source program contains COPY statements

SOURCE / NOSOURCE

It Indicates the listing of source statements should be included in compiler generated o/p. This listing also includes any statements embedded by COPY

By default SOURCE = YES

COMPILE(n)

Indicates that you want full compilation including diagnostics and object code

NOCOMPILE

indicates that you want only a syntax check

NOC (W) NOC (E) NOC (S)

Specifies an error message level : W is warning ; E is error : S is severe. When an error of the level specified or of a more severe level occurs, compilation stops and only syntax checking is done for the balance of the compilation

Default COMPILE(S)

CURRENCY

Default symbol is the dollar sign ($). This option allows you to define an alternate default currency symbol

DYNAM

causes subprograms that are invoked through the CALL literal statement to be dynamically loaded

Performance consideration : Using DYNAM = YES eases sub-programs maintenance since the application will not have to be relink-edited if the subprogram is changed. However individual applications may experience some performance degradation due to a longer path length but overall system performance may be slightly improved.

NODYNAM

www.mainframe230.blogspot.com Page 74 date:16.05.2010

Page 75: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

Causes the text files of subprograms called with a CALL literal statement to be included with the calling program into a single module file.

See also LIST, MAP and TEST Options

www.mainframe230.blogspot.com Page 75 date:16.05.2010

Page 76: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

13. APPENDIX

13.1. ERROR CODESThe File Status Code attached to a file will have Picture Clause of length 2 digits.

The first digit will denote the general category under which the return code falls.

The second digit will denote the particular type of Error/Message under that category.

The first digit category are as follows :

0 Returned When Successful Open / Read / Write / Operation.

1 Returned When At End Condition Fails

2 Returned When Index Key Fails

3 Returned When Permanent Open Error Occurs.

4 Returned When Logic Error in opening/closing/deleting etc

9 Implementator Defined

0 Successful Open / Read / Write / Operation.

0 Successful Completion

2 A duplicate key was detected.

4 The length of the record just read did not conform to the fixed file attributes for the file.

5 The referenced optional file was not present when the OPEN statement was executed. If the open mode is I-O or EXTEND, the file is created. (This does not apply to VSAM sequential files; file status 0 is returned.)

7 An OPEN or CLOSE statement was executed with a phrase that implies a tape file (i.NO REWIND, REEL/UNIT, or FOR REMOVAL but the referenced file is not a tape file.

1 When At End Condition Fails

0 A sequential READ was attempted on an optional file that was not present or after end of file was reached.

4 A sequential READ was attempted for a relative file and the relative record number is larger than the maximum number that can be stored in the relative key data item.

2 This Error Will Occur When Index Key Fails

1 A sequence error exists for a sequentially accessed indexed file.

2 An attempt was made to WRITE a record that would create a duplicate key.

3 An attempt was made to access a record that does not exist or an optional file that was not present.

4 An attempt was made to WRITE beyond the boundaries for a relative or indexed file.

3 Permanent Open Error Occurs.

4 An attempt was made to WRITE beyond the boundaries of a sequential file.

5 An OPEN statement was attempted on a non-optional file that was not present.

7 An OPEN statement was attempted on a file that would not support the open mode specified in the OPEN statement.

8 An OPEN statement was attempted on a file previously closed with lock.

www.mainframe230.blogspot.com Page 76 date:16.05.2010

Page 77: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

9 An OPEN statement was unsuccessful because of a conflict between the fixed file attributes and the attributes specified for the file in the program.

4 Logic Error in opening/closing/deleting etc.

1 An OPEN statement was attempted for a file in the open mode.

2 A CLOSE was attempted for a file not in the open mode.

3 The last I/O statement executed for the file prior to the execution of a REWRITE or DELETE statement was not a successfully executed READ statement.

4 An attempt was made to REWRITE a record with an invalid length.

6 A sequential READ was attempted but no valid next record had been established.

7 A READ was attempted for a file not open in the input or I-O mode.

8 A WRITE was attempted on a file not open in the I-O, output, or extend mode.

9 A DELETE or REWRITE was attempted on a file not open in the I-O mode.

9 Implemention Defined

0 No further information.

1 VSAM password error.

2 Logic error.

3 VSAM resource not available.

4 No file position indicator for sequential request.

5 Invalid or incomplete VSAM file information.

6 No DD statement specified for VSAM file.

7 File integrity verified for VSAM file.

13.2. EXERCISESThe following problems are only models. You can select similar problems and write programs.

13.2.1. BASIC PROBLEM SOLVING1. Write a program to ACCEPT your name and display the same the number of times you specify.

Follow the Program Coding standard strictly.

2. Write a program to ACCEPT the following details from the user and compute payslip, showing the given details along with GROSS and NET.

EMPLOYEE-NUMBER XXXXX.

EMPLOYEE-NAME X(30).

EMPLOYEE-BASIC 99999.

PERCENTAGE -OF- CCA 99.

PERCENTAGE OF HRA 99.

PERCENTAGE-OF-PF 99.

ANY OTHER ALLOWANCE 9999.

ANY OTHER DEDUCTION 9999.

www.mainframe230.blogspot.com Page 77 date:16.05.2010

Page 78: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

3.Write a program to display the class obtained by the student based on the marks obtained in three subjects as per the following rules.

Basic Condition :.In no subject the boy should have got < 40

Condition For Class III Class : 40 or More but Less than 50

II Class : 50 or More but Less than 60

I Class : 60 or More but Less than 80

DISTINCTION : 80 or More

13.2.2. REPORT PREPARATION

1. For the above programs give a proper report heading and column headings and print the report in the formatted form.

2. Use necessary edited picture clauses. Edit numeric data with comma for clarity.

13.2.3. ARRAY HANDLING

1. Write a program to input the temperature for a week and display the temperature on any day of the week.

2. Find the day on which there was the highest temperature.

3. Expand the Table to two dimensional array to accept week as well as the temperature on any day of the week. Display the temperature detail on Input.

13.2.4. FILE HANDLING

1. Transfer the output of Problem 2 to a PS DATA SET.

2. Write a program to Input the data in a ESDS Dataset and display the Data.

3. Write a program to create a KSDS for the Stock Master File

ITEM NUMBER XXXXX.

ITEM NAME X(30).

BALANCE QUANTITY 99999.

UNIT OF MEASUREMENT XXX.

4. Write a COBOL Program to maintain the above Stock Master and do necessary update process on Issue/Receipt of the Item. Transaction entries may be saved in a PS Data Set.

www.mainframe230.blogspot.com Page 78 date:16.05.2010

Page 79: Cobol Tutorials-Mainframe230.Blogspot.com Full

COBOL Training

13.3. JCLIGYWCL is a catalogued procedure to Compile and Linkedit any COBOL Program.IGYWCLG is a catalogued procedure to Compile, Linkedit and execute any COBOL Program.

JCL for IGYWCL

//&SYSUIDZZ JOB CLASS=B,MSGCLASS=X,MSGLEVEL=(1,1),NOTIFY=&SYSUID,// REGION=2M,TIME=(00,45)//* ----------------------------------------------------------------//* ------------COMPILING & LINK EDITING----------------------------//* ----------------------------------------------------------------//* CHECK JOBNAME AND USERID BEFORE SUBMITTING//* THIS JCL COMPILES AD/CYCLE COBOL/370 PROGRAM SPECIFIED BY//* COB.SYSIN DD NAME AND PRODUCES LOAD MODULE IN THE DATA SET//* MEMBER SPECIFIED BY LKED.SYSLMOD DD NAME .//* ----------------------------------------------------------------//CMPCOB EXEC IGYWCL or IGYWCLG//COBOL.SYSIN DD DSN=&SYSUID.TRG1.COBOL(MEMBER1),DISP=SHR//LKED.SYSLMOD DD DSN=&SYSUID.TRG1.LOAD(MEMBER1),DISP=SHR//

JCL for IGYWCLG

//&SYSUIDZZ JOB CLASS=B,MSGCLASS=X,MSGLEVEL=(1,1),NOTIFY=&SYSUID,// REGION=2M,TIME=(00,45)//* ----------------------------------------------------------------//* ------------COMPILING ,LINK EDITING & EXECUTING----------------//* ----------------------------------------------------------------//* CHECK JOBNAME AND USERID BEFORE SUBMITTING//* THIS JCL COMPILES AD/CYCLE COBOL/370 PROGRAM SPECIFIED BY//* COB.SYSIN DD NAME AND PRODUCES LOAD MODULE IN THE DATA SET//* MEMBER SPECIFIED BY LKED.SYSLMOD DD NAME AND EXECUTES THE/* MEMBER.//* ----------------------------------------------------------------//CMPCOB EXEC IGYWCLG//COBOL.SYSIN DD DSN=&SYSUID.TRG1.COBOL(VTOCLIST),DISP=SHR//LKED.SYSLMOD DD DSN=&SYSUID.TRG1.LOAD(VTOCLIST),DISP=SHR//GO.SYSIN DD */*//

******** End of the Document ********

www.mainframe230.blogspot.com Page 79 date:16.05.2010