Control groups organize data hierarchically for processing and reporting. Learn to define hierarchical structures, process multi-level groups, and generate comprehensive reports with subtotals and summaries.
123456789101112131415WORKING-STORAGE SECTION. 01 CONTROL-GROUP-STRUCTURE. 05 REGION-GROUP. 10 REGION-CODE PIC X(3). 10 REGION-TOTAL PIC 9(9)V99 VALUE 0. 05 DEPARTMENT-GROUP. 10 DEPT-CODE PIC X(5). 10 DEPT-TOTAL PIC 9(9)V99 VALUE 0. 05 EMPLOYEE-GROUP. 10 EMP-ID PIC 9(6). 10 EMP-SALARY PIC 9(7)V99. 01 PREVIOUS-VALUES. 05 PREV-REGION PIC X(3) VALUE SPACES. 05 PREV-DEPT PIC X(5) VALUE SPACES.
Define control group structures with hierarchical levels. Each level represents a different grouping (region, department, employee). Include totals for each level and previous values for break detection.
12345678910111213141516171819202122232425PROCESS-CONTROL-GROUPS. PERFORM UNTIL EOF READ EMPLOYEE-FILE AT END SET EOF TO TRUE NOT AT END MOVE EMP-REGION TO REGION-CODE MOVE EMP-DEPT TO DEPT-CODE MOVE EMP-ID TO EMP-ID MOVE EMP-SALARY TO EMP-SALARY *> Check for control breaks in hierarchical order IF REGION-CODE NOT = PREV-REGION PERFORM REGION-BREAK ELSE IF DEPT-CODE NOT = PREV-DEPT PERFORM DEPT-BREAK END-IF *> Process current record PERFORM PROCESS-EMPLOYEE-RECORD *> Update previous values MOVE REGION-CODE TO PREV-REGION MOVE DEPT-CODE TO PREV-DEPT END-READ END-PERFORM PERFORM FINAL-BREAKS.
Process data hierarchically by checking control breaks in order from highest to lowest level. Each break triggers subtotal processing and formatting. Process the current record and update previous values for next iteration.
123456789101112131415161718REGION-BREAK. IF PREV-REGION NOT = SPACES PERFORM DEPT-BREAK *> Process final dept break DISPLAY 'Region Total: ' PREV-REGION ' = ' REGION-TOTAL DISPLAY '========================================' END-IF DISPLAY 'REGION: ' REGION-CODE DISPLAY '========================================' MOVE 0 TO REGION-TOTAL. DEPT-BREAK. IF PREV-DEPT NOT = SPACES DISPLAY 'Department Total: ' PREV-DEPT ' = ' DEPT-TOTAL DISPLAY '----------------------------------------' END-IF DISPLAY 'Department: ' DEPT-CODE DISPLAY '----------------------------------------' MOVE 0 TO DEPT-TOTAL.
Process breaks at each hierarchical level. Higher-level breaks call lower-level breaks to ensure proper subtotal processing. Print headers, footers, and reset totals for each level.
12345678PROCESS-EMPLOYEE-RECORD. DISPLAY EMP-ID ' ' EMP-NAME ' ' EMP-SALARY ADD EMP-SALARY TO DEPT-TOTAL ADD EMP-SALARY TO REGION-TOTAL ADD EMP-SALARY TO GRAND-TOTAL ADD 1 TO EMP-COUNT ADD 1 TO DEPT-COUNT ADD 1 TO REGION-COUNT.
Perform calculations at all group levels simultaneously. Add values to department, region, and grand totals. Count records at each level for comprehensive reporting and analysis.
12345678910111213INITIALIZE-CONTROL-GROUPS. MOVE SPACES TO PREV-REGION MOVE SPACES TO PREV-DEPT MOVE 0 TO REGION-TOTAL MOVE 0 TO DEPT-TOTAL MOVE 0 TO GRAND-TOTAL MOVE 0 TO REGION-COUNT MOVE 0 TO DEPT-COUNT MOVE 0 TO EMP-COUNT DISPLAY 'Employee Report by Region and Department' DISPLAY '========================================' DISPLAY 'Region Dept Employee ID Name Salary' DISPLAY '------ ----- ----------- ---- ------'.
Initialize all control group variables before processing. Set previous values to spaces, totals to zero, and counters to zero. Print report headers with column titles for clear formatting.
12345678910FINAL-BREAKS. IF PREV-REGION NOT = SPACES PERFORM DEPT-BREAK *> Process final dept break DISPLAY 'Region Total: ' PREV-REGION ' = ' REGION-TOTAL DISPLAY 'Employees in region: ' REGION-COUNT DISPLAY '========================================' END-IF DISPLAY 'GRAND TOTAL: ' GRAND-TOTAL DISPLAY 'Total employees: ' EMP-COUNT DISPLAY '========================================'.
Process final breaks to complete the last group's processing. Print final subtotals, counts, and grand totals. This ensures all data is properly summarized and reported.
123456789VALIDATE-CONTROL-GROUPS. IF REGION-CODE < PREV-REGION DISPLAY 'Error: Data not sorted by region' PERFORM ERROR-HANDLING ELSE IF REGION-CODE = PREV-REGION AND DEPT-CODE < PREV-DEPT DISPLAY 'Error: Data not sorted by department' PERFORM ERROR-HANDLING END-IF.
Validate that input data is properly sorted for control group processing. Check that higher-level keys don't decrease and that lower-level keys are properly ordered within higher levels.
1234567891011*> Process variable number of group levels EVALUATE GROUP-LEVEL WHEN 1 PERFORM SINGLE-LEVEL-PROCESSING WHEN 2 PERFORM TWO-LEVEL-PROCESSING WHEN 3 PERFORM THREE-LEVEL-PROCESSING WHEN OTHER PERFORM MULTI-LEVEL-PROCESSING END-EVALUATE.
Handle variable numbers of control group levels based on configuration or data characteristics. Use EVALUATE to select appropriate processing logic for different hierarchical structures.