Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

create range table from single values

hi all,

I have a number (several thousands) of single values. I want to create a range table from these single values (as you can see there are gaps in the numbers). What is the best way to achieve that?

Values I have (in an internal table):

1

2

3

5

6

9

12

13

14

15

20

...

should be converted to:

I BT 1 3

I BT 5 6

I BT 9 9 (or I EQ 9 )

I BT 12 15

...

thanks

ec

Tags:
Former Member replied

Herewith is code for reference:

  TYPES: BEGIN OF ST_NUM,
           NUM TYPE I,
         END OF ST_NUM.
  DATA: IT_NUM TYPE TABLE OF ST_NUM,
        WA_NUM LIKE LINE OF IT_NUM.

  WA_NUM-NUM = 1.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 2.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 3.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 5.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 6.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 9.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 12.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 13.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 14.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 15.
  APPEND WA_NUM TO IT_NUM.
  WA_NUM-NUM = 20.
  APPEND WA_NUM TO IT_NUM.

  DATA: NEXT TYPE SY-TABIX,
        WA_NEXT LIKE LINE OF IT_NUM,
        WA_CURR LIKE LINE OF IT_NUM,
        DIFF TYPE I.
  TYPES: BEGIN OF SEL,
           SIGN(1),
           OPTION(2),
           LOW TYPE I,
           HIGH TYPE I,
         END OF SEL.
  DATA:  S_SEL TYPE TABLE OF SEL,
         WA_SEL LIKE LINE OF S_SEL,
         JUMP  TYPE SY-TABIX.

  JUMP = 1.
  LOOP AT IT_NUM INTO WA_NUM.
    CHECK JUMP EQ SY-TABIX.
    NEXT = SY-TABIX + 1.
    READ TABLE IT_NUM INTO WA_NEXT INDEX NEXT.
    IF SY-SUBRC EQ 0.
      DIFF = WA_NEXT-NUM - WA_NUM-NUM.
      IF DIFF EQ 1.
        WA_SEL-LOW = WA_NUM-NUM.
        WA_SEL-SIGN = 'I'.
        WA_SEL-OPTION = 'BT'.
        WHILE DIFF EQ 1.
          WA_CURR = WA_NEXT.
          ADD 1 TO NEXT.
          READ TABLE IT_NUM INTO WA_NEXT INDEX NEXT.
          DIFF = WA_NEXT-NUM - WA_CURR-NUM.
        ENDWHILE.
        WA_SEL-HIGH = WA_CURR-NUM.
        APPEND WA_SEL TO S_SEL.
        CLEAR WA_SEL.
        JUMP = NEXT.
      ELSE.
        WA_SEL-LOW = WA_NUM-NUM.
        WA_SEL-SIGN = 'I'.
        WA_SEL-OPTION = 'EQ'.
        APPEND WA_SEL TO S_SEL.
        CLEAR WA_SEL.
        JUMP = NEXT.
      ENDIF.
    ELSE.
      WA_SEL-LOW = WA_NUM-NUM.
      WA_SEL-SIGN = 'I'.
      WA_SEL-OPTION = 'EQ'.
      APPEND WA_SEL TO S_SEL.
      CLEAR WA_SEL.
    ENDIF.
  ENDLOOP.

0 View this answer in context

Helpful Answer

by
Not what you were looking for? View more on this topic or Ask a question