Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

delete adjacent duplicates retaining spaces

Former Member
0 Likes
1,389

Hi folks,

I have a requirement to delete adjacent duplicates from a table comparing a field only if it has value.

DELETE ADJACENT DUPLICATES FROM itab COMPARING matnr.

to be precise, i don't want to delete the data line from itab if 2 records have matnr as SPACE.

could someone give me the best possible solution?

Thanks,

Zid.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,157

HI Ziden,

I don't know why u need 2 Spaces but for this requirement try the below code.

itab1[] = itab2[].

sort itab1 by matnr.

delete adjacent duplicates from itab1 comparing matnr.

delete itab1 where matnr is initial.

delete itab2 where matnr is not initial.

append lines of itab2 to itab1.

itab1 is your final internal table.

Hi folks,

I have a requirement to delete adjacent duplicates from a table comparing a field only if it has value.

DELETE ADJACENT DUPLICATES FROM itab COMPARING matnr.

to be precise, i don't want to delete the data line from itab if 2 records have matnr as SPACE.

could someone give me the best possible solution?

Thanks,

Zid.

6 REPLIES 6
Read only

Former Member
0 Likes
1,158

HI Ziden,

I don't know why u need 2 Spaces but for this requirement try the below code.

itab1[] = itab2[].

sort itab1 by matnr.

delete adjacent duplicates from itab1 comparing matnr.

delete itab1 where matnr is initial.

delete itab2 where matnr is not initial.

append lines of itab2 to itab1.

itab1 is your final internal table.

Read only

0 Likes
1,157

Hi!

You are correct...

Only change would be,

"delete itab1 where matnr is initial." should come after "sort itab1 by matnr."

So your code is:

itab1[] = itab2[].

sort itab1 by matnr.

delete itab1 where matnr is initial.

delete adjacent duplicates from itab1 comparing matnr.

delete itab2 where matnr is not initial.

append lines of itab2 to itab1.

Regards,

KS

Read only

0 Likes
1,157

Try this logic.


SORT itab BY fld1.

LOOP AT itab INTO wa WHERE fld1 IS INITIAL.
   APPEND wa TO itab1.
   CLEAR wa.
ENDLOOP.

DELETE ADJACENT DUPLICATES FROM itab COMPARING fld1.

append lines of itab1 to itab.

SORT itab BY fld1.

Read only

0 Likes
1,157

you mean :

LOOP AT itab INTO wa WHERE fld1 IS NOT INITIAL.  "Adding NOT

   APPEND wa TO itab1.

   CLEAR wa.

ENDLOOP.


DELETE ADJACENT DUPLICATES FROM itab1 COMPARING fld1.  "changing from itab to itab1

delete itab where matnr is  not initial.

append lines of itab1 to itab.

Regards,

KS

Read only

0 Likes
1,157

i just gave matnr as example.

My actual req is with a z-text field.

How i wish SAP had given a provision to have it in the syntax itself.

like...delete adujcent duplicates from itab3 comparing matnr IGNORING spaces!?!

seems this is optimum in the absense of above... i strictly wanted to avoid loops.

Thanks.

Read only

Former Member
0 Likes
1,157

hi try this

1. define itab2 , itab 3 same as itab1.

2. loop at itab1 into wa1

      if wa1_matnr =  ' '.

         append wa1 to itab2

     else.

      append wa1 to itab3.

endif.

3. refresh itab1

4 delete adujcent duplicates from itab3 comparing matnr

5 append itab3 to itab1

    append itab2 to itab1

6. sort itab1 by matnr.

hope it will work

suresh