Search code examples
mainframeadabas-natural

How to only print unique case numbers?


I am trying to only print unique case numbers (which are located in the first 5 bytes of each input record). My input file structure is as in this example:

00001
00001
00002
00002
00002
00003
00004
00005

I should be able to read in the case number such as 0001 (or 00002, 00003, ...) and save that in a temp variable. Then compare the value of that temp variable to the case number of the current record being processed. When they do not match then I need to write the case number (as contained in the temp variable) to my output file. And then continue processing the remaining records.

So in the end, my output file should look similar to this one (note that only the last records with case number either 00001 or 00002 were written):

00001
00002
00003
00004
00005

Here is my code so far:

READ WORK FILE 1 #I-RECORD                            
 IF #RECORD-READ = 0 DO                                   
    WRITE WORK 2 #OUT-HEADER                              
 DOEND /* 510                                             
 ADD 1 TO #RECORD-READ                                    
*                                                         
IF #AA-CASE NOT EQUAL ' ' DO                              
 #CURRENT-CASE-NUM = #AA-CASE                             
 IF #COUNT-CASES = 0 DO                                   
    #OLD-CASE-NUM = #AA-CASE                              
 DOEND /* 570                                             
*                                                         
 IF #OLD-CASE-NUM NOT EQUAL #CURRENT-CASE-NUM DO          
    #OO-CASE-NUMBER            = #OLD-CASE-NUM            
*   #OO-TOTAL-OF-MONTHS        = 0                        
*   #OO-TOTAL-OF-TRANSACTIONS = #COUNT-CASES              
    WRITE WORK 2 #OUTPUT-RECORD                           
    #OLD-CASE-NUM = 0   
 DOEND  
*                                                      
 DISPLAY #CURRENT-CASE-NUM #OLD-CASE-NUM #COUNT-CASES  
*                                                      
 IF #OLD-CASE-NUM NOT EQUAL #CURRENT-CASE-NUM DO       
    #OO-CASE-NUMBER            = #OLD-CASE-NUM         
*   #OO-TOTAL-OF-MONTHS        = 0                     
*   #OO-TOTAL-OF-TRANSACTIONS = #COUNT-CASES           
    WRITE WORK 2 #OUTPUT-RECORD                        
    #OLD-CASE-NUM = 0                                  
 DOEND /* 610                                          
 ELSE DO                                               
   ADD 1 TO #COUNT-CASES                               
 DOEND /* 710                                          
DOEND /* 510                                                
LOOP(0500) 

Solution

  • Your sample file has the case numbers in order. That makes the problem easier. You can solve this problem by comparing the current case number to the prior case number (see paragraph TEST-CASE-NUMBER in the example below). Notice the order of assigning prior-case-number and comparing it to the current case-number. The example below was written for GNUCobol, but you should be able to adapt it for mainframe Cobol. Also, when reading a file always check the file-status to catch open, read, and close errors (paragraph TEST-FILE-STATUS).

       IDENTIFICATION DIVISION.
       PROGRAM-ID. PRINT-UNIQUE.
    
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       SELECT CASE-FILE
           ASSIGN TO 'CASE-FILE.DAT'
           ORGANIZATION IS LINE SEQUENTIAL
           FILE STATUS IS CASE-FILE-STATUS.
    
       DATA DIVISION.
       FILE SECTION.
       FD CASE-FILE.
           01  CASE-NUMBER                  PIC X(5).
    
       WORKING-STORAGE SECTION.
    
       01  MORE-RECORDS                     PIC XXX VALUE 'YES'.
           88  HAS-MORE-RECORDS             VALUE 'YES'.
           88  NO-MORE-RECORDS              VALUE 'NO '.
    
       01  CASE-FILE-STATUS                 PIC XX.
           88  CASE-FILE-STATUS-OKAY        VALUES '00' '10'.
      *        VALUE 00 = SUCCESS
      *        VALUE 10 = END OF FILE
    
       01  PRIOR-CASE-NUMBER                PIC X(5) VALUE SPACES.
    
       PROCEDURE DIVISION.
    
       MAIN.
           OPEN INPUT CASE-FILE
           PERFORM TEST-FILE-STATUS
           PERFORM HANDLE-CASE-RECORD UNTIL NO-MORE-RECORDS
           CLOSE CASE-FILE
           PERFORM TEST-FILE-STATUS
           STOP RUN
           .
    
       HANDLE-CASE-RECORD.
           READ CASE-FILE
               AT END SET NO-MORE-RECORDS TO TRUE
               NOT AT END
                   PERFORM TEST-CASE-NUMBER
           END-READ
           PERFORM TEST-FILE-STATUS
           .
    
       TEST-CASE-NUMBER.
           IF CASE-NUMBER NOT = PRIOR-CASE-NUMBER
               DISPLAY CASE-NUMBER
           END-IF
           MOVE CASE-NUMBER TO PRIOR-CASE-NUMBER
           .
    
       TEST-FILE-STATUS.
           IF NOT CASE-FILE-STATUS-OKAY THEN
               DISPLAY 'FILE READ ERROR ' CASE-FILE-STATUS
               CLOSE CASE-FILE
               STOP RUN
           END-IF
           .