Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Profile Log in to check your private messages Log in
 
Can I use same file as Input and Output for COBOL SORT ?

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
View previous topic :: :: View next topic  
Author Message
ParagChouguley

Active User


Joined: 03 Feb 2007
Posts: 175
Location: PUNE(INDIA)

PostPosted: Thu Feb 15, 2007 11:16 am    Post subject: Can I use same file as Input and Output for COBOL SORT ?
Reply with quote

Hi,
My COBOL program gives a output file, which I want in sorted form, so I'm using COBOL sort. To reduce the number of files I want to code as follows. Would it be allowed by COBOL ?

SORT SORT-FILE ON ASCENDING KEY
WS-SORT-KEY
USING MY-OUTPUT-FILE
GIVING MY-OUTPUT-FILE

Here, SORT-FILE is temporary file, whereas MY-OUTPUT-FILE is an output file given by program, which I want to be sorted within itself. [/b]
Back to top
View user's profile Send private message

prav_06
Warnings : 1

Active User


Joined: 13 Dec 2005
Posts: 154
Location: The Netherlands

PostPosted: Thu Feb 15, 2007 11:22 am    Post subject:
Reply with quote

paragChouguley,
The syntax wat u had give in absolutely rite but it can be refined like this


SORT SORT-FILE ON ASCENDING KEY
WS-SORT-KEY
USING MY-INPUT-FILE
GIVING MY-INPUT-FILE


as per your synatx , it uses the Output file as input , the output file would be empty i guess, and always u have to give the input file name in the USING clause. I hope u got this concept rite.


Cheer's,

Thamilzan.
Back to top
View user's profile Send private message
ParagChouguley

Active User


Joined: 03 Feb 2007
Posts: 175
Location: PUNE(INDIA)

PostPosted: Thu Feb 15, 2007 11:43 am    Post subject: Re: Can I use same file as Input and Output for COBOL SORT ?
Reply with quote

Hi prav_06,
Thanks for your reply.
and about your doubt, the file MY-OUTPUT-FILE will not be empty. It would contain the records given by earlier part of program. So now I want it to be sorted within itself. So I guess, I can do it , the way I mentioned. right ?
Back to top
View user's profile Send private message
prav_06
Warnings : 1

Active User


Joined: 13 Dec 2005
Posts: 154
Location: The Netherlands

PostPosted: Thu Feb 15, 2007 11:47 am    Post subject:
Reply with quote

ParagChouguley,
Absolutely yes boss.... u can use the syntax.

Cheer's,

Thamilzan.
Back to top
View user's profile Send private message
William Thompson

Global Moderator


Joined: 18 Nov 2006
Posts: 3158
Location: Tucson AZ

PostPosted: Thu Feb 15, 2007 2:18 pm    Post subject:
Reply with quote

Prav,

what would the DD statement look like? DCB? DISP?

Bill
Back to top
View user's profile Send private message
prav_06
Warnings : 1

Active User


Joined: 13 Dec 2005
Posts: 154
Location: The Netherlands

PostPosted: Thu Feb 15, 2007 3:48 pm    Post subject:
Reply with quote

Bill,
The dd statement would be

//output dd dsn=xxx.yyyy.zzzz,disp=shr ====> Output

//work-file dd dsn=&&temp,etc for Sort wk file


Since the user had told that

Quote:
the file MY-OUTPUT-FILE will not be empty. It would contain the records given by earlier part of program.


And the DCB parameters would be given by the earlier step which creates it


Cheer's,
Thamilzan.
Back to top
View user's profile Send private message
Bitneuker

CICS Moderator


Joined: 07 Nov 2005
Posts: 1105
Location: The Netherlands at Hole 19

PostPosted: Thu Feb 15, 2007 4:19 pm    Post subject:
Reply with quote

Yep........the inputfile for a sort can be the outputfile too (both disp=shr). We often do this. The intermediate results are stored in the SORTWK's. Be aware though; if the sort failes your inputfile might get lost --> be empty icon_exclaim.gif So carefull with this logic and always take care of enough space for the SORTWK's.
Back to top
View user's profile Send private message
dick scherrer

Site Director


Joined: 23 Nov 2006
Posts: 19270
Location: Inside the Matrix

PostPosted: Thu Feb 15, 2007 10:00 pm    Post subject:
Reply with quote

Hello,

If the file named in the using/giving is created by this program for use in this program, you will not want to use disp=shr as shown above.
Back to top
View user's profile Send private message
dick scherrer

Site Director


Joined: 23 Nov 2006
Posts: 19270
Location: Inside the Matrix

PostPosted: Thu Feb 15, 2007 10:22 pm    Post subject:
Reply with quote

Hello again,

As your program is reading some input data and creating the file you need to sort, it will be FAR more effecient to code
INPUT PROCEDURE section-name
GIVING MY-FILE.

The way the code is currently all of the data in MY-FILE must be processed 3 times:
1. create the data
2. read the data into the sort (USING)
3. write the sorted output (GIVING)

If you use INPUT PROCEDURE you wil directly RELEASE records to the sort and only write MY-FILE as the last process.

If you use this approach, your JCL can then be DISP=(,CATLG,DELETE) which will keep the dataset for this and other jobs.
Back to top
View user's profile Send private message
prav_06
Warnings : 1

Active User


Joined: 13 Dec 2005
Posts: 154
Location: The Netherlands

PostPosted: Fri Feb 16, 2007 11:05 am    Post subject:
Reply with quote

Dick,

Quote:
the file MY-OUTPUT-FILE will not be empty. It would contain the records given by earlier part of program


As mentioned above the input files is created in the earlier part of the pgm, so when the control comes to the sorting part of the Cobol pgm, the file is available as a shared one for the Sort process to happen, so Technically what u say is rite from the JCL prespective specifying disp as (,CATLG,DELETE) for the ds, but logically when it comes to the sort step (In cobol pgm), the data set is created by the previous part of the program and is available for sorting .


Cheer's,

Thamilzan.
Back to top
View user's profile Send private message
jinal_mca

New User


Joined: 05 Jan 2007
Posts: 23
Location: Pune, India

PostPosted: Fri Feb 16, 2007 3:07 pm    Post subject: answer
Reply with quote

see dd stands for data definition..

and disp, dcb .. and lot more are key words of dd statement ..

dd is a jcl stmt which have some parameters and disp,dcb,dsn.. are
key word para..

syntax is :

ddname dd dsn=xxxx.xxx.xx, disp=(new,catlg,delte),
dcb=(lrecl=X,recfm=X,..),
space=(trk,(pri,sec),rlse,contig)

still if wanna know in detail ..thn let m know
c ya
Back to top
View user's profile Send private message
prav_06
Warnings : 1

Active User


Joined: 13 Dec 2005
Posts: 154
Location: The Netherlands

PostPosted: Fri Feb 16, 2007 3:15 pm    Post subject:
Reply with quote

jinal_mca,

What u r trying to say icon_question.gif , No body has asked for what DD stands for and watr the keyword parameters in it icon_exclaim.gif , please read the entire post an reply accordingly.



Cheer's,

Thamilzan.
Back to top
View user's profile Send private message
dick scherrer

Site Director


Joined: 23 Nov 2006
Posts: 19270
Location: Inside the Matrix

PostPosted: Fri Feb 16, 2007 10:11 pm    Post subject:
Reply with quote

Hello,

While the earlier part of the program is creating the data to be sorted it does NOT need to write to MY-FILE. A better alternative for the program as described is to do the processing to create a data record, then RELEASE each record to the SORT. This is done in the INPUT PROCEDURE section of the COBOL program.

Please look in the COBOL manual if you are not familiar with sort optons other than using/giving. Here is a bit from the COBOL manual from this site:
Quote:


The RELEASE statement transfers records from an input/output area to the
initial phase of a sorting operation.

The RELEASE statement can only be used within the range of an INPUT
PROCEDURE associated with a SORT statement.



It may take a short time to convert your code to use INPUT PROCEDURE but the things you would need to do are minor. Here is a little sample that i wrote earlier this week for a quick-n-dirty requirement. Because this was a one-time shot i did not check for successful sort completion and issue an error message if the sort failed (i was running this manually and would see if any problem occurred). For production jobs, i'd recommend checking SORT-RETURN for zero and issuing an appropriate message/condition code if the sort ever fails (this should be done for ANY internal sort that is in production code). My preference is to make the INPUT PROCEDURE a section (when there is processing on both sides of the sort, i also make my OUTPUT PROCEDURE a section.
Code:

     SORT SORT-FILE ON ASCENDING SORT-KEY-1       
                       DESCENDING SORT-KEY-2       
          INPUT PROCEDURE  0000-GET-PRICES         
          GIVING MY-FILE.       
     GOBACK.                                       
*                                                 
 0000-GET-PRICES [b]SECTION[/b].                         
*                                                 
 010-OPEN-FILES.                                   
     OPEN INPUT  PRIC-IN.                         

The performance improvement is quite significant.

If this file is "very" large, your process would also perform better if you created MY-FILE in COBOL then had a following step to sort the data.
Back to top
View user's profile Send private message
mmwife

Super Moderator


Joined: 30 May 2003
Posts: 1592

PostPosted: Sat Feb 17, 2007 11:15 pm    Post subject:
Reply with quote

Hi parag,

While it's permissable to use the same file for sort ip/op, if the sort abends during the o/p phase (a rarity, we hope) you lose your i/p file.

If you have a copy somewhere that's not a tragedy, but it is a bit inconvienent to recover.
Back to top
View user's profile Send private message
dick scherrer

Site Director


Joined: 23 Nov 2006
Posts: 19270
Location: Inside the Matrix

PostPosted: Sun Feb 18, 2007 4:19 am    Post subject:
Reply with quote

Hello,

As the "using" file is created in this same program, recovering it shouldn't be an issue.

To re-iterate - with using/giving the data in that particular file will be written as the "real" input is processed, read back in by the sort, and written again coming out of the sort.

Even though we CAN do some things, it does NOT mean we should do them. As we're in a COBOL program there will code to be run before and/or after the data is sorted. For code that runs before the data is sorted, INPUT PROCEDURE should be used - for code that requires the data in sequence OUTPUT PROCEDURE should be used. If there is no processing on either side, the sort shouldn't be inside a COBOL program (i.e. using/giving has no need to be "inside" a program) . This, of course, assumes that we're interested in saving MANY, MANY machine resources versus a few minutes of programmer time. The code will be written once - the system resources will be used EVERY time the program is executed . . . Once someone gets into the bad habit of "using/giving" it is hard to break - once someone is confortable with input/output procedures, coding is as easy as using/giving. On the good news side, there are installations that are determined to conserve system resources and will not permit a program with using/giving in the code to be promoted to UAT or Production icon_smile.gif

Here is a bit from the Fine COBOL Manual available via this forum:

Quote:
Use SORT . . . USING if you don't need to process the records in an input
file (or files) before they are released to the sort program. With SORT .
. . USING file-name, the compiler generates an input procedure to open the
file, read the records, release the records to the sort program, and close
the file.

The input file must not be open when the SORT statement begins execution.
If you want to process the records in the input file before they are
released to the sort program, use the INPUT PROCEDURE option of the SORT
statement.

Each input procedure must be contained in either paragraphs or sections.
For example, to release records from Working-Storage (a table) to the new
file:

SORT SORT-WORK-2
ON ASCENDING KEY SORT-KEY
INPUT PROCEDURE 600-SORT3-INPUT-PROC
.
.
.

600-SORT3-INPUT-PROC SECTION.
PERFORM WITH TEST AFTER
VARYING X1 FROM 1 BY 1 UNTIL X1 = 100
RELEASE SORT-WORK-2-AREA FROM TABLE-ENTRY (X1)
END-PERFORM.

An input procedure contains code for processing records and releasing them
to the sort operation. You might want to use an input procedure to:

? Release data items to the new file from Working-Storage

? Release records that have already been read in elsewhere in the
program

? Read records from an input file, select or process them, and release
them to the new file.

.

Use SORT . . . GIVING if you want DFSORT to transfer the sorted records
directly from the sort work file into another file without any further
processing. With SORT . . . GIVING file-name, the compiler generates an
output procedure to open the file, return the records, write the records,
and close the file. At the time the SORT statement is executed, the file
named with the GIVING option must not be open.

If, however, you want to select, edit, or otherwise modify the sorted
records before writing them from the sort work file into another file, use
the OUTPUT PROCEDURE option of the SORT statement.

In the output procedure, you must use the RETURN statement to make each
sorted record available to your program (the RETURN statement for a sort
file is similar to a READ statement for an input file). Your output
procedure can then contain any statements necessary to process the records
that are made available, one at a time, by the RETURN statement.

You can use RETURN INTO, instead of RETURN, to return and process the
records into Working-Storage or to an output area. You can also use the
AT END and END-RETURN phrases with the RETURN statement. The imperative
statements on the AT END phrase will execute after all the records have
been returned from the sort file. The END-RETURN explicit scope
terminator serves to delimit the scope of the RETURN statement.

When you code output procedures, remember that each output procedure must
include at least one RETURN or RETURN INTO statement. Also, each output
procedure must be contained in either a section or a paragraph.


If you have any questions on this, please let me know.
Back to top
View user's profile Send private message
mmwife

Super Moderator


Joined: 30 May 2003
Posts: 1592

PostPosted: Sun Feb 18, 2007 9:24 pm    Post subject:
Reply with quote

You're right, Dick. I missed that he's using an O/P created in the same pgm.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Compare yesterday's date to the one o... migusd SYNCSORT 8 Fri Sep 22, 2017 11:35 pm
No new posts Difference in SORT & ICETOOL Mohan Kothakota DFSORT/ICETOOL 5 Fri Sep 22, 2017 4:56 pm
No new posts it is possible output a SET value fro... Martylin JCL & VSAM 5 Fri Sep 22, 2017 7:08 am
No new posts Split 1 file into 10 output Files - S... Prasanth Kumar SYNCSORT 5 Sat Sep 16, 2017 12:02 am
No new posts OC4 in SORT Mohan Kothakota DFSORT/ICETOOL 2 Tue Sep 12, 2017 6:30 pm

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us