MANIPULATING DATA
02-Sep-09 Kaavian Systems
Manipulating Character Data
String manipulation is the process of manipulating the values to the
required format, using various manipulating commands like:
Concatenate Split Shift
Condense Translate Convert Text
Overlay Replace Search
02-Sep-09 Kaavian Systems 2
Concatenate
Combines two or more separate strings into one.
CONCATENATE <c1> ... <cn> INTO <c> [SEPARATED BY <s>].
Examples
DATA: file_path(5) VALUE ‘/tmp/’,
name(8) VALUE ‘MATERIAL’,
ext(4) VALUE ‘.txt’,
file_name(50).
CONCATENATE file_path name ‘_’ sy-datum ‘_’ sy-uzeit ext
INTO file_name.
file_name has value “/tmp/MATERIAL_20060222_064430.txt”.
02-Sep-09 Kaavian Systems 3
Split
Split a character string into two or more smaller strings.
– SPLIT <c> AT <del> INTO <c1> ... <cn>.
Example
DATA: file_name(30) TYPE C VALUE ‘tmp/[Link]’,
directory(10) TYPE C,
name(12) TYPE C,
separator(1) VALUE ‘/’.
SPLIT file_names AT delimiter INTO directory name.
Now directory contains “tmp” and name contains “[Link]”.
02-Sep-09 Kaavian Systems 4
Split
SPLIT <c> AT <del> INTO TABLE <itab>.
system adds a new line to the internal table <itab> for each part of the
string
Example
TYPES: BEGIN OF struct,
word(20),
END OF struct.
DATA: itab TYPE STANDARD TABLE OF struct.
02-Sep-09 Kaavian Systems 5
Split
SPLIT ‘One#Two#Three#’ AT ‘#’ INTO TABLE itab.
Now Internal Table itab has three rows. The first row
contains “One”, second row contains “Two” and third row
contains “Three”.
02-Sep-09 Kaavian Systems 6
Shift
Shift the contents of a field, character by character.
SHIFT <c> [BY <n> PLACES] [<LEFT/RIGHT/CIRCULAR>].
LEFT shifts the field contents <n> places to the left and adds
<n> blanks at the right-hand end of the field (default).
RIGHT shift <n> positions to the right and adds <n> blanks at
the left-hand end of the field.
CIRCULAR shift <n> positions to the left so that <n> characters
on the left appear on the right.
02-Sep-09 Kaavian Systems 7
Shift
SHIFT <c> UP TO <str> <mode>.
searches the field contents of <c> until it finds the string <str> and
shifts the field <c> up to the edge of the field.
SHIFT <c> LEFT DELETING LEADING <str>.
SHIFT <c> RIGHT DELETING TRAILING <str>.
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.
02-Sep-09 Kaavian Systems 8
Condense
Deletes redundant spaces from a string.
CONDENSE <c> [NO-GAPS].
Removes any leading blanks in the field <c> and replaces
other sequences of blanks by exactly one blank. Result is a left-
justified sequence of words, each separated by one blank.
NO-GAPS is specified, all blanks are removed.
02-Sep-09 Kaavian Systems 9
Translate
Converts characters into upper or lower case, or uses substitution
rules to convert all occurrences of one character to another character
TRANSLATE <c> TO UPPER CASE/LOWER CASE.
TRANSLATE <c> USING <r>.
replaces all characters in field <c> according to the substitution
rule stored in field <r>.
replaces all characters in field <c> according to the substitution
rule stored in field <r>.
02-Sep-09 Kaavian Systems 10
Convert Text
Converts strings into a format that can be sorted alphabetically.
CONVERT TEXT <c> INTO SORTABLE CODE <sc>.
field <c> must be of type C and the field <sc> must be of type X with a
minimum size of 16 times the size of <c>.
02-Sep-09 Kaavian Systems 11
Overlay
Overlays one string with another
– OVERLAY <c1> WITH <c2>.
– The contents of the field c2 overlay the field c1 in all positions
where c1 has the value SPACE; c2 itself remains unchanged.
– OVERLAY <c1> WITH <c2> [ONLY <c3>].
– The contents of the field c2 overlay the field c1 only in those
positions where c1 has one of the characters existing as a value in
c3; the fields c2 and c3 remain unchanged.
02-Sep-09 Kaavian Systems 12
Replace
Replace a string in a field with a different string
– REPLACE <str1> WITH <str2> INTO <c> [LENGTH <l>].
Replaces the first occurrence of the contents of field str1 in field c
with the contents of field str2.
02-Sep-09 Kaavian Systems 13
Overlay
Example
DATA : fname(50) VALUE ‘/tmp/mat&.txt’.
REPLACE ‘&’ WITH sy-datum INTO fname.
Now fname has value “/tmp/[Link]”.
02-Sep-09 Kaavian Systems 14
Search
Search a character field for a particular pattern.
– SEARCH <c> FOR <str> <options>.
<options> are ABBREVIATED - first letter of the word and the
string <str> must be the same
STARTING AT n1 - starting at position <n1>
ENDING AT n2 - up to position <n2>
AND MARK - converted to upper case
02-Sep-09 Kaavian Systems 15
Offset
Offset- or length-based access is supported for character-type single
fields, strings and single fields of types X and XSTRING.
For character-type fields and fields of type STRING, offset and length
are interpreted on a character-by-character basis.
Only for types X and XSTRING, the values for offset and length are
interpreted in bytes.
02-Sep-09 Kaavian Systems 16
Conditional Operators
IF, ELSE, ELSEIF, ENDIF
Syntax
IF <condition1>.
<statement block>
ELSEIF <condition2>
<statement block>.
ELSEIF <condition3>.
<statement block>
.....
ELSE.
<statement block>
ENDIF.
02-Sep-09 Kaavian Systems 17
Conditional Operators
CASE Statement
Syntax
CASE <f>.
WHEN <f11> [OR <f 12> OR ...].
<Statement block>
WHEN <f21>.[OR <f 22> OR ...]
<Statement block>
WHEN <f31> [OR <f 32> OR ...].
<statement block>
WHEN ...
......
WHEN OTHERS.
<statement block>
ENDCASE.
02-Sep-09 Kaavian Systems 18
Looping Commands
Unconditional Loops
Syntax
DO [<n> TIMES] [VARYING <f> FROM <f1> NEXT <f 2>].
<Statement block>
ENDDO.
Conditional Loops
Syntax
WHILE <condition> [VARY <f> FROM <f1> NEXT <f 2>].
<statement block>
ENDWHILE.
02-Sep-09 Kaavian Systems 19
Comparison Operators
IS INTIAL
IS BETWEEN
NOT
Comparing Character fields
CO ------Contains only
CN-------Contains not only
CA-------Contains Any
NA-------Contains not any
CS-------Contains String
NS-------Contains Not string
CP-------Contains pattern
NP-------Contains no pattern
02-Sep-09 Kaavian Systems 20
Scenario 1
Get two numbers from the user and COMPUTE
ADD
SUBTRACT
MULTIPLY
DIVIDE
Use decimal values to perform the same operation, control the
precisions accurately.
02-Sep-09 Kaavian Systems 21
Scenario 2
Write - “My first ABAP program”
Using Substrings with offset print - “first ABAP program”
Use SHIFT to print - “program My first ABAP”
TRANSLATE to upper case & print - “MY FIRST ABAP
PROGRAM”
Use OVERLAY to print - “MY-FIRST-ABAP-PROGRAM”
Use REPLACE to print - “My second ABAP program”
Use SEARCH to print - “My FIRST ABAP program”
02-Sep-09 Kaavian Systems 22
Scenario 3
Get two values and the operation (+, -, /, *) to be carried out from the
user, based on the input, use the CASE statement to perform the
action. If the output is greater than 50, print “The <output> is greater
than 50”, If it is between 25 and 50, print “The <output> is between
25 and 50”, If the less than 25 print “The <output> is less then 25”.
Maintain a constant variable with the value (name) “Thompson Jack
H. David”,Get a parameter from the user (name). If the name
entered by the user is the same as the constant, print “The two names
are the same” else if the constant contains a part of the name (ex.
David or Jack), print “A part of the name is same” else if the
constant contains any of the letters as same as the name, print “At
least letters are the same”.
02-Sep-09 Kaavian Systems 23
Scenario 4
Get a number from the user n and print 1 to n. If the number is less
than 50, print from n to 50.
Print the following in the mentioned order.
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
02-Sep-09 Kaavian Systems 24
Thank You