# Filtering out Matching records from two work files

**URL:** <https://techcommunity.softwareag.com/t/filtering-out-matching-records-from-two-work-files/177361>\
**Category:** Forum\
**Tags:** Adabas-Natural, Natural, Natural-on-Mainframes\
**Created:** [July 10, 2014, 11:47am UTC](https://techcommunity.softwareag.com/t/filtering-out-matching-records-from-two-work-files/177361 "2014-07-10T11:47:23Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shashank\_Gite1](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/shashank_gite1/32/351_2.png) [@Shashank\_Gite1](https://techcommunity.softwareag.com/u/Shashank_Gite1)\
**Post date:** [July 10, 2014, 11:47am UTC](https://techcommunity.softwareag.com/t/filtering-out-matching-records-from-two-work-files/177361/1 "2014-07-10T11:47:23Z")

</div>

If we are reading two work files which are not in sorted order, we need to write the matching records into third file. What is the efficient way to do that?

I only have one approach -

Read work file 1  
Read work file 2  
If work file 1 policy = work file 2 policy  
write work file 3  
escape bottom  
End-if  
End-work  
End-work

This approach seems to have performance issue. Please let me know if there is any efficient approach.

---

<div class="post-metadata">

**Author:** ![Giles](https://avatars.discourse-cdn.com/v4/letter/g/c68b51/32.png) [@Giles](https://techcommunity.softwareag.com/u/Giles)\
**Post date:** [July 10, 2014, 2:36pm UTC](https://techcommunity.softwareag.com/t/filtering-out-matching-records-from-two-work-files/177361/2 "2014-07-10T14:36:04Z")

</div>

Sort both the two input files and code a two file match. The algorithm is simple.

---

<div class="post-metadata">

**Author:** ![Shashank\_Gite1](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/shashank_gite1/32/351_2.png) [@Shashank\_Gite1](https://techcommunity.softwareag.com/u/Shashank_Gite1)\
**Post date:** [July 10, 2014, 2:38pm UTC](https://techcommunity.softwareag.com/t/filtering-out-matching-records-from-two-work-files/177361/3 "2014-07-10T14:38:45Z")

</div>

> [@Giles](#):
>
> Sort both the two input files and code a two file match. The algorithm is simple.

Okay Giles! So you mean I will add a SORT step before executing Natural program and then use the same logic I posted here?

---

<div class="post-metadata">

**Author:** ![Giles](https://avatars.discourse-cdn.com/v4/letter/g/c68b51/32.png) [@Giles](https://techcommunity.softwareag.com/u/Giles)\
**Post date:** [July 10, 2014, 2:44pm UTC](https://techcommunity.softwareag.com/t/filtering-out-matching-records-from-two-work-files/177361/4 "2014-07-10T14:44:44Z")

</div>

yes sort the two input files, if they have to be preserved sort to two temp inut files then.

Read WF1  
Read WF2  
Repeat until EOF WF1 and EOF WF2  
if key WF1 = key WF2  
write new file  
else  
if key WF1 \< key WF2  
read WF1  
else  
if key WF1 \> WF2  
read WF2  
end-if  
end-if  
end-if  
end-repeat

Check for EOF and set key to high values at EOF

---

<div class="post-metadata">

**Author:** ![Shashank\_Gite1](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/shashank_gite1/32/351_2.png) [@Shashank\_Gite1](https://techcommunity.softwareag.com/u/Shashank_Gite1)\
**Post date:** [July 10, 2014, 2:52pm UTC](https://techcommunity.softwareag.com/t/filtering-out-matching-records-from-two-work-files/177361/5 "2014-07-10T14:52:43Z")

</div>

> [@Giles](#):
>
> yes sort the two input files, if they have to be preserved sort to two temp inut files then.
> 
> Read WF1  
> Read WF2  
> Repeat until EOF WF1 and EOF WF2  
> if key WF1 = key WF2  
> write new file  
> else  
> if key WF1 \< key WF2  
> read WF1  
> else  
> if key WF1 \> WF2  
> read WF2  
> end-if  
> end-if  
> end-if  
> end-repeat
> 
> Check for EOF and set key to high values at EOF

Okay got it! So here Read WF1 and Read WF2 mean READ WORK FILE 1 ONCE and READ WORK FILE 2 ONCE … right?

---

<div class="post-metadata">

**Author:** ![Giles](https://avatars.discourse-cdn.com/v4/letter/g/c68b51/32.png) [@Giles](https://techcommunity.softwareag.com/u/Giles)\
**Post date:** [July 10, 2014, 3:03pm UTC](https://techcommunity.softwareag.com/t/filtering-out-matching-records-from-two-work-files/177361/6 "2014-07-10T15:03:30Z")

</div>

Right!

READ WORK x ONCE  
AT END  
MOVE all H’FF’ TO WFxKEY  
END-ENDFILE

---

<div class="post-metadata">

**Author:** ![Chris\_Mayor](https://avatars.discourse-cdn.com/v4/letter/c/5e9695/32.png) [@Chris\_Mayor](https://techcommunity.softwareag.com/u/Chris_Mayor)\
**Post date:** [July 16, 2014, 5:12pm UTC](https://techcommunity.softwareag.com/t/filtering-out-matching-records-from-two-work-files/177361/7 "2014-07-16T17:12:09Z")

</div>

The problem could be solved using sort only,

write the first of the duplicate pair to the output file which could then be processed by the next step,

avoids writing any code.

---

<div class="post-metadata">

**Author:** ![Giles](https://avatars.discourse-cdn.com/v4/letter/g/c68b51/32.png) [@Giles](https://techcommunity.softwareag.com/u/Giles)\
**Post date:** [July 18, 2014, 9:16am UTC](https://techcommunity.softwareag.com/t/filtering-out-matching-records-from-two-work-files/177361/8 "2014-07-18T09:16:34Z")

</div>

I think the OP has obtained all the answers to his interview questions/college project now. :roll:

---

<div class="post-metadata">

**Author:** ![Shashank\_Gite1](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/shashank_gite1/32/351_2.png) [@Shashank\_Gite1](https://techcommunity.softwareag.com/u/Shashank_Gite1)\
**Post date:** [July 18, 2014, 9:29am UTC](https://techcommunity.softwareag.com/t/filtering-out-matching-records-from-two-work-files/177361/9 "2014-07-18T09:29:20Z")

</div>

Giles! You are right… 🙂  
I got many solutions but just waiting for trying everything. Unfortunately not having any platform to try them… I love discussing on this forum… will have many more topics to discuss once system is in my hand… 😃

---

<div class="post-metadata">

**Author:** ![Giles](https://avatars.discourse-cdn.com/v4/letter/g/c68b51/32.png) [@Giles](https://techcommunity.softwareag.com/u/Giles)\
**Post date:** [July 18, 2014, 9:42am UTC](https://techcommunity.softwareag.com/t/filtering-out-matching-records-from-two-work-files/177361/10 "2014-07-18T09:42:03Z")

</div>

It would probably have been polite to explain this when you started all your threads.  
I’ll send you my invoice for consultancy now 😉

---

<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, 3:31pm UTC](https://techcommunity.softwareag.com/t/filtering-out-matching-records-from-two-work-files/177361/11 "2021-04-02T15:31:31Z")

</div>


