Writing Values with Offset Specifications

You can specify offset and length for elementary data objects in any ABAP statement (see Specifying Offset Values fo Data Objects). The syntax of the WRITE TO statement is as follows:

Syntax

WRITE <f1>[+<o1>][(<l1>)] TO <f2>[+<o2>][(<l2>)].

The contents of the part of the field <f1> which begins at position <o1>+1 and has a length of <l1> are assigned to field <f2> where they overwrite the section which begins at position <o2>+1 and has a length of <l2>.

In the WRITE TO statement, the offset and length specifications of the target field can be variables.

SAP recommends that you assign values with offset and length specifications only between non-numeric fields. For numeric fields, the results can be meaningless.

DATA: STRING(20),
NUMBER(8) TYPE C VALUE '123456',
OFFSET TYPE I VALUE 8,
LENGTH TYPE I VALUE 12.

WRITE NUMBER+(6) TO STRING+OFFSET(LENGTH) LEFT-JUSTIFIED.
WRITE: / STRING.
CLEAR STRING.

WRITE NUMBER+(6) TO STRING+OFFSET(LENGTH) CENTERED.
WRITE: / STRING.
CLEAR STRING.

WRITE NUMBER TO STRING+OFFSET(LENGTH) RIGHT-JUSTIFIED.
WRITE: / STRING.
CLEAR STRING.

This produces the following output:

        123456

           123456

              123456

The first WRITE statement writes the first 6 positions of the field NUMBER left-justified to the last 12 positions of the field STRING.

The second WRITE statement writes the first 6 positions of NUMBER centered to the last 12 positions of STRING.