You are on page 1of 24

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:

02-Sep-09

Concatenate

Split

Shift

Condense

Translate

Convert Text

Overlay

Replace

Search

Kaavian Systems

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)

file_name(50).

VALUE .txt,

CONCATENATE file_path name _


sy-uzeit ext
02-Sep-09

sy-datum _

INTO file_name.

file_name has value

Kaavian Systems

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/MATERIAL.txt,
directory(10)TYPE C,
name(12)

TYPE C,

separator(1)

VALUE /.

SPLIT file_names AT delimiter INTO directory name.


02-Sep-09
Kaavianand
Systems
Now
directory contains tmp
name contains

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
right.

02-Sep-09

on the left appear on the


Kaavian Systems

Shift
Example
DATA v_cost(10) TYPE c VALUE 345.98-.
SHIFT v_cost BY 1 PLACES RIGHT CIRCULAR.
Now v_cost = - 345.98
CONDENSE v_cost NO-GAPS.
Now v_cost = -345.98
SHIFT v_cost RIGHT TRAILING space.
Now v_cost has value

02-Sep-09

-345.98.

Kaavian Systems

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

left-justified

sequence of words, each separated by one blank.


NO-GAPS is specified, all blanks are
removed.

02-Sep-09

Kaavian Systems

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

Translate
Example
DATA: letters(20) TYPE C VALUE
'abcabcabcXabc',
change(15)TYPE C VALUE 'aXbaYBabZacZB'.

TRANSLATE letters USING change.


letters now contains 'XaZXaZXaZXXaZ'.

02-Sep-09

Kaavian Systems

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

10

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

11

Overlay
Example
DATA : fname(50) VALUE
/tmp/mat&.txt.
REPLACE & WITH sy-datum INTO
fname.

Now fname has value


/tmp/mat20060222.txt.

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

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

14

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

15

Conditional Operators

CASE Statement

Syntax

02-Sep-09

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.
Kaavian Systems

16

Conditional Operators

IF, ELSE, ELSEIF, ENDIF

Syntax

02-Sep-09

IF <condition1>.
<statement block>
ELSEIF <condition2>
<statement block>.
ELSEIF <condition3>.
<statement block>
.....
ELSE.
<statement block>
ENDIF.
Kaavian Systems

17

Comparison Operators

IS INITIAL

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

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>].
02-Sep-09
Kaavian Systems

<statement block>

19

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

20

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

02-Sep-09

PROGRAM

MY-FIRST-ABAP-

Kaavian Systems

21

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
least
letters are the same. 22
02-Sep-09as the name, print At
Kaavian
Systems

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.

02-Sep-09

Print the following in the mentioned order.


1
2 3 4
2
4 6 8
3
6 9 12
4
8 12 16

Kaavian Systems

23

Reference
The learning material contained in this PowerPoint
Presentation is prepared with the help of http://help.sap.com/
and sap press book. All rights reserved by SAP AG.
Unless otherwise specifically stated, this learning material is
intended for the sole purpose of class room session, internal
use and for knowledge transfer to the Consultants.
However, for detailed information on this learning material,
http://help.sap.com / sap press book may be referred.
Kaavian is not liable for any legal liability or responsibility for
the accuracy, completeness or usefulness of any information
disclosed in these learning materials.
No portion of the learning materials
shall be modified,
reproduced or transmitted in any form by any means, whether
by electronic, photocopier or otherwise without the written
permission of Kaavian. In no event shall Kaavian be liable for
any damage whatsoever resulting in any action arising in
02-Sep-09
24
Kaavian Systems
connection with the use of learning
material.

You might also like