FINAL TOTAL LINES

Total lines are set up in Working-Storage in much the same way that headers are set up. The header will contain fields that will print out literal values on the total line and fields that will have accumulated totals moved into them so that the report reader can see the totals. The total lines that we are looking at in this section are final total lines.

To be able to produce final totals at the bottom of the report, the following coding must be added to your program: Accumulators are used to accumulate totals as you process. The accumulators needed in the program must be defined in the Working-Storage Section. If you are accumulating totals from all records, each time a record is read the accumulator is increased. At end of file, the total in the accumulators will therefore be the total from all of the records on the file. Accumulators fall into two categories. The first type is really a counter in that it counts by adding one to the accumulator each time particular processing is done. For example, if you want to count all records read then in the processing routine you should have a statement that adds 1 to an accumulator. This statement should be processed each time a record is read. The second type accumulates the countents of the field. Instead of adding one to the accumulator, the contents of the field on the record are added to the accumulator. For example if the program wants to print the total of the on hand field on each record, then every time a record is read the contents of the on hand field should be added to an accumulator.

If you look at the sample program, there are three accumulators set up in the Working-Storage Section. The first accumulator will count the number of records that are processed. The second accumulator will accumulate the contents of the ON-HAND field for all of the records processed. The third accumulator will accumulate the contents of the ON-ORDER field for all of the records processed.

WORKING-STORAGE SECTION.
...
01  ACCUMULATORS.
    05  TOTAL-RECORDS-ACC         PIC 999          VALUE 0.
    05  TOTAL-ON-HAND-ACC         PIC 9(6)         VALUE 0.
    05  TOTAL-ON-ORDER-ACC        PIC 9(6)         VALUE 0.

The accumulators are added to as each record is processed. The statements in the sample program are located in the paragraph B-200-LOOP:

B-200-LOOP.
    ...
    ...
    ADD 1 TO TOTAL-RECORDS-ACC.
    ADD ON-HAND TO TOTAL-ON-HAND-ACC.
    ADD ON-ORDER TO TOTAL-ON-ORDER-ACC.

Every time a record is read and processed, these three instructions are executed. The totals are gathered as processinng of the file is done.

At the end of file, when processing is complete, control in the sample program passes to C-100-TERMINATE. This paragraph will either contain the instructions to terminate processing or will PERFORM other paragraphs that contain the appropriate instructions. In the sample program, the instructions are in the C-100-TERMINATE paragraph. The first three instructions are MOVE statements which move the contents of the accumulators to the fields on the total line that were set up to hold them. Notice that the fields on the total line are edited with zero suppression and commas to make them more readable.

WORKING-STORAGE SECTION.
...
...
01  TOTAL-LINE.
    05  FILLER                 PIC X(16)
        VALUE " TOTAL RECORDS: ".
    05  TOTAL-RECORDS-ACC-TL   PIC ZZ9.
    05  FILLER                 PIC X(33)      VALUE SPACES.
    05  TOTAL-ON-HAND-ACC-TL   PIC ZZZ,ZZ9.
    05  FILLER                 PIC XXX        VALUE SPACES.
    05  TOTAL-ON-ORDER-ACC-TL  PIC ZZZ,ZZ9.
    05  FILLER                 PIC X(11)      VALUE SPACES.

When I name things, I try to make it easy on my memory. When I set up the accumulators in Working-Storage, I put the -ACC on them to indicate to me that they were accumulators. Now, when I set up the fields to receive these accumulators on the total line, I use the same names, but I add the -TL to indicate that these are on the Total Line. Notice also that there are no VALUE clauses with edited fields. A rule to follow! VALUE clauses are not used in the FILE SECTION (minor acceptions) and they are not used with edited fields.

The final step in printing a final total line is to move the data that has been accumulated to the line and write the line. In the sample final total program, this is done in the C-100-TERMINATE (again, note that it could be done in a separate paragraph performed by the C-100-TERMINATE.

C-100-TERMINATE.
    MOVE TOTAL-RECORDS-ACC TO TOTAL-RECORDS-ACC-TL.
    MOVE TOTAL-ON-HAND-ACC TO TOTAL-ON-HAND-ACC-TL.
    MOVE TOTAL-ON-ORDER-ACC TO TOTAL-ON-ORDER-ACC-TL.
    WRITE PRINTZ FROM TOTAL-LINE
        AFTER ADVANCING 2 LINES.

In this paragraph, the accumulators are moved to the fields on the total line that were set up to receive them. The final total line is then written. Notice that the final total line is written by saying WRITE PRINTZ FROM TOTAL-LINE. This is because the line was setup in WORKING-STORAGE. Lines to be written must be written out of the FILE SECTION using the data record name defined in the 01 level of the print file FD (this is PRINTZ). The WRITE...FROM statement moves the contents of TOTAL-LINE to PRINTZ and then writes PRINTZ. Note that the same thing could have been accomplished by the code:

    MOVE TOTAL-LINE TO PRINTZ.
    WRITE PRINTZ
        AFTER ADVANCING 2 LINES.

One final comment, the after advancing 2 lines means that before the line is written, the printer moves down 2 lines. This leaves a blank line prior to writing the total line.

The whole program being illustrated in this handout, is shown below:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. GRO1.
       AUTHOR. GROCER.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       SELECT INVEN-FILE
           ASSIGN TO "A:\INVEN.DAT".
       SELECT INVEN-REPORT
           ASSIGN TO PRINTER.
       DATA DIVISION.
       FILE SECTION.
       FD  INVEN-FILE
           DATA RECORD IS INVEN-REC.
       01  INVEN-REC.
           05  ITEMNO              PIC X(5).
           05  ITEMNAME            PIC X(15).
           05  ON-HAND             PIC 9(5).
           05  ON-ORDER            PIC 9(5).
           05  REORD-PT            PIC 9(5).
           05  COST                PIC 999V99.
           05  PRICE               PIC 9(4)V99.
           05  FILLER              PIC X(10).
           05  VENDORID            PIC XXX.
       FD  INVEN-REPORT
           DATA RECORD IS PRINTZ.
       01  PRINTZ.
           05  FILLER              PIC X.
           05  ITEMNO-PR           PIC X(5).
           05  FILLER              PIC X(3).
           05  ITEMNAME-PR         PIC X(15).
           05  FILLER              PIC X(3).
           05  ON-HAND-PR          PIC ZZ,ZZ9.
           05  FILLER              PIC X(3).
           05  ON-ORDER-PR         PIC ZZ,ZZ9.
           05  FILLER              PIC X(3).
           05  REORD-PT-PR         PIC ZZ,ZZ9.
           05  FILLER              PIC X(3).
           05  COST-PR             PIC ZZZ.99.
           05  FILLER              PIC X(3).
           05  PRICE-PR            PIC Z,ZZ.99.
           05  FILLER              PIC X(3).
           05  VENDORID-PR         PIC XXX.
           05  FILLER              PIC X(3).
       WORKING-STORAGE SECTION.
       01  INDICATORZ.
           05  EOF-IND             PIC XXX      VALUE SPACES.
       01  ACCUMULATORS.
           05  TOTAL-RECORDS-ACC        PIC 999     VALUE 0.
           05  TOTAL-ON-HAND-ACC        PIC 9(6)    VALUE 0.
           05  TOTAL-ON-ORDER-ACC       PIC 9(6)    VALUE 0.
       01  TOTAL-LINE.
           05  FILLER                 PIC X(16)
               VALUE " TOTAL RECORDS: ".
           05  TOTAL-RECORDS-ACC-TL   PIC ZZ9.
           05  FILLER                 PIC X(33)      VALUE SPACES.
           05  TOTAL-ON-HAND-ACC-TL   PIC ZZZ,ZZ9.
           05  FILLER                 PIC XXX        VALUE SPACES.
           05  TOTAL-ON-ORDER-ACC-TL  PIC ZZZ,ZZ9.
           05  FILLER                 PIC X(11)      VALUE SPACES.
       PROCEDURE DIVISION.
       MAIN-PROGRAM.
           PERFORM A-100-INITIALIZE.
           PERFORM B-100-PROCESS-FILE.
           PERFORM C-100-TERMINATE.
           STOP RUN.
       A-100-INITIALIZE.
           OPEN INPUT INVEN-FILE
                OUTPUT INVEN-REPORT.
       B-100-PROCESS-FILE.
           READ INVEN-FILE
               AT END
                   MOVE "YES" TO EOF-IND.
           PERFORM B-200-PROCESS-RECORD-LOOP
               UNTIL EOF-IND = "YES".
       B-200-PROCESS-RECORD-LOOP.
           MOVE SPACES TO PRINTZ.
           MOVE ITEMNO TO ITEMNO-PR.
           MOVE ITEMNAME TO ITEMNAME-PR.
           MOVE ON-HAND TO ON-HAND-PR.
           MOVE ON-ORDER TO ON-ORDER-PR.
           MOVE REORD-PT TO REORD-PT-PR.
           MOVE COST TO COST-PR.
           MOVE PRICE TO PRICE-PR.
           MOVE VENDORID TO VENDORID-PR.
           ADD 1 TO TOTAL-RECORDS-ACC.
           ADD ON-HAND TO TOTAL-ON-HAND-ACC.
           ADD ON-ORDER TO TOTAL-ON-ORDER-ACC
           WRITE PRINTZ
               AFTER ADVANCING 1 LINE.
           READ INVEN-FILE
               AT END
                   MOVE "YES" TO EOF-IND.
       C-100-TERMINATE.
           MOVE TOTAL-RECORDS-ACC TO TOTAL-RECORDS-ACC-TL.
           MOVE TOTAL-ON-HAND-ACC TO TOTAL-ON-HAND-ACC-TL.
           MOVE TOTAL-ON-ORDER-ACC TO TOTAL-ON-ORDER-ACC-TL.
           WRITE PRINTZ FROM TOTAL-LINE
               AFTER ADVANCING 2 LINES.
           CLOSE INVEN-FILE
                 INVEN-REPORT.