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

Select some fields internal table

Former Member
0 Likes
1,369

Hi, i have an internal table "itab" with a lot of fields. table is sorted.

Like this

Header 1Header 2Header 3
a2132123
a312123
a312312
b132132
c32123
c1213

lets say i want the last lines of the different values of Header 1.

in this case, the last one where header 1 is a, then last one where is b, last one where it is c

a 312 312

b 132 132

c 12 13

is there any simple solution?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,313

hi bruno,

Try this code for the solution.

REPORT  ZR_TEST2.

TYPES: BEGIN OF TY,
         HEADER1 TYPE C LENGTH 10,
         HEADER2 TYPE C LENGTH 10,
         HEADER3 TYPE C LENGTH 10,
        END OF TY.

DATA: WA TYPE TY,
       WA1 TYPE TY,
       ITAB LIKE STANDARD TABLE OF WA,
       ITAB1 LIKE STANDARD TABLE OF WA.

WA-HEADER1 = 'a'.
WA-HEADER2 = '2132'.
WA-HEADER3 = '123'.
APPEND WA TO ITAB.
CLEAR WA.

WA-HEADER1 = 'a'.
WA-HEADER2 = '312'.
WA-HEADER3 = '123'.
APPEND WA TO ITAB.
CLEAR WA.

WA-HEADER1 = 'a'.
WA-HEADER2 = '312'.
WA-HEADER3 = '312'.
APPEND WA TO ITAB.
CLEAR WA.

WA-HEADER1 = 'b'.
WA-HEADER2 = '132'.
WA-HEADER3 = '132'.
APPEND WA TO ITAB.
CLEAR WA.

WA-HEADER1 = 'c'.
WA-HEADER2 = '32'.
WA-HEADER3 = '123'.
APPEND WA TO ITAB.
CLEAR WA.

WA-HEADER1 = 'c'.
WA-HEADER2 = '12'.
WA-HEADER3 = '13'.
APPEND WA TO ITAB.
CLEAR WA.

SORT ITAB BY header1.

LOOP AT ITAB INTO WA.   
   WA1-HEADER1 = WA-HEADER1.
   WA1-HEADER2 = WA-HEADER2.
   WA1-HEADER3 = WA-HEADER3.
  
   AT END OF HEADER1.   
     APPEND WA1 TO ITAB1.
   ENDAT.   
ENDLOOP.

WRITE: / 'FINAL'.

LOOP AT ITAB1 INTO WA.
   WRITE: / WA-header1, WA-header2, wa-header3
ENDLOOP.

11 REPLIES 11
Read only

0 Likes
1,313

that table is sorted only by key of the header 1 so maybe your result in a future will be not the correct...

init variable actual with the first row of the table

loop.

     save the actual value of header1 in a variable actual.

     when the next value is different append the preview row to result table..

endloop.

here a link for abap code https://training.sap.com/shop/certification/c_taw12_731-sap-certified-development-associate---abap-w...

regards.

Alberto

Read only

0 Likes
1,313

well, i can try to search the SAP FM and get a table for each one of those aaaa bb cccc. But then again i need to read the last line. i can get that by sy-tabix right?

and then read the index = sy-tabix.

Read only

Former Member
0 Likes
1,314

hi bruno,

Try this code for the solution.

REPORT  ZR_TEST2.

TYPES: BEGIN OF TY,
         HEADER1 TYPE C LENGTH 10,
         HEADER2 TYPE C LENGTH 10,
         HEADER3 TYPE C LENGTH 10,
        END OF TY.

DATA: WA TYPE TY,
       WA1 TYPE TY,
       ITAB LIKE STANDARD TABLE OF WA,
       ITAB1 LIKE STANDARD TABLE OF WA.

WA-HEADER1 = 'a'.
WA-HEADER2 = '2132'.
WA-HEADER3 = '123'.
APPEND WA TO ITAB.
CLEAR WA.

WA-HEADER1 = 'a'.
WA-HEADER2 = '312'.
WA-HEADER3 = '123'.
APPEND WA TO ITAB.
CLEAR WA.

WA-HEADER1 = 'a'.
WA-HEADER2 = '312'.
WA-HEADER3 = '312'.
APPEND WA TO ITAB.
CLEAR WA.

WA-HEADER1 = 'b'.
WA-HEADER2 = '132'.
WA-HEADER3 = '132'.
APPEND WA TO ITAB.
CLEAR WA.

WA-HEADER1 = 'c'.
WA-HEADER2 = '32'.
WA-HEADER3 = '123'.
APPEND WA TO ITAB.
CLEAR WA.

WA-HEADER1 = 'c'.
WA-HEADER2 = '12'.
WA-HEADER3 = '13'.
APPEND WA TO ITAB.
CLEAR WA.

SORT ITAB BY header1.

LOOP AT ITAB INTO WA.   
   WA1-HEADER1 = WA-HEADER1.
   WA1-HEADER2 = WA-HEADER2.
   WA1-HEADER3 = WA-HEADER3.
  
   AT END OF HEADER1.   
     APPEND WA1 TO ITAB1.
   ENDAT.   
ENDLOOP.

WRITE: / 'FINAL'.

LOOP AT ITAB1 INTO WA.
   WRITE: / WA-header1, WA-header2, wa-header3
ENDLOOP.

Read only

0 Likes
1,313

that's it! thanks mate! works great

Read only

0 Likes
1,313

Although this does work in this case, I prefer to take care of things like this manually. See what happens of you sort by header3 and do AT END OF header3 in the loop.

Rob

Read only

0 Likes
1,313

Hi

Interesting the code (easy), is possible, explain a little about this line code:

AT END OF HEADER1.   

     APPEND WA1 TO ITAB1.

   ENDAT.   

if possible.

thanks


Read only

0 Likes
1,313

Interesting, AT END have a isussue, if the column isn't the first of the table not work properly.

the Help Say..

thanks

Read only

0 Likes
1,313

within the AT...... ENDAT the fields are filled with '*'. therefore before this AT....ENDAT I am assigning values to work area wa1.

AT END ... is used here to get the last record  only.

Read only

0 Likes
1,313

abdul raheem mohammad wrote:

within the AT...... ENDAT the fields are filled with '*'. therefore before this AT....ENDAT I am assigning values to work area wa1.

AT END ... is used here to get the last record  only.

And that's the problem. Since the dawn of time, programmers have written code to do a specific task. We need to develop code that is more easily reusable.

After all, the dirty little secret of programmers is that a large portion of what we do is <CTL>C <CTL>V.

Rob

Read only

0 Likes
1,313

Any sugestion aboup a better way to do it?
Is better to check the lines of the itab with sy-tabix and then read the last one by it's index? is it possible to do it with an internal table? (it should have a header line)

sorrry the questions.

i'm starting in this whole new world of abap! all the best.

Read only

0 Likes
1,313

Like I said, I prefer to do it manually. Keep a work are with the last value of the field you are interested in. Compare the next value with the old value. When it changes, do what you need to do and replace the work area with the new value.

I'm sure there are better ways. This just what I have done since running into the noted problems with AT NEW, ON CHANGE OF, etc.

Rob