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

Loop at itab..........

Former Member
0 Likes
586

Hi Experts,

I have data in itab like :

Off. Prod. Qty. Value.

MUM A 13 1200

MUM C 14 1300

MUM B 9 990

DEL A 6 550

DEL E 21 1350

DEL B 125 9389

DEL C 35 7890

I am writting a classical reports and i want to print like

A B C D E F G

MUM 13 9 14

DEL 6 125 35 21

How to Loop the itab and how to write ? Pl. guide me....

Yusuf

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
560

Hi

Assumptions:

- you always have six letter A..F

- value in column 1 is always max. 3 chars

- value in column 2 is always max. 3 chars

- you want to show it in table like this:


    A   B   C   D   E   F
MUM 13  9   14
DEL 6   125 35      21

So, here we go:


CONSTANTS: lco_position_a TYPE i VALUE 4,
           lco_position_b TYPE i VALUE 8,
           lco_position_c TYPE i VALUE 12,
           lco_position_d TYPE i VALUE 16,
           lco_position_e TYPE i VALUE 20,
           lco_position_f TYPE i VALUE 24.

DATA: ltp_value_position TYPE i,
      lwa_data LIKE LINE OF itab.

WRITE AT lco_position_a 'A   B   C   D   E   F'.

SORT itab BY col1, col2. "put proper col names here
LOOP AT itab INTO lwa_itab.
  CASE lwa_itab-col2. "put proper col name here
    WHEN 'A'.
      ltp_value_position = lco_position_a. 
    WHEN 'B'.
      ltp_value_position = lco_position_b. 
    WHEN 'C'.
      ltp_value_position = lco_position_c. 
    WHEN 'D'.
      ltp_value_position = lco_position_d.
    WHEN 'E'.
      ltp_value_position = lco_position_e. 
    WHEN 'F'.
      ltp_value_position = lco_position_f. 
  ENDCASE.

  AT NEW lwa_itab-col1. "put proper col name here
    WRITE / lwa_itab-col1. "put proper col name here
  ENDAT.
  WRITE AT ltp_value_position lwa_itab-col2. "put proper col name here
ENDLOOP.

Haven't tested it though. Also if you have much more of the A..F letters or the number of these is not constant then additional logic for the auto-positioning would be required.

Rgds

Mat

4 REPLIES 4
Read only

chaouki_akir
Contributor
0 Likes
560

Hello,

1ST : write the first line "A B C D E F G"

LOOP at itab.
  if  sy-tabix eq 1.
* Start New line
     write / ...
  else. 
* Continue the Current line
     write at ...
  endif.


ENDLOOP.

2D : write the 2d and 3d lines ("MUM 13 9 14" and "DEL 6 125 35 21")

SORT itab by "field1" (name of the 1st field of itab  (with value 'MUM')
LOOP AT itab.
  AT NEW field1.
* Start New line
     write / field1.     
 ENDAT.
* Continue the Current line
  write at 10 itab-field3 (3d field of itab with values 13 or 9 or 14 for MUM)
ENDLOOP.

Cordialement,

Chaouki.

Read only

Former Member
0 Likes
561

Hi

Assumptions:

- you always have six letter A..F

- value in column 1 is always max. 3 chars

- value in column 2 is always max. 3 chars

- you want to show it in table like this:


    A   B   C   D   E   F
MUM 13  9   14
DEL 6   125 35      21

So, here we go:


CONSTANTS: lco_position_a TYPE i VALUE 4,
           lco_position_b TYPE i VALUE 8,
           lco_position_c TYPE i VALUE 12,
           lco_position_d TYPE i VALUE 16,
           lco_position_e TYPE i VALUE 20,
           lco_position_f TYPE i VALUE 24.

DATA: ltp_value_position TYPE i,
      lwa_data LIKE LINE OF itab.

WRITE AT lco_position_a 'A   B   C   D   E   F'.

SORT itab BY col1, col2. "put proper col names here
LOOP AT itab INTO lwa_itab.
  CASE lwa_itab-col2. "put proper col name here
    WHEN 'A'.
      ltp_value_position = lco_position_a. 
    WHEN 'B'.
      ltp_value_position = lco_position_b. 
    WHEN 'C'.
      ltp_value_position = lco_position_c. 
    WHEN 'D'.
      ltp_value_position = lco_position_d.
    WHEN 'E'.
      ltp_value_position = lco_position_e. 
    WHEN 'F'.
      ltp_value_position = lco_position_f. 
  ENDCASE.

  AT NEW lwa_itab-col1. "put proper col name here
    WRITE / lwa_itab-col1. "put proper col name here
  ENDAT.
  WRITE AT ltp_value_position lwa_itab-col2. "put proper col name here
ENDLOOP.

Haven't tested it though. Also if you have much more of the A..F letters or the number of these is not constant then additional logic for the auto-positioning would be required.

Rgds

Mat

Read only

0 Likes
560

Hi Mateusz,

Thanks, problem solved. Rewarded.

Yusuf

Read only

0 Likes
560

Hi

Glad I could help. Thanks for the points.

Rgds

Mat