# sort with previous record

Author Message
anatol

Active User

Joined: 20 May 2010
Posts: 121

Posted: Thu Oct 06, 2016 2:36 am

Hi,

I have file (I need just idea, lets it fb 80)
 Code: AAAAA        AAAAA   8    AAAAA   7    AAAAA   6    AAAAA   W    AAAAA   0    AAAAA   1

in position 7 if value is W i need in sort order previous record before record W

 Code: AAAAA      AAAAA   0  AAAAA   1  AAAAA   6  AAAAA   W  AAAAA   7  AAAAA   8

Is it possible?
Kerry Ropar

New User

Joined: 14 Sep 2016
Posts: 25
Location: Australia

 Posted: Thu Oct 06, 2016 3:47 am anatol, not sure if other people did understood your problem statement, perhaps you would like to explain it again more elaborately with some real data.
enrico-sorichetti

Senior Moderator

Joined: 14 Mar 2007
Posts: 10714
Location: italy

 Posted: Thu Oct 06, 2016 3:51 am the explanation seems clear to me ... a W record is considered as an extension of the preceding record and the relation should be kept in the sorted output the only problem - the way the question is posed - is that the sort keys are not properly described
anatol

Active User

Joined: 20 May 2010
Posts: 121

Posted: Thu Oct 06, 2016 4:03 am

oh, sorry Enrico ... yes - the sort key is first 5 positions ( i need just idea how)...
and you are right about what I try to get
Input like
 Code: AAAAA      AAAAA   8 AAAAA   7 AAAAA   6 AAAAA   W AAAAA   0 AAAAA   1 BBBBB   8 BBBBB   7 BBBBB   0 BBBBB   W

Output
 Code: AAAAA      AAAAA   0 AAAAA   1 AAAAA   6 AAAAA   W AAAAA   7 AAAAA   8 BBBBB   0 BBBBB   W BBBBB   7 BBBBB   8
RahulG31

Active User

Joined: 20 Dec 2014
Posts: 446
Location: USA

Posted: Thu Oct 06, 2016 9:52 pm

My idea:
I would push position 7 of previous record and add some decimal value myself and then sort on the value including decimal. But then we'll have to take care of the character 'W'. I think it's just simpler to show what I did to achieve what you want:
 Code: INREC IFTHEN=(WHEN=GROUP,BEGIN=(7,1,CH,NE,C'W'),PUSH=(9:7,1)),         IFTHEN=(WHEN=(7,1,CH,NE,C'W'),BUILD=(1:1,9,10:C'.1')),         IFTHEN=(WHEN=(7,1,CH,EQ,C'W'),BUILD=(1:1,9,10:C'.2'))  SORT FIELDS=(9,3,CH,A)  OUTFIL BUILD=(1,7)

So, with input as this:
 Code: AAAAA AAAAA 8 AAAAA 7 AAAAA 6 AAAAA W AAAAA 0 AAAAA 1

I get output as this:
 Code: AAAAA AAAAA 0 AAAAA 1 AAAAA 6 AAAAA W AAAAA 7 AAAAA 8

.
anatol

Active User

Joined: 20 May 2010
Posts: 121

 Posted: Thu Oct 06, 2016 10:15 pm wow ... so elegant ... thank you
Arun Raj

Moderator

Joined: 17 Oct 2006
Posts: 2481
Location: @my desk

Posted: Thu Oct 06, 2016 10:54 pm

Rahul,

Nice work. You can add the 'EQUALS' to the SORT and get rid of those 2 additional IFTHENs. Something like.
 Code: INREC IFTHEN=(WHEN=GROUP,BEGIN=(7,1,CH,NE,C'W'),PUSH=(9:7,1))  SORT FIELDS=(1,5,CH,A,9,1,CH,A),EQUALS                         OUTREC BUILD=(1,7)
RahulG31

Active User

Joined: 20 Dec 2014
Posts: 446
Location: USA

 Posted: Thu Oct 06, 2016 11:30 pm @anatol: You're welcome! @Arun Raj: I was trying to remember 'EQUALS' to preserve order, but I just couldn't get that word in my mind at the time of writing that. Thanks for reminding me. .
Arun Raj

Moderator

Joined: 17 Oct 2006
Posts: 2481
Location: @my desk

 Posted: Thu Oct 06, 2016 11:37 pm Rahul - You're welcome!
Bill Woodger

Moderator Emeritus

Joined: 09 Mar 2011
Posts: 7311
Location: Inside the Matrix

 Posted: Thu Oct 06, 2016 11:48 pm When you have an IFTHEN which simply negates the condition of the single IFTHEN before it, use WHEN=NONE, like an ELSE in COBOL. Similarly, if you have more than one condition, and without HIT=NEXT, you have a COBOL EVALUATE structure, and WHEN=NONE is the equivalent of OTHER.
 View Bookmarks All times are GMT + 6 Hours

 Topic Forum Replies Similar Topics Very Large Sort failed - Seeking reco... DFSORT/ICETOOL 5 Date conversion using SORT SYNCSORT 4 Execute step regardless of previous s... JCL & VSAM 2 Record count of RECFM=U tape file DFSORT/ICETOOL 17 Which SORT utility can improve the Pe... DFSORT/ICETOOL 16
Search our Forums: