[USflag] The American Programmer [USflag]


Home
Books on Mainframe Programming
Mainframe Manuals and Tutorials
System Abend codes, Sqlcodes, VSAM/QSAM codes
Everything about the IBM AS/400 Midrange Computer - iSeries
Everything about CICS
Cobol programs, manuals, books
  Sample Cobol code: The Simple, Single File COBOL Program
  Sample Cobol code: The Simple, Single File Report COBOL Program with Record Count or Final Totals
  Sample Cobol code: The Simple, Single File Report COBOL Program with Record Count or Final Totals
  Sample Cobol code: The Sequence Check COBOL Program
  Sample Cobol code: The Record Selection COBOL Program
  Sample Cobol code: The Edit or Validate COBOL Program
  Sample Cobol code: The The One Level Subtotal (Control Break) COBOL Program
  Sample Cobol code: The Three Level Subtotal (Control Break) COBOL Program
  Sample Cobol code: The Sequential File, Batch Update COBOL Program
  Sample Cobol code: The COBOL Sort
  Sample Cobol code: The CASE Structure: COBOL EVALUATE
  Sample Cobol code: Direct Subscripting in COBOL
  Sample Cobol code: The Sequential, or Serial Search in COBOL
  Sample Cobol code: The Binary Search in COBOL
  Sample Cobol code: Loading a Table from a Sequential File in a COBOL program
  Sample Cobol code: The VSAM File Read Sequentially in a COBOL program
  Sample Cobol code: The VSAM KSDS, Read Randomly in a COBOL program
  Sample Cobol code: The VSAM File, Read Randomly in a COBOL program
  Sample Cobol code: VSAM Initial Load in a COBOL program
  Sample Cobol code: VSAM File Maintenance (Add, Change, Delete) in a COBOL program
  Sample Cobol code: VSAM Read Sequentially, with START, in a COBOL program
  Sample Cobol code: Creating a Variable Format File in a COBOL program
  Sample Cobol code: Reading a Variable Format File in a COBOL program
  Sample Cobol code: Creating a Variable Format File with Occurs Depending On in a COBOL program
  Sample Cobol code: COBOL Reading a Variable Format File with Occurs Depending On, in a COBOL program
  Sample Cobol code: COBOL The Table Load with Occurs Depending On
  Sample Cobol/DB2 code: Singleton Select
  Manuals on the COBOL programming language.
  Books on Cobol
  Abend Codes from Cobol programs
Everything about DB2 and SQL
Everything about IMS
Everything about Java and JavaScript
Everything about JCL and JES
Everything about REXX
Everything about zOS, VSAM, Tivoli, Assembler
Everything about TSO, ISPF, Spufi
Site Map and Site Search

           Home   > COBOL   > The One Level Subtotal (Control Break) Program

The One Level Subtotal (Control Break) COBOL Program


7. The One Level Subtotal (Control Break) Program

BRKLV1.
Similar to The Simple, Single File Report Program with record count or final totals, #3 above, but with a big difference: it checks for a “break” (the control break) in some field, such as account number, and when the break occurs prints out subtotals for the amounts pertaining to the field. At end of the file, it prints out totals of the amounts (grand or final totals).

Now we are getting into the Real World! One large, all-inclusive grand total may not be detailed enough. Each department of the company may want to see just its totals: enter the program that shows subtotals by department. Each department head can inspect just the part of the report that interests her or him, while the chief financial officer will flip through the report to the last page to devour the grand all-inclusive totals, and know whether to declare Chapter 11 or not.

Here is the program BRKLV1:

Compliments of Gabe Gargiulo, author of several recent books on programming and modern languages, available at Amazon.com.



000200  IDENTIFICATION DIVISION.
000300* 1 level control break - on state
000400* this is an excellent example of a 1 level control break
000500* (subtotal) program.  Use it as a model when writing yours.
000600 PROGRAM-ID. BRKLV1.
000700 ENVIRONMENT DIVISION.
000800 CONFIGURATION SECTION.
000900 INPUT-OUTPUT SECTION.
001000 FILE-CONTROL.
001100*    INPUT FILE IS SALES1
001200     SELECT SALES-FILE  ASSIGN SALES1.
001500*  OUTPUT FILE IS SENT TO THE PRINTER
001600     SELECT REPORT-FILE  ASSIGN REPORTFI.
001900
002000 DATA DIVISION.
002100 FILE SECTION.
002200*
002300 FD  SALES-FILE
002400     RECORDING MODE IS F
002700     RECORD CONTAINS  80 CHARACTERS.
002800*
002900 01  SALES-RECORD.
003000*   Program assumes that input data is sorted by state
003100*   state is the field that the program "breaks" on
003200     05  SR-STATE             PIC X(02).
003300     05  FILLER               PIC X(03).
003400     05  SR-BRANCH            PIC X(10).
003500     05  SR-SALESPERSON       PIC X(15).
003600     05  FILLER               PIC X(05).
003700     05  SR-ITEM-SOLD         PIC X(15).
003800     05  FILLER               PIC X(05).
003900     05  SR-AMOUNT-SOLD       PIC S9(7)V99.
004000     05  FILLER               PIC X(16).
004100*
004200 FD  REPORT-FILE
004300     RECORDING MODE IS F
004600     RECORD CONTAINS 133 CHARACTERS.
004700*
004800 01  REPORT-RECORD             PIC X(133).
004900*
005000 WORKING-STORAGE SECTION.
005100 01  SWITCHES.
005200     05  FILE-AT-END              PIC X  VALUE 'N'.
005300*
005400 01  ACCUMULATORS.
005500     05  GRAND-TOTAL-ACCUM PIC S9(9)V99 PACKED-DECIMAL VALUE +0.
005600     05  STATE-ACCUM       PIC S9(9)V99 PACKED-DECIMAL VALUE +0.
005700*
005800 01  LINE-AND-PAGE-CTRS.
005900     05  LINE-CTR                    PIC S9(3)
006000                PACKED-DECIMAL VALUE +0.
006100     05  MAX-LINES-PER-PAGE          PIC S9(3)
006200                PACKED-DECIMAL VALUE +40.
006300     05  PAGE-CTR                    PIC S9(3)
006400             PACKED-DECIMAL VALUE +0.
006500*
006600 01  SAVE-AREAS.
006700*   AN AREA TO HOLD THE FIELD THAT THE PROGRAM 'BREAKS' ON
006800*   IT MUST HAVE SAME PICTURE AS THE ITEM IT HOLDS
006900     05  SAVE-STATE               PIC X(02) VALUE SPACES.
007000*
007100 01  PRINT-LINES.
007200     05  GRAND-TOTAL-LINE.
007300       10  FILLER               PIC X(02) VALUE SPACES.
007400       10  FILLER               PIC X(20)
007500           VALUE '*** GRAND TOTAL ***'.
007600       10  FILLER               PIC X(43) VALUE SPACES.
007700       10  GTL-AMOUNT-SOLD      PIC  Z(9).99-.
007800*
007900     05  STATE-TOTAL-LINE.
008000       10  FILLER               PIC X(08) VALUE SPACES.
008100       10  FILLER               PIC X(20)
008200           VALUE '*** TOTAL FOR ***'.
008300       10  STL-STATE            PIC X(02) VALUE SPACES.
008400       10  FILLER               PIC X(22) VALUE SPACES.
008500       10  STL-AMOUNT-SOLD      PIC  Z(9).99-.
008600**
008700     05  DETAIL-PRINT-LINE.
008800     10  FILLER               PIC X(05) VALUE SPACES.
008900     10  DPL-STATE            PIC X(02) VALUE SPACES.
009000     10  FILLER               PIC X(05) VALUE SPACES.
009100     10  DPL-BRANCH           PIC X(10) VALUE SPACES.
009200     10  FILLER               PIC X(06) VALUE SPACES.
009300     10  DPL-SALESPERSON      PIC X(15) VALUE SPACES.
009400     10  FILLER               PIC X(05) VALUE SPACES.
009500     10  DPL-ITEM-SOLD        PIC X(15) VALUE SPACES.
009600     10  FILLER               PIC X(04) VALUE SPACES.
009700     10  DPL-AMOUNT-SOLD      PIC  Z(7).99-.
009800**
009900 01   BLANK-LINE.
010000     05  FILLER                  PIC X(133) VALUE SPACES.
010100**
010200 01   HEADING-LINE-1.
010300     05  FILLER                  PIC X(30) VALUE SPACES.
010400     05  FILLER                  PIC X(40)
010500     VALUE 'MODEL CONTROL BREAK REPORT'.
010600     05  FILLER                  PIC X(50) VALUE SPACES.
010700     05  HL1-PAGE                PIC ZZZ.
010800**
010900 01   HEADING-LINE-2.
011000     05  FILLER                  PIC X(2) VALUE SPACES.
011100     05  FILLER                  PIC X(10)
011200         VALUE 'STATE'.
011300     05  FILLER                  PIC X(16)
011400         VALUE 'BRANCH'.
011500     05  FILLER                  PIC X(20)
011600         VALUE 'SALESPERSON'.
011700**
011800 PROCEDURE DIVISION.
011900     PERFORM INITIALIZATION
012000     PERFORM PRODUCE-THE-REPORT
012100     UNTIL FILE-AT-END = 'Y'
012200     PERFORM TERMINATION
012300     GOBACK.
012400**
012500 INITIALIZATION.
012600     OPEN INPUT SALES-FILE
012700     OPEN OUTPUT REPORT-FILE
012800     PERFORM READ-A-RECORD
012900**   SAVE CONTROL FIELD
013000     MOVE SR-STATE       TO SAVE-STATE
013100     MOVE ZEROS TO GTL-AMOUNT-SOLD
013200     MOVE ZEROS TO STL-AMOUNT-SOLD
013300     MOVE ZEROS TO DPL-AMOUNT-SOLD
013400     PERFORM PRINT-HEADER-LINES.
013500**
013600 PRODUCE-THE-REPORT.
013700**
013800     PERFORM DETAIL-PROCESSING
013900     PERFORM READ-A-RECORD.
014000
014100 DETAIL-PROCESSING.
014200**   IF THERE IS A CHANGE IN THE CONTROL FIELD,
014300**       PERFORM THE CONTROL BREAK ROUTINE
014400     IF SR-STATE       IS NOT EQUAL TO SAVE-STATE
014500     THEN PERFORM STATE-BREAK
014600     END-IF
014700**   THE FOLLOWING IS KNOWN AS DETAIL PROCESSING
014800**   ADD INPUT AMOUNT TO FIRST LEVEL ACCUMULATOR
014900**   COUNT INPUT RECORDS
015000**   PRINT A DETAIL LINE
015100**   ADD DETAIL AMOUNT   TO LOWEST LEVEL (STATE) TOTAL.
015200     ADD SR-AMOUNT-SOLD  TO STATE-ACCUM
015300**   FORMAT DETAIL LINE FOR PRINTING.
015400**   PRINT  DETAIL LINE.
015500     MOVE SR-STATE       TO DPL-STATE
015600     MOVE SR-BRANCH      TO DPL-BRANCH
015700     MOVE SR-SALESPERSON TO DPL-SALESPERSON
015800     MOVE SR-ITEM-SOLD   TO DPL-ITEM-SOLD
015900     MOVE SR-AMOUNT-SOLD TO DPL-AMOUNT-SOLD
016000     WRITE REPORT-RECORD FROM DETAIL-PRINT-LINE
016100         AFTER ADVANCING 1 LINE
016200     ADD 1 TO LINE-CTR
016300     IF LINE-CTR  IS GREATER THAN MAX-LINES-PER-PAGE
016400     THEN PERFORM PRINT-HEADER-LINES
016500     END-IF.
016600**
016700 STATE-BREAK.
016800**   WRITE OUT THIS LEVEL TOTALS (STATE)
016900**   ADD THIS LEVEL TOTAL (STATE) TO GRAND TOTAL
017000**   ZERO OUT THIS LEVEL TOTALS (STATE)
017100**   SAVE THIS LEVEL INPUT CONTROL FIELD (SAVE INPUT STATE)
017200**
017300     PERFORM PRINT-STATE-TOTALS
017600     ADD STATE-ACCUM TO GRAND-TOTAL-ACCUM
017610     MOVE ZERO TO STATE-ACCUM
017620     MOVE SR-STATE TO SAVE-STATE.
017700**
017800 PRINT-STATE-TOTALS.
017900     MOVE SAVE-STATE TO STL-STATE
018000     MOVE STATE-ACCUM TO STL-AMOUNT-SOLD
018100     WRITE REPORT-RECORD FROM STATE-TOTAL-LINE
018200         AFTER ADVANCING 3 LINES
018300     WRITE REPORT-RECORD FROM BLANK-LINE
018400     AFTER ADVANCING 2 LINES
018500     ADD 5 TO LINE-CTR
018600     IF LINE-CTR     IS GREATER THAN MAX-LINES-PER-PAGE
018700     THEN PERFORM PRINT-HEADER-LINES
018800     END-IF.
018900**
019000 PRINT-GRAND-TOTALS.
019100     MOVE GRAND-TOTAL-ACCUM TO GTL-AMOUNT-SOLD
019200     WRITE REPORT-RECORD FROM GRAND-TOTAL-LINE
019300         AFTER ADVANCING 3 LINES.
019400* *
019500 PRINT-HEADER-LINES.
019600     ADD  1 TO PAGE-CTR
019700     MOVE PAGE-CTR TO HL1-PAGE
019800     MOVE 0 TO LINE-CTR
019900     WRITE REPORT-RECORD FROM HEADING-LINE-1
020000         AFTER ADVANCING PAGE
020100     WRITE REPORT-RECORD FROM HEADING-LINE-2
020200         AFTER ADVANCING 1 LINE
020300     WRITE REPORT-RECORD FROM BLANK-LINE
020400         AFTER ADVANCING 3 LINES.
020500**
020600 READ-A-RECORD.
020700     READ SALES-FILE
020800     AT END MOVE  'Y' TO FILE-AT-END
021000     END-READ.
021100
022000 TERMINATION.
022100**  HAVE TO PERFORM THE BREAK, BECAUSE END OF FILE IS LIKE A BREA
022200     PERFORM STATE-BREAK
022300**  PRINT FINAL TOTALS, DISPLAY RECORD COUNTS, IF ANY
022400     PERFORM PRINT-GRAND-TOTALS
022500     CLOSE SALES-FILE
022600           REPORT-FILE.

Compliments of Gabe Gargiulo, author of several recent books on programming and modern languages, available at Amazon.com.
Here is the input data file SALES1: (the next two lines are a column ruler) 1 2 3 4 5 6 123456789.123456789.123456789.123456789.123456789.123456789.12345678 CT HARTFORD ANN T. HILL CHEVELLE 000800000 CT HARTFORD SAL MONELLA FERRARI 006000000 CT NEW HAVEN ANNA KONDA OMNI 000030000 CT NEW HAVEN ANNA KONDA STING RAY 000200000 CT NEW HAVEN ANNA KONDA STING RAY 000200000 CT NEW HAVEN ANNA KONDA STING RAY 000200000 CT NEW HAVEN BILL COLLECTOR CAMARO 000600000 CT NEW HAVEN CAL ZONI JAGUAR 009000000 CT NEW HAVEN CAL ZONI PEUGEOT 000200000 CT NEW HAVEN ROSE BUSH PINTO 000040000 MA BOSTON MOE PEDD HONDA 000010000 MA BOSTON RON ZONEY LEMON 000020000 MA BOSTON RON ZONEY SUBARU 000010000 MI DETROIT LEN GUINI EDSEL 000000010 MI DETROIT LEN GUINI FIAT 004000000 MI DETROIT LEN GUINI FORD 000020000 MI DETROIT LEN GUINI STUDEBAKER 000000600 NY ALBANY MAC A. RONEY CONTINENTAL 002000000 NY ALBANY MAC A. RONEY ESCORT 000010000 NY ALBANY MAC A. RONEY FORD 000010000 NY ALBANY MAC A. RONEY HYUNDAI 000010000 NY ALBANY MAC A. RONEY HYUNDAI 000010000 NY ROCHESTER BILL E. GOAT MASERATI 000001000 Here is sample JCL: //STEP1 EXEC PGM=BRKLV1 //STEPLIB DD DSN=your.executable.program.library.goes.here,DISP=SHR //*OF COURSE, THE NEXT LIBRARY NAME MAY BE DIFFERENT AT YOUR COMPANY //SALES1 DD DSN=userid.COBBOOK.DATA(SALES1),DISP=SHR //REPORTFI DD SYSOUT=* //SYSOUT DD SYSOUT=* //SYSUDUMP DD SYSOUT=*

Top of Page

Your email and other personal information will not be given to anyone
[Books Computer]

Home Books for Computer Professionals Privacy Terms |
Site Map and Site Search Programming Manuals and Tutorials The REXX Files Top of Page |