Process file/records until a specific total is reached.



Support for NetApp SyncSort for z/OS, Visual SyncSort, SYNCINIT, SYNCLIST and SYNCTOOL

Process file/records until a specific total is reached.

Postby chillmo » Thu Mar 24, 2022 6:18 am

Is it possible to process a file/records until a specific total is reached?

For example, I have a file that has 100 records but I need to stop processing when a specific total, e.g., $100, has been reached or exceeded. The next file could have 25 records but same requirement......need to stop processing after $100 has been reached or exceeded.

File layout:

DD000000000000000000010000000000011000075
DD000000000000000000020000000000089000075
DD000000000000000000030000000000100000075

In this case the total was reached after the first two records, so no need to continue processing.

Let me know.

Thanks!
chillmo
 
Posts: 11
Joined: Wed Aug 30, 2017 8:56 pm
Has thanked: 1 time
Been thanked: 0 time

Re: Process file/records until a specific total is reached.

Postby sergeyken » Thu Mar 24, 2022 5:29 pm

What does this mean???

chillmo wrote:File layout:

DD000000000000000000010000000000011000075
DD000000000000000000020000000000089000075
DD000000000000000000030000000000100000075
Javas and Pythons come and go, but JCL and SORT stay forever.
User avatar
sergeyken
 
Posts: 408
Joined: Wed Jul 24, 2019 10:12 pm
Has thanked: 6 times
Been thanked: 40 times

Re: Process file/records until a specific total is reached.

Postby sergeyken » Fri Mar 25, 2022 8:30 pm

sergeyken wrote:What does this mean???

chillmo wrote:File layout:

DD000000000000000000010000000000011000075
DD000000000000000000020000000000089000075
DD000000000000000000030000000000100000075


Hint:
What is presented here is (highly likely?) the "dump of input data"
The term "layout" means: description of field name/position/type/size/purpose for EACH field of the WHOLE record.
At least for the field(s) you are interested in.
The rest of records is completely irrelevant for your issue; it only adds unneeded mess to this topic.

When it is missing, the question cannot be answered.
Javas and Pythons come and go, but JCL and SORT stay forever.
User avatar
sergeyken
 
Posts: 408
Joined: Wed Jul 24, 2019 10:12 pm
Has thanked: 6 times
Been thanked: 40 times

Re: Process file/records until a specific total is reached.

Postby sergeyken » Fri Mar 25, 2022 9:15 pm

Since the issue of this topic is not very trivial for the Beginners Forum, I can give one of possible solutions not waiting for any response from the TS.

As usually, I use my own test "layout" only to demonstrate the approach, not to allow using it as copy-and-paste code, without a minor understanding how it works.

// EXPORT SYMLIST=*                                                  
//*                                                                  
// SET LIMIT=100                                                      
//*====================================================================
//SORTSUM  EXEC PGM=SYNCTOOL                                          
//*                                                                    
//SSMSG    DD  SYSOUT=*                                              
//TOOLMSG  DD  SYSOUT=*                                              
//SORTIN   DD  *                                                      
AAAAAAAA 1234567890 ZZZZZZZZ 9876543210 00000011 XXXXXXXX            
QQQQQQQQ 2345678901 DDDDDDDD 8765432109 00000089 HHHHHHHH            
CCCCCCCC 3456789012 NNNNNNNN 7654321098 00000100 KKKKKKKK            
YYYYYYYY 4567890123 PPPPPPPP 6543210987 00000125 JJJJJJJJ            
//*-+----1----+----2----+----3----+----4----+----5----+----6----+----7
//*                                                                  
//NUM1     DD  SPACE=(TRK,(20,20))                                    
//NUM2     DD  SPACE=(TRK,(20,20))                                    
//NUM12    DD  SPACE=(TRK,(20,20))                                    
//*                                                                  
//SORTOUT  DD  SYSOUT=*                                                
//*....................................................................
//TOOLIN   DD  *                                                      
 COPY FROM(SORTIN) TO(NUM1)    USING(SEQ1)                            
 COPY FROM(SORTIN) TO(NUM2)    USING(SEQ2)                            
 SORT JKFROM       TO(NUM12)   USING(JOIN)                            
 COPY JKFROM       TO(SORTOUT) USING(DROP)                            
//*....................................................................
//SEQ1CNTL DD  *                                                      
 INREC OVERLAY=(81:SEQNUM,8,ZD,START=1)     record nums from 1        
//*....................................................................
//SEQ2CNTL DD  *                                                      
 INREC OVERLAY=(81:SEQNUM,8,ZD,START=2)     record nums from 2        
//*....................................................................
//JOINCNTL DD  *                                                      
 JOINKEYS F1=NUM1,FIELDS=(81,8,A),SORTED                              
 JOINKEYS F2=NUM2,FIELDS=(81,8,A),SORTED                              
 JOIN UNPAIRED,F1                                                      
 REFORMAT FIELDS=(F1:1,80,     full input record                      
                  F1:81,8,     SEQNUM1                                
                  F2:41,8)     prev record amount                    
                                                                       
* Calculate running SUM                                              
 INREC BUILD=(81,8,            SEQNUM1                                
           10:41,08,ZD,ADD,89,8,ZD,TO=ZDF,LENGTH=8)                  
                                                                       
 SORT FIELDS=(1,8,CH,A)    - make sure JOIN did not change the order  
//*....................................................................
//DROPCNTL DD  *,SYMBOLS=EXECSYS                                      
 JOINKEYS F1=NUM1,FIELDS=(81,8,A),SORTED   numbered input            
 JOINKEYS F2=NUM12,FIELDS=(1,8,A),SORTED,                            
          INCLUDE=(10,8,ZD,LE,&LIMIT)      allowed SEQNUMs            
                                                                       
 REFORMAT FIELDS=(F1:1,80)                 = initial record          
//*                                                                  
//*====================================================================
Javas and Pythons come and go, but JCL and SORT stay forever.
User avatar
sergeyken
 
Posts: 408
Joined: Wed Jul 24, 2019 10:12 pm
Has thanked: 6 times
Been thanked: 40 times

Re: Process file/records until a specific total is reached.

Postby sergeyken » Fri Mar 25, 2022 9:59 pm

An improved version, to avoid double scan of the original dataset.

// EXPORT SYMLIST=*                                                  
//*                                                                  
// SET LIMIT=100                                                      
//*====================================================================
//SORTSUM  EXEC PGM=SYNCTOOL                                          
//*                                                                    
//SSMSG    DD  SYSOUT=*                                              
//TOOLMSG  DD  SYSOUT=*                                              
//SORTIN   DD  *                                                      
AAAAAAAA 1234567890 ZZZZZZZZ 9876543210 00000011 XXXXXXXX            
QQQQQQQQ 2345678901 DDDDDDDD 8765432109 00000089 HHHHHHHH            
CCCCCCCC 3456789012 NNNNNNNN 7654321098 00000100 KKKKKKKK            
YYYYYYYY 4567890123 PPPPPPPP 6543210987 00000125 JJJJJJJJ            
//*-+----1----+----2----+----3----+----4----+----5----+----6----+----7
//*                                                                  
//NUM1     DD  SPACE=(TRK,(20,20))                                    
//NUM2     DD  SPACE=(TRK,(20,20))                                    
//NUM12    DD  SPACE=(TRK,(20,20))                                    
//*                                                                  
//SORTOUT  DD  SYSOUT=*                                                
//*....................................................................
//TOOLIN   DD  *                                                      
 COPY FROM(SORTIN)             USING(SEQN)                            
 SORT JKFROM       TO(NUM12)   USING(JOIN)                            
 COPY JKFROM       TO(SORTOUT) USING(DROP)                            
//*....................................................................
//SEQNCNTL DD  *                                                      
 OUTFIL FNAMES=NUM1,BUILD=(1,80,SEQNUM,8,ZD,START=1)   renum from 1
 OUTFIL FNAMES=NUM2,BUILD=(1,80,SEQNUM,8,ZD,START=2)   renum from 2
//*....................................................................
//JOINCNTL DD  *                                                      
 JOINKEYS F1=NUM1,FIELDS=(81,8,A),SORTED                              
 JOINKEYS F2=NUM2,FIELDS=(81,8,A),SORTED                              
 JOIN UNPAIRED,F1                                                      
 REFORMAT FIELDS=(F1:1,80,     full input record                      
                  F1:81,8,     SEQNUM1                                
                  F2:41,8)     prev record amount                    
                                                                       
* Calculate running SUM                                              
 INREC BUILD=(81,8,            SEQNUM1                                
           10:41,08,ZD,ADD,89,8,ZD,TO=ZDF,LENGTH=8)                  
                                                                       
 SORT FIELDS=(1,8,CH,A)    - make sure JOIN did not change the order  
//*....................................................................
//DROPCNTL DD  *,SYMBOLS=EXECSYS                                      
 JOINKEYS F1=NUM1,FIELDS=(81,8,A),SORTED   numbered input            
 JOINKEYS F2=NUM12,FIELDS=(1,8,A),SORTED,                            
          INCLUDE=(10,8,ZD,LE,&LIMIT)      allowed SEQNUMs            
                                                                       
 REFORMAT FIELDS=(F1:1,80)                 = initial record          
//*                                                                  
//*====================================================================
Javas and Pythons come and go, but JCL and SORT stay forever.
User avatar
sergeyken
 
Posts: 408
Joined: Wed Jul 24, 2019 10:12 pm
Has thanked: 6 times
Been thanked: 40 times

Re: Process file/records until a specific total is reached.

Postby sergeyken » Sat Mar 26, 2022 5:06 am

As usually, after reviewing any code once again one can find new ways for its further improvement, and removing unneeded steps.

// EXPORT SYMLIST=*                                                  
//*                                                                  
// SET LIMIT=100      the desired limit for maximum total amount                                                    
//*====================================================================
//SORTSUM  EXEC PGM=SYNCTOOL                                          
//*                                                                    
//SSMSG    DD  SYSOUT=*                                              
//TOOLMSG  DD  SYSOUT=*                                              
//SORTIN   DD  *                                                      
AAAAAAAA 1234567890 ZZZZZZZZ 9876543210 00000011 XXXXXXXX            
QQQQQQQQ 2345678901 DDDDDDDD 8765432109 00000089 HHHHHHHH            
CCCCCCCC 3456789012 NNNNNNNN 7654321098 00000100 KKKKKKKK            
YYYYYYYY 4567890123 PPPPPPPP 6543210987 00000125 JJJJJJJJ            
//*-+----1----+----2----+----3----+----4----+----5----+----6----+----7
//*                                                                  
//NUM1     DD  SPACE=(TRK,(20,20))                                    
//NUM2     DD  SPACE=(TRK,(20,20))                                    
//NUM12    DD  SPACE=(TRK,(20,20))                                    
//*                                                                  
//SORTOUT  DD  SYSOUT=*                                                
//*....................................................................
//TOOLIN   DD  *                                                      
 COPY FROM(SORTIN)    USING(SEQN)                            
 SORT JKFROM          USING(JOIN)                            
//*....................................................................
//SEQNCNTL DD  *                                                      
 OUTFIL FNAMES=NUM1,BUILD=(1,80,SEQNUM,8,ZD,START=1)   renum from 1
 OUTFIL FNAMES=NUM2,BUILD=(1,80,SEQNUM,8,ZD,START=2)   renum from 2
//*....................................................................
//JOINCNTL DD  *,SYMBOLS=EXECSYS                                                      
 JOINKEYS F1=NUM1,FIELDS=(81,8,A),SORTED                              
 JOINKEYS F2=NUM2,FIELDS=(81,8,A),SORTED                              
 JOIN UNPAIRED,F1                                                      
 REFORMAT FIELDS=(F1:1,80,     full input record                      
                  F1:81,8,     SEQNUM1                                
                  F2:41,8)     prev record amount                    
                                                                       
* Calculate running SUM                                              
 INREC OVERLAY=(89:41,8,ZD,ADD,89,8,ZD,TO=ZDF,LENGTH=8)                  
                                                                       
 SORT FIELDS=(81,8,CH,A) - make sure the JOIN did not change the order  

 OUTFIL FNAMES=SORTOUT,
        INCLUDE=(89,8,ZD,LE,&LIMIT), include until specified sum
        BUILD=(1,80)                 truncate to original size
//*                                                                  
//*====================================================================
Javas and Pythons come and go, but JCL and SORT stay forever.
User avatar
sergeyken
 
Posts: 408
Joined: Wed Jul 24, 2019 10:12 pm
Has thanked: 6 times
Been thanked: 40 times

Re: Process file/records until a specific total is reached.

Postby sergeyken » Sat Mar 26, 2022 11:27 pm

To be on safe side for ADD operation, it may be useful to clean-up the field in the non-matching first record

. . . . . . . .
 REFORMAT FIELDS=(F1:1,80,     full input record                      
                  F1:81,8,     SEQNUM1                                
                  F2:41,8),    prev record amount  
          FILL=C'0'            zero the unpaired first record prev. value                
. . . . . . . . . . . . . . .                                                                       
 
Javas and Pythons come and go, but JCL and SORT stay forever.
User avatar
sergeyken
 
Posts: 408
Joined: Wed Jul 24, 2019 10:12 pm
Has thanked: 6 times
Been thanked: 40 times

Re: Process file/records until a specific total is reached.

Postby sergeyken » Tue Mar 29, 2022 12:55 am

During the weekend I've reviewed this topic once again, and I found my misunderstanding of the original issue.
That's why my previous solution would not produce the result really expected.

Please find here another approach to this task.

// EXPORT SYMLIST=*                                                  
//*                                                                  
// SET LIMIT=100                                                      
//*====================================================================
//SORTSUM  EXEC PGM=SYNCTOOL                                          
//*                                                                    
//TOOLMSG  DD  SYSOUT=*                                              
//SSMSG    DD  SYSOUT=*                                              
//SORTIN   DD  *                                                      
AAAAAAAA 1234567890 ZZZZZZZZ 9876543210 00000011 XXXXXXXX            
QQQQQQQQ 2345678901 DDDDDDDD 8765432109 00000089 HHHHHHHH            
CCCCCCCC 3456789012 NNNNNNNN 7654321098 00000100 KKKKKKKK            
YYYYYYYY 4567890123 PPPPPPPP 6543210987 00000125 JJJJJJJJ            
//*-+----1----+----2----+----3----+----4----+----5----+----6----+----7
//*                                                                  
//SUB1     DD  SPACE=(TRK,(20,20))                                    
//*                                                                    
//SORTOUT  DD  SYSOUT=*                                              
//*....................................................................
//TOOLIN   DD  *                                                      
 COPY FROM(SORTIN)  TO(SUB1)     USING(SUBT)                          
 COPY FROM(SUB1)    TO(SORTOUT)  USING(DROP)                          
//*....................................................................
//SUBTCNTL DD  *                                                      
 INREC OVERLAY=(81:SEQNUM,8,ZD,  renumber for unique id              
                89:8X)           space for trailer                    
 OUTFIL FNAMES=SUB1,                                                  
        REMOVECC,NODETAIL,                                            
        SECTIONS=(81,8,                unique num for each record    
                  TRAILER3=(1,80,      original record part          
                  SUBTOTAL=(41,8,ZD,EDIT=(TTTTTTTT))))  running sum  
//*....................................................................
//DROPCNTL DD  *,SYMBOLS=EXECSYS                                      
 INREC IFTHEN=(WHEN=GROUP,     detect the group of records needed                                          
               BEGIN=(81,8,ZD,EQ,41,8,ZD),                            
               END=(81,8,ZD,GE,&LIMIT),                              
               PUSH=(89:ID=4))                                        
 OUTFIL FNAMES=SORTOUT,                                              
        INCLUDE=(89,4,CH,NE,C' '),     select records from the marked group                                  
        BUILD=(1,80)                truncate to original LRECL                                               
//*                                                                    
//*====================================================================


Sorry for misleading previous recommendations.
Javas and Pythons come and go, but JCL and SORT stay forever.
User avatar
sergeyken
 
Posts: 408
Joined: Wed Jul 24, 2019 10:12 pm
Has thanked: 6 times
Been thanked: 40 times

Re: Process file/records until a specific total is reached.

Postby chillmo » Wed Mar 30, 2022 3:28 am

Sergeyken,

Sorry for the late response but here's the breakdown of fields:

1st record:
Application code (2 bytes) - DD
Account number (20 bytes) - 00000000000000000001
Amount (15 bytes) - 000000000001100 ($11.00)
Source code (4 bytes) - 0075

2nd record:
Application code (2 bytes) - DD
Account number (20 bytes) - 00000000000000000002
Amount (15 bytes) - 000000000008900 ($89.00)
Source code (4 bytes) - 0075

3rd record:
Application code (2 bytes) - DD
Account number (20 bytes) - 00000000000000000003
Amount (15 bytes) - 000000000010000 ($100.00)
Source code (4 bytes) - 0075


DD000000000000000000010000000000011000075
DD000000000000000000020000000000089000075
DD000000000000000000030000000000100000075
 


Since the total of $100 was reached after the first two records, Sort should step processing and write out the two records.

If the next file has


DD000000000000000000030000000000100000075
DD000000000000000000010000000000111000075
DD000000000000000000020000000000089000075
 


The sort should stop processing and write out the first record.

Hope this helps!
chillmo
 
Posts: 11
Joined: Wed Aug 30, 2017 8:56 pm
Has thanked: 1 time
Been thanked: 0 time

Re: Process file/records until a specific total is reached.

Postby sergeyken » Wed Mar 30, 2022 5:16 pm

I have provided you with the working approach to this task.

It is your responsibility to adjust this general solution to your own particular record layout.
Javas and Pythons come and go, but JCL and SORT stay forever.
User avatar
sergeyken
 
Posts: 408
Joined: Wed Jul 24, 2019 10:12 pm
Has thanked: 6 times
Been thanked: 40 times

Next

Return to Syncsort/Synctool

 


  • Related topics
    Replies
    Views
    Last post