# Write to arbitrary work file

**URL:** <https://techcommunity.softwareag.com/t/write-to-arbitrary-work-file/94029>\
**Category:** Forum\
**Tags:** Adabas-Natural, Natural, Natural-on-Mainframes\
**Created:** [June 30, 2010, 8:56pm UTC](https://techcommunity.softwareag.com/t/write-to-arbitrary-work-file/94029 "2010-06-30T20:56:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mWever](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@mWever](https://techcommunity.softwareag.com/u/mWever)\
**Post date:** [June 30, 2010, 8:56pm UTC](https://techcommunity.softwareag.com/t/write-to-arbitrary-work-file/94029/1 "2010-06-30T20:56:45Z")

</div>

I am writing a subprogram that will write some wrapper information to a work file. I want to be able to call this subprogram multiple times from the same program to write to different work files. Is there any way to do this?

My first thought was to supply the work file number as a variable, but Natural complained when I did that. Then I tried using this code:

```auto
DECIDE ON FIRST #P-WORK-FILE
  VALUE 1 WRITE WORK FILE 1 VARIABLE #OUTPUT
  VALUE 2 WRITE WORK FILE 2 VARIABLE #OUTPUT
  VALUE 3 WRITE WORK FILE 3 VARIABLE #OUTPUT
  /* AND SO ON UP TO WORK FILE 9...
  NONE VALUE IGNORE
END-DECIDE
```

This worked well in programs that only generated output files. I ran into a problem, though, when I tried to use the subprogram in a program that reads work file 1 and writes work file 2. When I called the subprogram, it would close work file 1 for input and re-open it as output instead, even though the subprogram never executed the line that actually performs a write to work file 1. It appears that Natural is scanning through the subprogram for any write work file statements and when it finds one, it opens that file for output.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/techcommunity/original/3X/c/d/cd19967bb605d28a72d77eb83a1cae22f3df2eb8.svg) [@system](https://techcommunity.softwareag.com/u/system)\
**Post date:** [July 1, 2010, 3:59am UTC](https://techcommunity.softwareag.com/t/write-to-arbitrary-work-file/94029/2 "2010-07-01T03:59:42Z")

</div>

Q1) What are the NTWORK AM/OPEN/CLOSE/DISP settings ?  
Q2) Which operating system are you using?  
[http://techcommunity.softwareag.com/ecosystem/documentation/natural/nat426mf2/parms/work.htm#work\_OPEN](http://techcommunity.softwareag.com/ecosystem/documentation/natural/nat426mf2/parms/work.htm#work_OPEN)

---

<div class="post-metadata">

**Author:** ![Douglas\_Kelly](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/douglas_kelly/32/102_2.png) [@Douglas\_Kelly](https://techcommunity.softwareag.com/u/Douglas_Kelly)\
**Post date:** [July 1, 2010, 8:52am UTC](https://techcommunity.softwareag.com/t/write-to-arbitrary-work-file/94029/3 "2010-07-01T08:52:49Z")

</div>

See [http://techcommunity.softwareag.com/ecosystem/documentation/natural/nat426mf2/sm/definewo.htm#Work\_Files\_under\_OS390](http://techcommunity.softwareag.com/ecosystem/documentation/natural/nat426mf2/sm/definewo.htm#Work_Files_under_OS390)

With the DEFINE WORK FILE and USR2021N, you should be able to change the file that is written to without having to use different file numbers.

---

<div class="post-metadata">

**Author:** ![mWever](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@mWever](https://techcommunity.softwareag.com/u/mWever)\
**Post date:** [July 7, 2010, 2:37am UTC](https://techcommunity.softwareag.com/t/write-to-arbitrary-work-file/94029/4 "2010-07-07T02:37:46Z")

</div>

I’m using Z/VSE. I don’t think that DEFINE WORK FILE would provide any advantage to me because I’m not trying to allocate multiple files to a single file number (although I have other applications where that may be helpful).

I found that changing the OPEN mode to ACC allowed my logic to work as I expected in this situation. I’ll have to do some testing to make sure it works correctly in other situations, but I think that my problem is solved.

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/techcommunity/original/3X/c/d/cd19967bb605d28a72d77eb83a1cae22f3df2eb8.svg) [@system](https://techcommunity.softwareag.com/u/system)\
**Post date:** [April 2, 2021, 2:58pm UTC](https://techcommunity.softwareag.com/t/write-to-arbitrary-work-file/94029/5 "2021-04-02T14:58:43Z")

</div>


