IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Alternate key data extracttion


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
07wickedwizard

New User


Joined: 24 Jul 2024
Posts: 10
Location: India

PostPosted: Thu Aug 14, 2025 11:25 am
Reply with quote

I am reading a ksds file with alternate key which has duplicates and want to extract data for that key even if it is repeating multiple times.

Lets say, if this alternate key (say account number 12345) is repeated 7 times i am checking for a flag value 1, if 4 of them have flag as 1, i need to copy them all, but with my current program, i am only able to fetch the first occurence or the latest one in the ksds file, and it skips the rest.

Can we fetch all the record without going to EOF? or any other way to get this done?
Back to top
View user's profile Send private message
sergeyken

Senior Member


Joined: 29 Apr 2008
Posts: 2263
Location: USA

PostPosted: Thu Aug 14, 2025 5:04 pm
Reply with quote

Your sample of code, please!
Only the fragment related to obtaining the record is enough.

(Do not forget to enclose the code into Code tags)
Back to top
View user's profile Send private message
07wickedwizard

New User


Joined: 24 Jul 2024
Posts: 10
Location: India

PostPosted: Tue Aug 19, 2025 3:26 pm
Reply with quote

Code:
**section is reading a flat file to get some data (acct number mostly)

 IF WS-L2BSFLAT-IO-OK
   PERFORM UNTIL WS-END-OF-INP-FILE = 'Y'
     READ L2BSFLAT
       AT END
          MOVE 'Y' TO WS-END-OF-INP-FILE
       NOT AT END
          MOVE L2BS-KEY-ORG    TO ACCT-ORG
                                  WS-EVALUATE-ORG
           PERFORM C2110-EVALUATE-ORG
          THRU    C2110-EXIT
          MOVE L2BS-KEY-ACCT   TO ACCT-NBR
                                  WS-KEY-ACCT
          MOVE L2BS-CUST-NBR   TO ACCT-CUST-NBR
          MOVE L2BS-DATE-OPENED TO ACCT-OPEN-DATE
          MOVE 'I'              TO ACCT-UPDATE-FUNCTION
          MOVE WS-TODAYS-DATE   TO ACCT-CREATE-DATE
          PERFORM C2200-READ-CARDS-KSDSFILE
         END-READ
       END-PERFORM
     END-IF

     IF NOT WS-L2BSFLAT-IO-OK
        STRING 'ERROR' WS-L2BSFLAT-STATUS
               ' ERROR IN READING THE L2BS FLAT FILE'
        DELIMITED BY SIZE INTO WS-ERR-TXT
        END-STRING
     END-IF
     .
 C2100-EXIT.
      EXIT.

**this section needs to read the ksds file with alternate key (this has dupliactes, which is the acct number) which has multiple cards attached to it where a cardholder flag should be 1

 C2200-READ-CARDS-KSDSFILE SECTION.
        DISPLAY 'IN L2ED KSDS FILE READ'
     MOVE ACCT-ORG     TO L2ED-ORG
     MOVE ACCT-NBR     TO L2ED-POST-TO-ACCT

     START L2EDKSDS KEY IS EQUAL TO L2ED-ALTERNT-KEY
         INVALID KEY
           DISPLAY 'ACCOUNT/KEY NOT FOUND: ' L2ED-ALTERNT-KEY
     END-START.

     READ L2EDKSDS NEXT RECORD
        AT END
           SET WS-L2EDKSDS-EOF TO TRUE
     END-READ.

     IF WS-L2EDKSDS-IO-OK
      PERFORM UNTIL WS-L2EDKSDS-EOF
        READ L2EDKSDS
          AT END
             SET WS-L2EDKSDS-EOF TO TRUE
          NOT AT END
            IF (L2ED-CARDHOLDER-TYPE = '1' AND
                L2ED-KEY-CARD-NBR(1:4) = '0000' AND
             L2ED-KEY-ORG = '120' AND
            (L2ED-LOGO = '100' OR '109' OR '110' OR '120'
                 OR '130' OR '140' OR '150' OR '160' OR '169')
                AND (L2ED-BLOCK-CODE NOT EQUAL TO 'L' OR 'S'
                      OR 'T' OR 'U' OR 'D'))
                   MOVE L2ED-KEY-CARD-NBR TO ACCT-PRIM-CARD-NUMBER
                 PERFORM W2000-WRITE-DETAIL-RECORDS
             ELSE
                  IF (L2ED-CARDHOLDER-TYPE = '1' AND
                    (L2ED-BLOCK-CODE NOT EQUAL TO 'L' OR 'S'
                            OR 'T' OR 'U' OR 'D'))
                        MOVE L2ED-KEY-CARD-NBR TO ACCT-PRIM-CARD-NUMBER
                        PERFORM W2000-WRITE-DETAIL-RECORDS
                     END-IF
               END-IF
           END-READ
         END-PERFORM
      END-IF
      .
  C2200-EXIT.
       EXIT.

**normal evaluate section

 C2110-EVALUATE-ORG SECTION.
        DISPLAY 'IN EVALUATE ORG SECTION'
     EVALUATE WS-EVALUATE-ORG
        WHEN '120'
        WHEN '190'
        WHEN '195'
           MOVE '02' TO ACCT-RECORD-TYPE
        WHEN '180'
           MOVE '03' TO ACCT-RECORD-TYPE
     END-EVALUATE
     .
 C2110-EXIT.
      EXIT.

Back to top
View user's profile Send private message
Garry Carroll

Senior Member


Joined: 08 May 2006
Posts: 1216
Location: Dublin, Ireland

PostPosted: Tue Aug 19, 2025 5:32 pm
Reply with quote

Are you reading the KSDS or the Alternate Index dataset? It's the Alternate Index that would have the multiple records, not the KSDS. Your code and comments suggest you're reading the KSDS.

Garry.
Back to top
View user's profile Send private message
sergeyken

Senior Member


Joined: 29 Apr 2008
Posts: 2263
Location: USA

PostPosted: Tue Aug 19, 2025 5:42 pm
Reply with quote

07wickedwizard wrote:

Lets say, if this alternate key (say account number 12345) is repeated 7 times i am checking for a flag value 1, if 4 of them have flag as 1, i need to copy them all, but with my current program, i am only able to fetch the first occurence or the latest one in the ksds file, and it skips the rest.

In order to verify, that "4 of them have flag as 1" you must read at least 4 records, leaving 3 or more previous ones behind.
From your code it is not clear: how do you plan "to copy them all" when previous matching records are gone (e.g. not reachable anymore)?
You may need either to re-read those previous records once again, using the current record key, or another option: you must keep those previous records in some temporary storage, to be able "to copy them all" when detected that "4 of them have flag as 1".

Your topic is not related to "COBOL programming"; it is 100% question on algorithm design. If you planned the general logic: how to provide access to previously read records, then implementation of this algorithm either in COBOL or any other tool would be a piece of cake.
Back to top
View user's profile Send private message
sergeyken

Senior Member


Joined: 29 Apr 2008
Posts: 2263
Location: USA

PostPosted: Tue Aug 19, 2025 8:16 pm
Reply with quote

P.S.
I only took a general view on the code sample. More time may be needed to verify it in details. Will do it when have some free time.

P.P.S.
Your initial goal is described as operating with only two fields: the key field, and the flag field.
But in your sample of code there are tons of IFs checking hundreds of values for unknown reasons.
I recommend, as a step of code debugging, temporary to delete (or to comment-out) all the code not related to verification of your KEY, and your FLAG fields. First: organize selection and copying of required records based on your initially mentioned requirement, and then - add all additional fields verifications.
Back to top
View user's profile Send private message
07wickedwizard

New User


Joined: 24 Jul 2024
Posts: 10
Location: India

PostPosted: Wed Aug 20, 2025 11:07 am
Reply with quote

Will try to comment them out and try it once.


Gary, don't we usually define the aix or path file for the alternate key in the JCL, i have the same, while reading the Input KSDS it usually reads the KSD1 file as well right? or have i got it wrong

Code:

//INPTFLAT DD  DISP=SHR,DSN=INPUT.FLAT.FILE
//INPTKSDS DD  DISP=SHR,DSN=INPUT.KSDS
//INPTKSD1 DD  DISP=SHR,DSN=INPUT.KSDS.PATH
//*OUTPUT FILE
//EXTRACT  DD  DSN=OUTPUT.FILE
Back to top
View user's profile Send private message
Garry Carroll

Senior Member


Joined: 08 May 2006
Posts: 1216
Location: Dublin, Ireland

PostPosted: Wed Aug 20, 2025 1:39 pm
Reply with quote

07wickedwizard wrote:
Will try to comment them out and try it once.


Gary, don't we usually define the aix or path file for the alternate key in the JCL, i have the same, while reading the Input KSDS it usually reads the KSD1 file as well right? or have i got it wrong

Code:

//INPTFLAT DD  DISP=SHR,DSN=INPUT.FLAT.FILE
//INPTKSDS DD  DISP=SHR,DSN=INPUT.KSDS
//INPTKSD1 DD  DISP=SHR,DSN=INPUT.KSDS.PATH
//*OUTPUT FILE
//EXTRACT  DD  DSN=OUTPUT.FILE


The alternate index records contain the alternate keys and actual key of the KSDS record. While there can be multiple records in the AIX, there is only one record in the KSDS with a unique key. For example, the KSDS might have a record for a unique account number, the AIX could have a record for each signatory. What is the key relationship between the base KSDS and the AIX? Has the base KSDS a key other than account number and the AIX therefore having multiple account number keys pointing to different KSDS records?

GaRRy.
Back to top
View user's profile Send private message
07wickedwizard

New User


Joined: 24 Jul 2024
Posts: 10
Location: India

PostPosted: Wed Aug 20, 2025 2:08 pm
Reply with quote

Garry Carroll wrote:
07wickedwizard wrote:
Will try to comment them out and try it once.


Gary, don't we usually define the aix or path file for the alternate key in the JCL, i have the same, while reading the Input KSDS it usually reads the KSD1 file as well right? or have i got it wrong

Code:

//INPTFLAT DD  DISP=SHR,DSN=INPUT.FLAT.FILE
//INPTKSDS DD  DISP=SHR,DSN=INPUT.KSDS
//INPTKSD1 DD  DISP=SHR,DSN=INPUT.KSDS.PATH
//*OUTPUT FILE
//EXTRACT  DD  DSN=OUTPUT.FILE


The alternate index records contain the alternate keys and actual key of the KSDS record. While there can be multiple records in the AIX, there is only one record in the KSDS with a unique key. For example, the KSDS might have a record for a unique account number, the AIX could have a record for each signatory. What is the key relationship between the base KSDS and the AIX? Has the base KSDS a key other than account number and the AIX therefore having multiple account number keys pointing to different KSDS records?

GaRRy.


The base KSDS has primary key values for card number, aix has secondary key values with acct number.

With the flat read, I am getting the acct number and other required data in the output, and with this acct number (which is the alternate key and has duplicates of the same acct number) I want to fetch the key value (card number) when flag value is 1, so if the acct number is repeated 3-4 times and of them 2 have flag as 1, i need to copy both the card numbers related to that account one after the other.

so, if acct an has 4 card numbers attached to it

Code:
inside KSDS:
org | primary key(card number)|secondary key(Acct number) | other output data | Flag
-------------------------------------------------------------------------------------
120 | xxxxxxxxxxxxxx140         | 1234567890123456789     | ***************** | 1
170 | xxxxxxxxxxxxxx711       | 1234567890123000000     | ***************** | 0
120 | xxxxxxxxxxxxxx111       | 1234567890123456789     | ***************** | 1
170 | xxxxxxxxxxxxxx777       | 1234567890123000000     | ***************** | 1
120 | xxxxxxxxxxxxxx133       | 1234567890123456789     | ***************** | 0
120 | xxxxxxxxxxxxxx144       | 1234567890123456789     | ***************** | 1

Ideal output:
org | primary key(card number)|secondary key(Acct number) | other output data | Flag
-------------------------------------------------------------------------------------
120 | xxxxxxxxxxxxxx111       | 1234567890123456789     | ***************** | 1
120 | xxxxxxxxxxxxxx140       | 1234567890123456789     | ***************** | 1
120 | xxxxxxxxxxxxxx144       | 1234567890123456789     | ***************** | 1
170 | xxxxxxxxxxxxxx777       | 1234567890123000000     | ***************** | 1
Back to top
View user's profile Send private message
Garry Carroll

Senior Member


Joined: 08 May 2006
Posts: 1216
Location: Dublin, Ireland

PostPosted: Wed Aug 20, 2025 6:04 pm
Reply with quote

How is L2EDKSDS defined in FILE-CONTROL paragraph?

Garry
Back to top
View user's profile Send private message
07wickedwizard

New User


Joined: 24 Jul 2024
Posts: 10
Location: India

PostPosted: Thu Aug 21, 2025 5:41 pm
Reply with quote

Garry Carroll wrote:
How is L2EDKSDS defined in FILE-CONTROL paragraph?

Garry


Code:
**L2ED KSDS FILE
     SELECT L2EDKSDS
       ASSIGN TO L2EDKSDS
       ACCESS IS DYNAMIC
       ORGANIZATION IS INDEXED
       RECORD KEY IS L2ED-KEY
       ALTERNATE KEY IS L2ED-ALTERNT-KEY
                     WITH DUPLICATES
       FILE STATUS IS WS-L2EDKSDS-STATUS.
Back to top
View user's profile Send private message
07wickedwizard

New User


Joined: 24 Jul 2024
Posts: 10
Location: India

PostPosted: Tue Aug 26, 2025 12:17 pm
Reply with quote

Any insights Gary?
Back to top
View user's profile Send private message
Garry Carroll

Senior Member


Joined: 08 May 2006
Posts: 1216
Location: Dublin, Ireland

PostPosted: Tue Aug 26, 2025 12:58 pm
Reply with quote

07wickedwizard wrote:
Any insights Gary?


No, and I don't have bandwidth to spend on this, unfortunately. However, for what you describe I'd most likely process the cards KSDS using a sort product to achieve what you want by processing groups of card records with the same account number and selecting those with a flag value of 1.

If you must do this in Cobol, I'd suggest following Sergeyken's recommendation:
Quote:
Your initial goal is described as operating with only two fields: the key field, and the flag field.
But in your sample of code there are tons of IFs checking hundreds of values for unknown reasons.
I recommend, as a step of code debugging, temporary to delete (or to comment-out) all the code not related to verification of your KEY, and your FLAG fields. First: organize selection and copying of required records based on your initially mentioned requirement, and then - add all additional fields verifications.


Garry.
Back to top
View user's profile Send private message
Rohit Umarjikar

Global Moderator


Joined: 21 Sep 2010
Posts: 3108
Location: NYC,USA

PostPosted: Wed Aug 27, 2025 9:57 pm
Reply with quote

Debug the code
Back to top
View user's profile Send private message
View previous topic : : View next topic  
Post new topic   Reply to topic All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts db2 vs static data COBOL Programming 1
No new posts External data queue (changes?) CLIST & REXX 0
No new posts Pull data using date difference betwe... DB2 6
No new posts fast data scrambling PL/I & Assembler 10
No new posts How to save SYSLOG as text data via P... All Other Mainframe Topics 4
Search our Forums:


Back to Top