Start of Content Area

Shifting Field Contents  Locate the document in its SAP Library structure

You can shift the contents of a field using the following variants of the SHIFT statement. SHIFT moves field contents character by character.

Shifting a String by a Given Number of Positions

SHIFT <c> [BY <n> PLACES] [<mode>].

This statement shifts the field <c> by <n> positions. If you omit BY <n> PLACES, <n> is interpreted as one. If <n> is 0 or negative, <c> remains unchanged. If <n> exceeds the length of <c>, <c> is filled out with blanks. <n> can be variable.

With the different <mode> options, you can shift the field <c> in the following ways:

Shifts the field contents <n> places to the left and adds <n> blanks at the right-hand end of the field (default).

Shift <n> positions to the right and adds <n> blanks at the left-hand end of the field.

Shift <n> positions to the left so that <n> characters on the left appear on the right.

Example

DATA: T(10) VALUE 'abcdefghij',
STRING LIKE T.

STRING = T.
WRITE STRING.

SHIFT STRING.
WRITE / STRING.

STRING = T.
SHIFT STRING BY 3 PLACES LEFT.
WRITE / STRING.

STRING = T.
SHIFT STRING BY 3 PLACES RIGHT.
WRITE / STRING.

STRING = T.
SHIFT STRING BY 3 PLACES CIRCULAR.
WRITE / STRING.

Output:

abcdefghij

bcdefghij

defghij

   abcdefg

defghijabc

Shifting a Structure up to a Given String

SHIFT <c> UP TO <str> <mode>.

This statement searches the field contents of <c> until it finds the string <str> and shifts the field <c> up to the edge of the field. The <mode> options are the same as described above. <str> can be a variable.

If <str> is not found in <c>, SY-SUBRC is set to 4 and <c> is not shifted. Otherwise, SY-SUBRC is set to 0.

Example

DATA: T(10) VALUE 'abcdefghij',
STRING LIKE T,
STR(2) VALUE 'ef'.

STRING = T.
WRITE STRING.

SHIFT STRING UP TO STR.
WRITE / STRING.

STRING = T.
SHIFT STRING UP TO STR LEFT.
WRITE / STRING.

STRING = T.
SHIFT STRING UP TO STR RIGHT.
WRITE / STRING.

STRING = T.
SHIFT STRING UP TO STR CIRCULAR.
WRITE / STRING.

Output:

abcdefghij

efghij

efghij

    abcdef

efghijabcd

Shifting a Structure According to the First or Last Character

SHIFT <c> LEFT DELETING LEADING <str>.

SHIFT <c> RIGHT DELETING TRAILING <str>.

This statement shifts the field <c> to the left or to the right, provided the first character on the left or the last character on the right occur in <str>. The right or left of the field is then padded with blanks. <str> can be a variable.

Example

DATA: T(14) VALUE ' abcdefghij',
STRING LIKE T,
STR(6) VALUE 'ghijkl'.

STRING = T.
WRITE STRING.

SHIFT STRING LEFT DELETING LEADING SPACE.
WRITE / STRING.

STRING = T.
SHIFT STRING RIGHT DELETING TRAILING STR.
WRITE / STRING.

Output:

    abcdefghij

abcdefghij

        abcdef