Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 38 ยท Projects and Practice

Project: A Payroll Report

Build a complete COBOL batch program step by step - read a sorted employee file, compute overtime and tax with a SEARCH table, print a formatted report with department subtotals, grand totals and error handling.

This project ties the course together in one realistic batch program. It reads a file of employee time records already sorted by department, computes each person's pay with overtime and a tax bracket, prints a formatted report, and shows a subtotal at every change of department plus a grand total. Along the way it uses sequential files, file status checks, a table with SEARCH and the classic control break pattern.

You can run the COBOL below with GnuCOBOL (cobc -x payroll.cob) if you have it installed (setup). On this page the program is shown read-only, and a Python stand-in runs the same logic so you can see the report it produces.

The requirements

  1. Input: employees.dat, one record per employee, sorted by department: department (2 characters), employee id (5 digits), name (15), hours worked (999V9) and hourly rate (999V99).
  2. Hours above 40 are paid at 1.5 times the rate.
  3. Tax is a percentage of gross pay taken from a bracket table: up to 500.00 pays 10 percent, up to 1,000.00 pays 15, up to 2,000.00 pays 20, anything above pays 30. (A real tax calculation is more complex; the flat bracket keeps the example short.)
  4. Output: a report with a heading per department, one detail line per employee (gross, tax, net), a subtotal line per department and a grand total.
  5. If the input file cannot be opened or a read fails, print the file status and end with return code 16.

Design first

flowchart TD
    A["Open files"] --> B["Read first record"]
    B --> C{"More records?"}
    C -->|"yes"| D{"Department changed?"}
    D -->|"yes"| E["Print subtotal of previous dept<br/>start new heading"]
    D -->|"no"| F["Calculate pay"]
    E --> F
    F --> G["Print detail line<br/>add to totals"]
    G --> H["Read next record"]
    H --> C
    C -->|"no"| I["Print last subtotal<br/>and grand total"]
    I --> J["Close files, end"]

Read ahead, compare the department to the previous one, and handle the last group after the loop ends.

The most common bugs in control-break programs are forgetting the final group's subtotal after the loop and printing a bogus subtotal before the first group. The program below handles both with a WS-PREV-DEPT that starts as spaces.

The data layout

The files and the input record come first:

COBOL
       FILE-CONTROL.
           SELECT EMP-FILE ASSIGN TO "employees.dat"
               ORGANIZATION IS LINE SEQUENTIAL
               FILE STATUS IS WS-EMP-STATUS.
           SELECT REPORT-FILE ASSIGN TO "payroll.rpt"
               ORGANIZATION IS LINE SEQUENTIAL.
       ...
       FD  EMP-FILE.
       01  EMP-REC.
           05  EMP-DEPT    PIC XX.
           05  EMP-ID      PIC 9(5).
           05  EMP-NAME    PIC X(15).
           05  EMP-HOURS   PIC 9(3)V9.
           05  EMP-RATE    PIC 9(3)V99.
       FD  REPORT-FILE.
       01  REPORT-LINE     PIC X(60).

Then the working storage:

COBOL
       WORKING-STORAGE SECTION.
       01  WS-EMP-STATUS       PIC XX.
           88  EMP-OK          VALUE "00".
           88  EMP-EOF         VALUE "10".
       01  WS-PREV-DEPT        PIC XX    VALUE SPACES.
       01  WS-REG-HOURS        PIC 9(3)V9.
       01  WS-OT-HOURS         PIC 9(3)V9.
       01  WS-GROSS            PIC 9(6)V99.
       01  WS-TAX              PIC 9(6)V99.
       01  WS-NET              PIC 9(6)V99.
       01  WS-DEPT-TOTALS.
           05  WS-DEPT-GROSS   PIC 9(8)V99 VALUE ZERO.
           05  WS-DEPT-TAX     PIC 9(8)V99 VALUE ZERO.
           05  WS-DEPT-NET     PIC 9(8)V99 VALUE ZERO.
       01  WS-GRAND-TOTALS.
           05  WS-GRAND-GROSS  PIC 9(8)V99 VALUE ZERO.
           05  WS-GRAND-TAX    PIC 9(8)V99 VALUE ZERO.
           05  WS-GRAND-NET    PIC 9(8)V99 VALUE ZERO.

       01  TAX-BRACKET-VALUES.
           05  FILLER  PIC 9(6)V99  VALUE 000500.00.
           05  FILLER  PIC V99      VALUE .10.
           05  FILLER  PIC 9(6)V99  VALUE 001000.00.
           05  FILLER  PIC V99      VALUE .15.
           05  FILLER  PIC 9(6)V99  VALUE 002000.00.
           05  FILLER  PIC V99      VALUE .20.
           05  FILLER  PIC 9(6)V99  VALUE 999999.99.
           05  FILLER  PIC V99      VALUE .30.
       01  TAX-TABLE REDEFINES TAX-BRACKET-VALUES.
           05  TAX-BRACKET OCCURS 4 TIMES INDEXED BY TX-IX.
               10  TB-LIMIT    PIC 9(6)V99.
               10  TB-RATE     PIC V99.

The bracket table is built with the REDEFINES trick from tables with OCCURS. Note the amounts are V99 with implied decimal points: a VALUE of .10 on PIC V99 is stored as the digits 10.

The report lines use edited pictures: Z replaces leading zeros by spaces, , and . insert punctuation:

COBOL
       01  DETAIL-LINE.
           05  DL-ID           PIC 9(5).
           05  FILLER          PIC XX     VALUE SPACES.
           05  DL-NAME         PIC X(15).
           05  FILLER          PIC XX     VALUE SPACES.
           05  DL-GROSS        PIC ZZZ,ZZ9.99.
           05  FILLER          PIC XX     VALUE SPACES.
           05  DL-TAX          PIC ZZZ,ZZ9.99.
           05  FILLER          PIC XX     VALUE SPACES.
           05  DL-NET          PIC ZZZ,ZZ9.99.

An edited field can be written but not used in arithmetic: always calculate in a numeric field and MOVE it to the edited one.

The procedure division

COBOL
       PROCEDURE DIVISION.
       MAIN.
           OPEN INPUT EMP-FILE
           IF NOT EMP-OK
               DISPLAY "Cannot open employees.dat, status " WS-EMP-STATUS
               MOVE 16 TO RETURN-CODE
               STOP RUN
           END-IF
           OPEN OUTPUT REPORT-FILE
           PERFORM READ-EMP
           PERFORM UNTIL EMP-EOF
               IF EMP-DEPT NOT = WS-PREV-DEPT
                   IF WS-PREV-DEPT NOT = SPACES
                       PERFORM DEPT-BREAK
                   END-IF
                   MOVE EMP-DEPT TO WS-PREV-DEPT
                   PERFORM DEPT-HEADING
               END-IF
               PERFORM CALC-PAY
               PERFORM WRITE-DETAIL
               PERFORM READ-EMP
           END-PERFORM
           IF WS-PREV-DEPT NOT = SPACES
               PERFORM DEPT-BREAK
           END-IF
           PERFORM GRAND-TOTAL
           CLOSE EMP-FILE REPORT-FILE
           STOP RUN.

       READ-EMP.
           READ EMP-FILE
           EVALUATE TRUE
               WHEN EMP-OK   CONTINUE
               WHEN EMP-EOF  CONTINUE
               WHEN OTHER
                   DISPLAY "Read error, status " WS-EMP-STATUS
                   MOVE 16 TO RETURN-CODE
                   STOP RUN
           END-EVALUATE.

       CALC-PAY.
           IF EMP-HOURS > 40
               MOVE 40 TO WS-REG-HOURS
               COMPUTE WS-OT-HOURS = EMP-HOURS - 40
           ELSE
               MOVE EMP-HOURS TO WS-REG-HOURS
               MOVE ZERO TO WS-OT-HOURS
           END-IF
           COMPUTE WS-GROSS ROUNDED =
               WS-REG-HOURS * EMP-RATE + WS-OT-HOURS * EMP-RATE * 1.5
           SET TX-IX TO 1
           SEARCH TAX-BRACKET
               AT END
                   MOVE ZERO TO WS-TAX
               WHEN WS-GROSS <= TB-LIMIT (TX-IX)
                   COMPUTE WS-TAX ROUNDED = WS-GROSS * TB-RATE (TX-IX)
           END-SEARCH
           COMPUTE WS-NET = WS-GROSS - WS-TAX.

       WRITE-DETAIL.
           MOVE EMP-ID    TO DL-ID
           MOVE EMP-NAME  TO DL-NAME
           MOVE WS-GROSS  TO DL-GROSS
           MOVE WS-TAX    TO DL-TAX
           MOVE WS-NET    TO DL-NET
           WRITE REPORT-LINE FROM DETAIL-LINE
           ADD WS-GROSS TO WS-DEPT-GROSS
           ADD WS-TAX   TO WS-DEPT-TAX
           ADD WS-NET   TO WS-DEPT-NET.

(DEPT-HEADING, DEPT-BREAK and GRAND-TOTAL follow the same pattern: build a line, WRITE it, add the department totals to the grand totals, and zero the department fields. The full source is a good exercise.)

Check the decisions in this code:

  • Read ahead: READ-EMP is performed once before the loop and again at the bottom.
  • Status handling: FILE STATUS is declared, so end of file is simply status 10, and any other non-zero status is an error with return code 16.
  • Numeric fields for calculation, edited fields for display.
  • COMPUTE ... ROUNDED: without it COBOL truncates the extra decimals.
  • SEARCH has AT END and the index is set to 1 first. The last bracket has a limit of 999999.99 so it always matches.

Seeing the report

This Python stand-in processes the same records with Decimal arithmetic and the same control-break structure. It is labelled as a model, not COBOL, but its numbers are what the COBOL program computes.

Python Stand-in: the payroll batch with control break (Decimal)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

You should see ENG with gross 2,000.00 and 1,900.00 (department total 3,900.00, tax 780.00, net 3,120.00), OPS with 770.00, 1,210.00 and 300.00 (total 2,280.00, tax 387.50, net 1,892.50), and a grand total of 6,180.00 gross, 1,167.50 tax and 5,012.50 net.

Python Stand-in: why the last group needs a final break

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first line lists only department A; the second includes B with 8. That missing last line is the classic control-break bug.

Extending the project

Try these changes, each one teaches a different feature:

  1. Add a page break and column headings every 20 lines (LINAGE or a counter).
  2. Validate records: non-numeric hours or a rate of zero go to an error file instead of the report (file status).
  3. Replace the file input with a sorted version built by a SORT step from an unsorted file.
  4. Store the tax table in a file and load it at the start.
  5. Print an average pay per department and the highest earner.
  6. Run it on a mainframe-style layout: change the files to RECFM=FB datasets and write the JCL.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the program test WS-PREV-DEPT NOT = SPACES before printing a department subtotal?

    On the very first record the previous department is blank, so there is nothing to total yet.
  2. 2. What is needed after the main loop ends?

    The break is only triggered by a change of key, so the last group has to be closed explicitly.
  3. 3. Why is gross pay computed in WS-GROSS and then moved to DL-GROSS?

    A numeric-edited item such as ZZZ,ZZ9.99 is for display; calculations use plain numeric fields.
  4. 4. What does a 40-hour employee at rate 50.00 earn gross?

    40 x 50.00 = 2,000.00 with no overtime hours.

Summary

  • A batch report reads sorted data once, with a read-ahead loop and a control break on the key.
  • Close the last group after the loop; guard the first break with a blank previous key.
  • Use numeric fields for arithmetic, edited fields for printing, ROUNDED where cents matter and a SEARCH table for brackets.
  • Check FILE STATUS and set RETURN-CODE so the job scheduler sees failures.

Next: Project: updating an account master