SHIFT (ABAP Keyword)

SHIFT is a keyword used in SAP ABAP programming.This tutorial covers its introduction & syntax details.

SHIFT

Variants

1. SHIFT c.
2. SHIFT c BY n PLACES.
3. SHIFT c UP TO c1.
4. SHIFT c LEFT DELETING LEADING c1.
5. SHIFT c RIGHT DELETING TRAILING c1.

Variant 1
SHIFT c.

Additions

1. … CIRCULAR
2. … RIGHT
3. … LEFT

Effect
Shifts the field c one position to the left. Omits the first letter and inserts a blank on the right.

Example

DATA ALPHABET(10) VALUE ‘ABCDEFGHIJ’.
SHIFT ALPHABET.

ALPHABET now contains ‘BCDEFGHIJ ‘ .

Addition 1
… CIRCULAR

Effect
Shifts the field c , so that the “lost” character on the left appears on the right.

Example

DATA ALPHABET(10) VALUE ‘ABCDEFGHIJ’.
SHIFT ALPHABET CIRCULAR.

ALPHABET now contains ‘BCDEFGHIJA’ .

Addition 2
… RIGHT

Effect
Shifts the field c to the right instead of the left.

Example

DATA ALPHABET(10) VALUE ‘ABCDEFGHIJ’.
SHIFT ALPHABET RIGHT CIRCULAR.

ALPHABET now contains ‘JABCDEFGHI’ . The additional specifications can also be combined.

Addition 3
… LEFT

Effect
Shifts the field c to the left. Since this is the default, you can omit this addition.

Variant 2
SHIFT c BY n PLACES.

Additions

Similar to variant 1.

Effect
Shifts the field c by n positions to the left and inserts blanks on the right.

Example

DATA ALPHABET(10) VALUE ‘ABCDEFGHIJ’,
FIVE TYPE I VALUE 5.
SHIFT ALPHABET BY FIVE PLACES.

ALPHABET now contains ‘FGHIJ ‘ .

Note
If n = 0 or has a negative value, c remains unchanged.
If n is greater than the field length of c , c is padded with blanks.

Variant 3
SHIFT c UP TO c1.

Additions

Similar to variant 1.

Effect
Searches
c for the character string in c1 (starting on the left!). If it finds a
match, it shifts the contents of c to the left until the character
string concerned appears on the left. If no match is found, c remains
unchanged.

The return code value is set as follows:

SY-SUBRC = 0 c1 was found in c .
SY_SUBRC = 4 c1 was not found in c ; c remains unchanged.

Example

DATA ALPHABET(10) VALUE ‘ABCDEFGHIJ’,
THREE(3) VALUE ‘DEF’,
FOUR(4) VALUE ‘DEF ‘.
SHIFT ALPHABET UP TO FOUR.

SY-SUBRC is now set to 4 and the field ALPHABET remains unchanged.

SHIFT ALPHABET UP TO THREE CIRCULAR.

SY-SUBRC is now set to 0 and the field ALPHABET contains ‘DEFGHIJABC’.

Note
The operation searches c for the full length of the string in c1 , together with any existing blanks.

Variant 4
SHIFT c LEFT DELETING LEADING c1.
Variant 5
SHIFT c RIGHT DELETING TRAILING c1.

Effect
Shifts
the field c to the left or right so that it begins or ends with a
character which occurs in c1 and pads it with blanks accordingly.
If c does not begin or end with a character from c1 , c remains unchanged.

Example

DATA: ALPHABET(15) VALUE ‘ ABCDEFGHIJ’,
M1(4) VALUE ‘ABCD’,
M2(6) VALUE ‘BJJCA ‘.
SHIFT ALPHABET LEFT DELETING LEADING M1.

The field ALPHABET is unchanged.

SHIFT ALPHABET LEFT DELETING LEADING SPACE.

The field ALPHABET now contains ‘ABCDEFGHIJ ‘ .

SHIFT ALPHABET RIGHT DELETING TRAILING M2.

The field ALPHABET now contains ‘ ABCDEFGHI’ .

Note
Performance
The use of the SHIFT command in WHILE loops should be avoided for performance reasons.
Shifting
a field one position to the right or left requires approx. 5 msn
(standardized microseconds), while cyclical shifting requires approx. 7
msn. The variant … LEFT DELETING LEADING … needs about 3,5 msn, the
variant …RIGHT DELETING TRAILING … about 4,5 msn.