Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Obsolete Language Elements →  Obsolete Program Flow →  Obsolete Control Structures 

DO - VARYING

Short Reference

Other versions: 7.31 | 7.40 | 7.54

Obsolete Syntax

DO ... VARYING dobj FROM dobj1 NEXT dobj2 [RANGE range]
      [VARYING ...].
  [statement_block]
ENDDO.

Effect

The VARYING addition assigns a new value to a dobj variable for each run of a DO loop. It can be used more than once in a DO statement.

dobj1 and dobj2 are the first two data objects in a sequence of data objects that are the same distance apart in the memory. The data types of data objects dobj, dobj1, and dobj2 must be flat.

In Unicode programs, dobj, dobj1, and dobj2 must be compatible. Furthermore, in Unicode programs, dobj1 and dobj2 must either be structure components that belong to the same structure, or subareas of the same data object specified by offset/length specifications.

In the first loop pass, the content of data object dobj1 is assigned to dobj. In the second loop pass, the content of data object dobj2 is assigned. In the subsequent loops, dobj is assigned the content of the data object that is the same distance in the memory from the data object previously assigned, as dobj2 is from dobj1. Type conversion does not take place.

If the processing block is exited correctly using ENDDO, CHECK, or EXIT, the content of variable dobj is assigned to the data object previously assigned (dobj1 or dobj2) without conversion at the end of the loop pass. If it is exited using a different statement such as RETURN or RAISE EXCEPTION, this assignment does not take place.

The RANGE addition determines the memory area that can be processed using the VARYING addition. After RANGE, an elementary data object range of type c, n, or x or a structure can be specified. The memory area of range must include those of dobj1 and dobj2. In deep structures, the deep components are excluded from the area permitted. The DO loop must be ended before non-permitted memory areas are accessed, that is, areas outside of range or its deep components. Otherwise, an untreatable exception may occur.

If RANGE is not specified explicitly, the memory area permitted is determined as follows:

  • In non-Unicode programs, the permitted memory area of dobj1 extends to the limit of the current data area of the ABAP program. If the RANGE addition is not specified, there is a danger of unintentionally overwriting the memory.
  • In Unicode programs, RANGE can only be omitted if it can be statically determined that dobj1 and dobj2 are components of the same structure. The memory area permitted is then determined from the smallest substructure that contains dobj1 and dobj2.

Note

Instead of the VARYING addition, the ASSIGN statement should be used in the loop with the INCREMENT addition.


Example

In the first DO loop, subareas of data object text are processed by means of offset/length access. In Unicode programs, the RANGE addition must be specified here. In the second DO loop, the program accesses the components of data object text. In this case, it is not necessary to specify RANGE. The third DO loop shows how the functions of the second loop can be programmed using the ASSIGN INCREMENT statement.

DATA: BEGIN OF text, 
        word1 TYPE c LENGTH 4 VALUE 'AAAA', 
        word2 TYPE c LENGTH 4 VALUE 'BBBB', 
        word3 TYPE c LENGTH 4 VALUE 'CCCC', 
        word4 TYPE c LENGTH 4 VALUE 'DDDD', 
      END OF text. 

DATA: word  TYPE c LENGTH 4, 
      char1 TYPE c LENGTH 1, 
      char2 TYPE c LENGTH 1, 
      leng TYPE i. 

FIELD-SYMBOLS <word> LIKE text-word1. 
DATA inc TYPE i. 

DESCRIBE FIELD text LENGTH leng IN CHARACTER MODE. 
leng = leng / 2. 

DO leng TIMES VARYING char1 FROM text(1) 
                           NEXT text+2(1) RANGE text 
              VARYING char2 FROM text+1(1) 
                            NEXT text+3(1) RANGE text. 
  WRITE: char1, char2. 
  char1 = 'x'. 
  char2 = 'y'. 
ENDDO. 

DO 4 TIMES VARYING word FROM text-word1 NEXT text-word2. 
  WRITE / word. 
ENDDO. 

DO. 
  inc = sy-index  - 1. 
  ASSIGN text-word1 INCREMENT inc TO <word> RANGE text. 
  IF sy-subrc = 0. 
    WRITE / <word>. 
  ELSE. 
    EXIT. 
  ENDIF. 
ENDDO. 

Exceptions


Non-Catchable Exceptions

  • Cause: Illegal access to deep components within the area specified by the RANGE addition.
    Runtime Error: DO_WHILE_VARY_ILLEGAL_ACCESS
  • Cause: Access to data outside the area specified by the RANGE addition.
    Runtime Error: DO_WHILE_VARY_NOT_IN_RANGE