1

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) 
Pierre.Vriens
  • 2,117
  • 75
  • 29
  • 42
  • 2
    That's `NATURAL`, not COBOL, isn't it? Must have been a long week. How you got an Upvote when its even tagged with the wrong language, I don't know :-) – Bill Woodger Aug 09 '14 at 09:15
  • 1
    Looking beyond that, there is not actually a question. What output do you get from that input? Any error messages? What is actually wrong? – Bill Woodger Aug 12 '14 at 13:35
  • 1
    Does the Accept mean you're OK with a COBOL solution? Can you re-tag it as COBOL and make it clear in the question. Clarified question. – Bill Woodger Aug 13 '14 at 07:22

1 Answers1

1

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
       .
  • 1
    You did notice it is not actually COBOL? If you test for FILE STATUS of 10, rather than use the AT END, things will become simplified. Remove the 10 from the OKAY 88. – Bill Woodger Aug 09 '14 at 09:11
  • I did not notice! I saw the tag "COBOL," the problem definition, and ran with my own solution. Facing my own blindness is sobering. I was not using my eyes or brain as much as I thought I was. Thanks for the advice on simplifying the exit-condition test. – Valdis Grinbergs Aug 12 '14 at 12:39
  • 1
    Happens. Often not a bad way to go about it, as it avoids the pre-suppositions of OP's chosen solution (not working). I missed that there wasn't even a question. I gave you a vote anyway. Useful to have an example with the COBOL questions we get. I'd better re-tag it as well... – Bill Woodger Aug 12 '14 at 13:37
  • 1
    Looks like you should do it more often :-) – Bill Woodger Aug 13 '14 at 07:23
  • thank you for the posts. I was able to adapt it over so that it would work. Thank you for the help. –  Aug 13 '14 at 19:58