COBOL Bible
v1.0.2 ALPHA

Written by Colin Wheeler from various sources

These are notes that I have obtained from various sources on how to write the syntax of COBOL programs.  I initially wrote this  for myself but then decided to make this document public. Feel free to re-distribute this but do not alter it. If I find any errors on this page I will post the error and correct it and update the version number  which is why I wrote a version number above so you can keep track of the updates. This page was written with Mozilla Composer v1.1a(Alpha) and is generally the best browser to view this page with because Mozilla supports more web standards than Internet Explorer. If you find anywhere in this document that I have made an error please compose an e-mail to me @ cdwheeler@mac.com with the subject "COBOL Error" and copy the section of this page that has an error and paste it into the e-mail message and then write what the error is then paste the code or section again and correct it. I will attempt to correct any errors on this page as quickly as possible.  I know that i will probably make many errors but for me finding them and correcting them is more important than anything else. If you think that anything needs to be added to this guide please go to the COMING SOON page and check to see if your idea is in the works if it's not then e-mail it to me or if you think that one of my descriptions doesn't aqeately describe the code or section it's refeering to then e-mail me and let me know if there are enough people who have a problem with a section i will revise it.  Please note that this is the very first post of this webpage and it could very well have a hundred errors and will eventually expand, but for now just notify me of anything wrong with the page.

COBOL Bible v1.0.3 Coming Soon! Some of the fixes/new features are:
* Over 30 various spelling and format errors fixed!
* Fixed some various syntax errors present in the COBOL code
* Fixed the code on this page that caused some text to be disproportional in Internet Explorer (still no probs with Mozilla :-) )
* Finally started work to enhance the page for Mozilla/Netscape users (Mozilla 1.0 and above - Netscape 7.0 preview 1 and above)
* Finished Bolding all Procedure Division Statements

What's new in COBOL Bible v1.0.2 ALPHA? (Updated 12:10am Wednesday July 17)
* Completed Procedure Division Statements
* Added Evaluate Statement Syntax (was previously missing from list of commands)
* Corrected Miscelanious Errors on Page
* Corrected Font Code in HTML so the page can be viewed well on Internet Explorer

Index of COBOL Notes:

I. Book Conventions
II.  Compiler Directives

III. SELECT Statements
IV. File Descriptions

V.  Picture & Value Clauses (Working Storage)
VI.  Procedure Division Statements
VII.  Validation Tecniques
VIII. Table Usage
IX. End Notes

I. Book Conventions
In order for you to get the most out of this page this book will assume a couple of things. The first assumption is that you have a general understanding of COBOL and want to know more and make sure that the syntax you use it correct to better correct errors. Eventually this page might be expanded to include sections on how to build better and more efficient COBOL programs and if I receive suggestions on anything else I might include too. The next thing is that code is shown as such:
SELECT DISK-FILE
ASSIGN TO 'C:\Documents and Settings\User\file.dat'
ORGANIZATION IS LINE SEQUENTIAL.
In some cases I may show code that is like this...
CARD-FILE
RECORD CONTAINS 80 CHARACTERS

01 CARD-RECORD.
05 CARD-CODE PIC X(25).
...
...
05 CARD-NAME PIC X(15).
When I put "..." on a line as such that means that you can put more code in the section as necessary. When I enter code like this "CARD-CODE" that means that it is a variable name or section of code that you can change as needed. Please note though that in some instances some variable names depend on another defined name at which I will note it within or after the code. Please note that with this document i am trying to provide the framework for your syntax and in a lot of cases you can move parts of the code I have provided around or substitute different statements other than the ones i have provided. These examples are here to show you ways of doing your syntax that you might not have even been aware you could do but they are hardly the limit of what you can do with your syntax . I encourage you to look up other websites that involve COBOL syntax examples and see what they have also. Knowledge is Power after all.

II. Compiler Directives
COBOL uses compiler directives to specify options that affect the way the compiler behaves, what output is produced, what code is actually compiled, and how the compiled code behaves when run. Compiler directives are set by using the "$SET" statement in a Microfocus COBOL program. Microfocus COBOL has default directives that can be overridden with the "$SET" statement. When using the "$SET" statement it should be the first statement in the program.

$SET statement in the Microfocus Environment

$SET OSVS NOFORM CHECKDIV"OSVS"

OSVS - Indicates the COBOL dialect used for compilation. The dialect will be IBM/OS/VS COBOL for most compilations unless the application is directed at a database or CICS environment.

NOFORM - Restricts the inserion of form feed characters into the program listing. Omitting NOFORM causes blank pages to be embedded in the printed program listing.

CHECKDIV"OSVS" - Causes a runtime error to occur if your program tries to divide by Zero in a statemtent  that has no "ON SIZE ERROR" clause encoded.

The VSE and OS COBOL evironment also has default directives that can be overridden or added to with a COBOL option statemtent called a "CBL" statement. It must be coded as the first source statement in a VSE or OS COBOL program.

III. SELECT Statements
Line Sequential disk file

SELECT DISK-FILE
ASSIGN TO '/students/testdata/testfile.dat'
ORGANIZATION IS LINE SEQUENTIAL.

Record sequential disk file:
SELECT DESK-FILE
ASSIGN TO '/students/testdata/prtfile.prt'
ORGANIZATION IS RECORD SEQUENTIAL.

Indexed sequential disk file:
SELECT DISK-FILE
ASSIGN TO '/students/testdata/indxfile.dat'
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS INDEX-KEY
FILE STATUS IS RESPONSE-CODE.
Please note that later on i will give more explanation to this section as for now i just want to post this information.

IV. File Descriptions
File descriptions begin in column 8 with each subsequent ssubordinate level being indented 4 positions to the right of the prior level . PICTURE clauses should all be aligned in the same colmn throughout the program. Seperate the file description (FD) from the record description with either a blank line or a comment line.

FD  CARD-FILE
LABEL RECORD IS OMITTED
DATA RECORD IS CARD-RECORD CARD-REC
RECORD CONTAINS 80 CHARACTERS.

01 CARD-RECORD.
05 CARD-CODE PIC X.
... ...
10 CARD-HOUSE-NUM PIC X(25).
... ...

FD PRT-FILE
LABEL RECORD IS OMITTED
DATA RECORD IS PRT-RECORD
RECORD CONTAINS 132 CHARACTERS.

01 PRT-RECORD PIC X(132).
 
V. Picture and Value Clauses (Working Storage)
WORKING-STORAGE entries follow the same conventions as the file desription entries that 01 levels begin in column 8 with subsequent subordinate entries being indented 4 positions from the prior entry. PICTURE, VALUE, COMP, and COMP-3 clauses should all be alligned in the same columns throughout the program. If the initial value in the VALUE clause is too long to be coded on the same line as the PICTURE, the VALUE clause is continued on the next line and indented 4 positions. Subordinate entry name should begin with a common prefix to make indentification of the group level easier.

01 FLD1                    PICTURE IS X.
01 FLD2 PIC X USAGE IS DISPLAY.

** Note ** The PICTURE clause is normally abbreviated to PIC and the USAGE clause is normally omitted since the default usage
is DISPLAY.

01 H1.
05 PIC X VALUE SPACES. (There is no name here because this part of the line is blank
... ... ...
... ... ...
05 PIC X(12) VALUE "REPORT TITLE".
**Note** The number of characters on each line must equal the amount on the printer file

01 PERSONELL-REC.
05 PERS-NUM PIC 999.
05 PERS-NAME PIC X(25).
... ...
66 Level Items:
01  EMPLOYEE-RECORD.
05 EMPLOYEE-LNAME PIC X(26).
05 EMPLOYEE-FNAME PIC X(26).
05 EMPLOYEE-BIRTH-DATE.
10 EMPLOYEE-BIRTH-DAY PIC 99.
10 EMPLOYEE-BIRTH-MON PIC 99.
66 EMPLOYEE-FULL-NAME RENAMES
EMPLOYEE-LNAME THRU EMPLOYEE-FNAME.
77 Level Items:
77  IO-STATUS              PIC XX  VALUE SPACES.
77 IO-LITERAL PIC X(29) VALUE "TOTALS FOR SECTION".
88 Level Items:
01 WOR-REC.
05 WK-GRADE PIC 99.
88 K6 VALUE 1 THRU 6.
88 MIDSCHL VALUE 7 8.
88 HIGH-SCHOOL VALUE 9 THRU 12.
88 GRADE-ERROR VALUE 00 13 THRU 99.

VI. The Procedure Division Statements

All Procedure Division Statements must begin in column 12 or higher although in the new version of COBOL coming out shortly the column requirements will go away and you will have the freedom to code wherever you want.  Currently paragraph names should begin in column 8 and should be segnificant. Each paragraph must begin with the level number of the paragraph (ex "1000-MAINLINE".) If a EXIT statement is necessary it should look like such " 1000-X. EXIT." 

Procedure Division Statements:

- 1. ACCEPT/ DISPLAY -
ACCEPT WORK-DATE FROM DATE.

**Note** : "WORK-DATE" should be defined in the working storage and DATE is supplied by the operating system you are using ( Mac OS X, Windows XP, etc.) ACCEPT causes the program to 'stop' and allows you to enter data on console and will not continue with the execution of the program until the user presses the Enter key. This allows you to communicate with the terminal (user) and enter data into the program.

ACCEPT FILENAME FROM CONSOLE.

This statement will receive information into the program and places it into a data-item defined in the program. FILENAME is desined in the working storage section.

DISPLAY 'message here' UPON CONSOLE

** Note that the "UPON CONSOLE" section is not required in this statement **

DISPLAY PAGE-CTR 
This causes  the value of PAGE-CTR to be displayed

- 2. ADD -

(1)ADD SUB-BAL TO TOTAL-BAL.
(2)ADD SUB-BAL SLS-BAL SSLS-BAL TO TOT-BAL
(3)ADD SUB-BAL SLS-BAL SSLS-BAL GIVING TOT-BAL
(4)ADD INPUT-BAL TO TOT-BAL ROUNDED
ON SIZE ERROR
MOVE 0 TOT TOT-BAL
DISPLAY 'ERROR IN ADDITION'.

(1) Adds "SUB-BAL" to "TOTAL-BAL"
(2) Adds "SUB-BAL", "SLS-BAL","SSLS-BAL" to "TOT-BAL"
(3) Adds "SUB-BAL","SLS-BAL","SSLS-BAL" using the fiield defined for "TOT-BAL"
(4) Adds "INPUT-BAL" to "TOT-BAL" rounding the number. If the field is too big  it will give an error

- 3. CALL -

CALL 'PAYCAL' USING PARM1
PARM2
PARM3
.


- 4. CLOSE -
CLOSE PRT-FILE.
CARD-FILE.
INPUT-FILE
.

CLOSE PRT-FILE CARD-FILE INPUT-FILE.


- 5. COMPUTE -

(1)COMPUTE BALANCE=(INTRST * MBAL/SBAL)
(2)COMPUTE BALANCE=(INTRST * MBAL/SBAL) / 365.
(3)COMPUTE BALANCE ROUNDED = (INTRST * MPAY)/ INTRST
ON SIZE ERROR
MOVE 0 TO BALANCE
DISPLAY 'ERROR ON COMPUTE'.

- 6 . DELETE -

DELETE TABLE-FILE.

This form of the delete statement has to follow a sucsessful read before the delete statement can be performed. If the read is not performed then the delete statement will not be performed and in an interactive COBOL program you would need to generate an error message.

- 7. DIVIDE -

(1)DIVIDE 100 INTO INT-RATE.
(2)DIVIDE MONTHS INTO INT-RATE GIVING MONTH-RATE.
(3)DIVIDE INTO-RATE BY MONTHS GIVING MONTH-RATE.

- 8. EVALUATE -

EVALUATE YEARS-IN-COLLEGE-IN
WHEN 1 PERFORM 300-FRESHMAN-RTN
WHEN 2 PERFORM 400-SOPHMORE-TRN
WHEN 3 PERFORM 500-JUNIOR-RTN
WHEN 4 PERFORM 600-SENIOR-RTN
WHEN OTHER PERFORM 700-ERR-RTN
END EVALUATE

- 8. EXAMINE -

EXAMINE statements replace one value with another value. Example below:
EXAMINE PRT-FLD REPLACING ALL ',' BY '/'.

- 9. IF... ELSE -

(1)IF RATE-CODE EQUAL 'A'
MOVE '9' TO RATE.
(2)IF RATE-CODE EQUAL 'A'
MOVE '9' TO RATE.
ELSE
MOVE '7' TO RATE.
WRITE PRT-REC FROM D1
AT EOP
PERFORM 4000-HDG.
GO TO 4000-EXIT.

10. If...Else  If...Else

IF SALES LESS 500
COMPUTE COMMISSSION = .02 * SALES
ELSE
IF SALES LESS 5000
COMPUTE COMISSION = .05 * SALES
ELSE
COMPUTE COMISSION = .1 * SALES.

11. IF IF... ELSE ELSE

IF SALES GREATER 499.99
IF SALES GREATER 4999.99
COMPUTE COMISSION = .1 * SALES
...
ELSE
COMPUTE COMISSION = .05 * SALES
...
ELSE
COMPUTE COMISSION = .05 * SALES


12. INSPECT

-INSPECT FLD3 TALLYING CTR1 FOR ALL ','.
-INSPECT FLD3 REPLACING ALL ',' BY '/'.

13. MOVE

MOVE INPUT-BAL TO PRT-BAL.
MOVE ZERO TO REC-CTR LINE-CTR PAGE-CTR
MOVE ZERO TO REC-CTR
LINE-CTR
PAGE-CTR
.

14. MOVE CORRESPONDING

01 CURR-DATE.
05 DD PIC 99.
05 PIC X.
05 MM PIC 99.
05 PIC X.
05 YY PIC 9999.
01 SWT-DATE.
05 YYYY PIC 9999.
05 PIC X.
05 MM PIC 99.
05 PIC X.
05 DD PIC 99.

MOVE CORRESPONDING CURR-DATE TO SWT-DATE.
MOVE CORR CURR-DATE TO SWT-DATE.
**Note** If identically named field must be referenced, the group level of the field must be identified with an OF clause
ex:
MOVE DD OF CURR-DATE TO WORK-DD.

15. MULTIPLY

MULTIPLY INPUT-HRS BY INPUT-RATE.

MULTIPLY INPUT-HRS BY INPUT-RATE GIVING GROSS-PAY.

MULTIPLY INPUT-HRS BY INPUT-RATE
GIVING GROSS-PAY ROUNDED
ON SIZE ERROR
MOVE 'Y' TO ERROR-SWT
DISPLAY 'ERROR ON MULTIPLY'.

16. OPEN

OPEN INPUT CARD-FILE OUTPUT PRT-FILE ISAM-FILE.

OPEN INPUT CARD-FILE
TAPE-FILE
VSAM-FILE
.
OPEN OUTPUT PRT-FILE.

17. PERFORM

PERFORM 2000-WRITE.

PERFORM 2000-WRITE 4 TIMES.

PERFORM 2000-WRITE THRU 2000-X.


18. PERFORM UNTIL:

PERFORM 2000-PRT
UNTIL PAGE-CTR GREATER THAN 100.

PERFORM 2000-PRT THRU 2000-X.
UNTIL MORE-CARDS EQUAL 'N'.

PERFORM 4100-READ THRU 4100-X
UNTIL MORE-CARD EQUAL 'N'
OR CARD-CODE EQUAL 3.

PERFORM 4100-READ THRU 4100-X
UNTIL MORE-CARD EQUAL 'N'
OR CARD-CODE EQUAL 3
OR CARD-CTR GREATER 100.

19. PERFORM VARYING:

PERFORM 1100-CLEAR
VARYING SUB FROM 1 BY 1
UNTIL SUB GREATER 100.

PERFORM 4100-PRT THRU 4100-X
VARYING EMP-NO FROM 10 BY -1
UNTIL LIST(EMP-NO) EQUAL 'ADAMSON'
OR EMP-NO EQUAL 1.

20. PERFORM VARYING ...AFTER:

PERFORM 1100-FIND THRU 1100-X
VARYING ROW-SUB FROM 1 BY 1
UNTIL ROW-SUB GREATER 10
AFTER COL-SUB FROM 1 BY 1
UNTIL COL-SUB GREATER 3
AFTER DEPTH-SUB FROM 1 BY 1
UNTIL DEPTH-SUB GREATER THAN 5.

21. READ:

READ CARD-FILE

READ CARD-FILE INTO EMP-CARD.

READ CARD-FILE INTO EMP-CARD
AT END
MOVE 'N' TO MORE-RECS
GO TO 4100-X.

Read Random Access - Indexed Sequential File - Primary Key

READ VSAM-FILE INTO EMP-CARD
INVALID KEY
MOVE 'Y' TO ERROR-SWT
DISPLAY 'ERROR ON RANDOM READ'.

Read Random Access - Indexed Sequential File - Alternate Index

READ VSAM-FILE INTO EMP-CARD
KEY IS VSAM-ALTERNATE-KEY-FIELD
INVALID KEY
MOVE 'Y' TO ERROR-SWT
DISPLAY 'ERROR ON READOM READ'.

22. RELEASE:

RELEASE SORT-RECORD.

RELEASE SORT-RECORD FROM CARD-RECORD.

23. RETURN:

RETURN SORT-FILE.

RETURN SORT-FILE INTO WK-RECORD.

RETURN SORT-FILE INTO WK-RECORD
AT END
MOVE 'N' TO MORE-SORT-RECS
GO TO 1200-X.

24. REWRITE:

REWRITE VSAM-REC.

REWRITE VSAM-REC FROM WK-REC.

REWRITE VSAM-REC FROM WK-REC
INVALID KEY
MOVE 'Y' TO ERROR-SWT
DISPLAY 'ERROR ON REWRITE'.

25. SEARCH:

01 GRADE-AREA.
05 GRADE-TABLE OCCURS 25 TIMES INDEXED BY INDX.
10 GRADE-NAME PIC X(10).
10 GRADE-NUM PIC 999.

SET INDX TO 1.
SEARCH GRADE-TABLE
AT END
MOVE 'DONE' TO SEARCH-STATUS
WHEN WK-NAME GREATER GRADE-NAME(INDX)
MOVE 'FOUND' TO SEARCH-NAME-SWT.

SET INDX TO 1.
SEARCH GRADE-TABLE
AT END
MOVE 'DONE' TO SEARCH-STATUS
WHEN WK-NAME GREATER GRADE-NAME(INDX)
MOVE 'FOUND' TO SEARCH-NAME-SWT
WHEN WK-NUM EQUAL GRADE-NUM(INDX)
MOVE 'FOUND' TO SEARCH-NUM-SWT.

26. SORT:

SORT SORT-FILE ASCENDING KEY SORT-SSN
SORT-LNAME
SORT-FNAME
USING INPUT-FILE
GIVING OUTPUT-FILE.

SORT SORT-FILE ASCENDING KEY SORT-SSN
SORT-LNAME
SORT-FNAME
DESCENDING KEY SORT-ACT-BAL
ASCENDING KEY SORT-TRAN-DATE
INPUT PROCEDURE IS 4100-READ THRU 4100-X
OUTPUT PROCEDURE IS 4200-PRT THRU 4200-X.

27. START:

START VSAM-FILE
INVALID KEY
DISPLAY 'NO RECORD FOUND WITH MATCHING KEY'

START VSAM-FILE
KEY GREATER THAN EMP-NAME
INVALID KEY
DISPLAY 'NO RECORD FOUND WITH REQUESTED KEY'.

28. STRING:

STRING 'A' 'B' 'C' DELMITED BY SIZE
INTO REC-FLD.

STRING FNAME DELMITED BY ' '
SPACE DELMITED BY SIZE
LNAME DELMITED BY SPACE
INTO PRT-NAME.

STRING FNAME
MNAME
SPACE DELMITED BY SIZE
LNAME DELMITED BY SPACE
INTO PRT-NAME
WITH POINTER BEGIN-POSITION
ON OVERFLOW
DISPLAY 'STRUNG TOO MUCH'.

29. SUBTRACT:

SUBTRACT INPUT-BAL FROM TOT-BAL.

SUBTRACT INPUT-BAL INPUT-DEP INPUT-INT FROM TOT-BAL.

SUBTRACT INPUT-BAL FROM TOT-BAL ROUNDED
ON SIZE ERROR
MOVE 0 TO TOT-BAL
DISPLAY 'ERROR IN SUBTRACTION'.

30. TRANSFORM:

TRANSFORM FLD3 CHARACTERS FROM ',' TO '-'

31. WRITE

WRITE PRT-REC
AFTER ADVANCING 2 LINES.

WRITE PRT-REC FROM H1
AFTER AVDANCING PAGE.

WRITE PRT-REC
AT EOP
PERFORM 4100-HDGS.

WRITE PRT-REC
AFTER ADVANCING 4 LINES
AT EOP
PERFORM 4100-HDGS.

Sequential Disk

WRITE SEQ-DISK-REC FROM WK-REC
INVALID KEY
PERFORM 4300-ERR
GO TO 2000-X.

Indexed Sequential Disk:

WRITE INDX-SEQ-DISK-REC FROM WK-REC
INVALID KEY
PERFORM 4300-ERR
GO TO 2000-X.