The No of records in the file may vary on a daily basis.
I did this is 2 SORT Steps. I would prefer to have it in 1 step.
I tried to do it in 1 step by using:
INREC - to provide Sequence Number
SORT - descending based on the Sequence No
OUTFIL IFTHEN - to build ');' in case Sequence No is 1 otherwise build ','.
This arrangement actually flips the records.
Can this be done without flipping using a single SORT Step?
At the first step, no info can be available in advance: what is following next to the current record?
This is fundamental principle; it is not related neither to SORT, nor to anything else.
Any possible trick would simulate the second scan of records, either explicitly, or in hidden manner.
As daveporcelan mentioned, if it is indeed a part of SQL query where you are doing something like a 'WHERE values IN' and trying to build the list of values then, you could possibly make use of a 'Dummy' record: