0% found this document useful (0 votes)
7 views32 pages

String and Loop Operations in ABAP

ABAP_basic_string_modu_techniqu

Uploaded by

Anand Reddy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views32 pages

String and Loop Operations in ABAP

ABAP_basic_string_modu_techniqu

Uploaded by

Anand Reddy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

String Operations:

1. SHIFT
2. TRANSLATE
3. REPLACE
4. STRLEN
5. OFFSET FUNCTIONALITY
6. SPLIT
7. CONCATINATE
8. CONDENSE

SHIFT keyword:
It shifts a specific string to left (or) right side by deleting required options in a
particular string

Syntax-1:
SHIFT <string> LEFT DELETING LEADING <option>
Syntax-2:
SHIFT <string> RIGHT DELETING TRAILING <option>

Example: ‘000001000’----------- 1000


DATA: str(10) TYPE C VALUE ‘000001000’.
SHIFT str LEFT DELETING LEADING ‘0’ .
WRITE:/ str.
Output: 1000
Note:
Size of the string variable must be equal to given string

TRANSLATE keyword:
It converts the case of a particular string from lower to upper and vice-versa

Syntax-1:
TRANSLATE str TO UPPER CASE
Syntax-2:
TRANSLATE str TO LOWER CASE

Example: SATYA--------satya
DATA: str(10) TYPE C VALUE ‘SATYA’.
TRANSLATE str TO LOWER CASE.
WRITE:/ str
Output:
satya

REPLACE keyword:
It replaces a string with another string based on requirement

Syntax:
REPLACE <string1> WITH <string2> INTO <final string>

Example: rupees-------dollars
DATA: str(30) TYPE C VALUE ‘one thousand rupees’.
REPLACE ‘rupees’ WITH ‘dollars’ INTO str.
WRITE:/ str.
Output:
one thousand dollars

STRLEN keyword:
It returns length of a string

Syntax:
n = STRLEN ( str ).
Where n is Integer Variable

Example:
DATA: str(10) TYPE C VALUE ‘satya’,
n TYPE I.
n = STRLEN ( str ).
WRITE:/ n.
Output:
5

OFFSET FUNCTIONALITY keyword:


0 1 2 3 4 5 6 7 --------offset
A B C D E F G H

1. st = str + 2 ( 1 ). --------output------ C
2. st = str + 4 ( 2 ). --------output------ EF

SPLIT keyword:
It splits a single string based on special charectors
Syntax:
SPLIT <string> AT <special charector> INTO <stirng1> <string2>
<string3>…..

Example:
DATA: str(10) TYPE C VALUE ‘cool-drink’,
str1(10) TYPE C,
str2(10) TYPE C.
SPLIT str AT ‘-’ INTO str1 str2.
WRITE:/ str1,
str2.
Output:
cool
drink

CONCATENATE keyword:
It concatenates two (or) more than two strings into a final string

Syntax:
CONCATENATE <str1> <str2> <str3> INTO <final string>
SAPARATED BY <special character>.

Example-1:
DATA: data(10) TYPE C.
CONCATENATE SY-DATUM + 6(2) SY-DATUM + 4(2) SY-DATUM +
0(4) INTO date SAPARATED BY ‘.’.
WRITE:/ date.
Output:
20.09.2014

Example-2:
DATA: year(4),
mon(2),
day(2),
date(10).
year = SY-DATUM + 0(4).
mon = SY-DATUM + 4(2).
day = SY-DATUM + 6(2).
CONCATENATE day mon year INTO date SAPARATED BY ‘.’.
WRITE:/ date.
Output:
20.09.2014
CONDENSE keyword:
It combines a specific string by removing gaps between them

Syntax:
CONDENSE <string> NO-GAPS.

Example:
DATA: str(20) TYPE C VALUE ‘satya narayana’.
CONDENSE str NO-GAPS.
WRITE:/ str.
Output:
satyanarayana

Assignment:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Looping Statements:
Branching:
a. Conditional Statements
b. Unconditional Statements

Conditional Statements:
1. IF…………ENDIF.

Sytax-1:
IF <condition>.
Do this
ENDIF.

Sytax-2:
IF <condition>.
Do this
ELSE.
Do this
ENDIF.

Sytax-3:
IF <condition>.
Do this
ELSE IF <condition>
Do this
ELSE.
Do this
ENDIF.

Note:
Use ELSE IF condition only a single condition is true

2. CASE…………..ENDCASE

Syntax:
CASE <expression>.
WHEN <exp1>.
Do this
WHEN <exp2>.
Do this
……….
……….
WHEN OTHERS.
Do this
ENDCASE.

3. WHILE statement:
Syntax:
WHILE <condition>
----------
----------
ENDWHILE.

Unconditional Statements:
1. DO…………….ENDDO.
Syntax-1:
DO.
---------
---------
ENDDO.

Syntax-2:
DO <n> TIMES.
---------
---------
ENDDO.

Syntax-3:
DO <n> TIMES VARYING <expression> FROM <m> NEXT <n>.
---------
---------
ENDDO.

Operators:
a. Logical Operators
b. Relational Operators

Logical Operators:
1. AND
2. OR
3. NOT

Relational Operators:
1. GT----- >
2. LT ----- <
3. GE ---- >=
4. LE ---- <=
5. NE ------ <>
6. EQ ------ =
CONTINUE keyword:
Whenever CONTINUE is executed it terminates the current LOOP pass & returns
the control to next LOOP pass

Example:
DATA rem TYPE I.
DO 20 TIMES.
rem = SY-INDEX MOD 2.
IF rem NE 0.
CONTINUE.
ENDIF.
WRITE:/ SY-INDEX.--------- current LOOP pass number
ENDDO.

Output:
2 4 6 8 …………………… 20

CHECK keyword:
If check expression is true it will allows the remaining LOOP pass else it will
terminates the current loop pass

Syntax:
CHECK <expression>.

Example:
DATA rem TYPE I.
DO 20 TIMES.
rem = SY-INDEX MOD 2.
CHECK rem = 2.
WRITE:/ SY-INDEX.--------- current LOOP pass number
ENDDO.

EXIT keyword:
When the keyword EXIT is executed the whole loop will be terminated

Example:
DATA: n TYPE I VALUE 10.
DO n TIMES.
IF SY-INDEX >= n.
EXIT.
ENDIF.
WRITE:/ SY-INDEX.
ENDDO.
Mathematical Operations:
Ex: 5.55
1. FRAC -------- 0.55
2. CEIL --------- 6.0
3. FLOOR ----- 5.0
4. SIGN -------- 1 (+)
5. ABS --------- 5.55 (always returns only positive value)
6. TRUNC ---- 5.0
Ex: 6 / 2
7. REM -------- 0
8. DIV --------- 3
9. MOD ------- 0

FRAC:
It returns fraction part from a decimal value

CEIL:
It returns highest value in existing decimal value

FLOOR:
It returns lowest value from a particular decimal value

SIGN:
It returns the signature (positive (or) negative) from a particular numeric value

ABS:
It returns absolute value from a numeric value

TRUNC:
It truncates (removes) decimal part from a numeric value

REM (or) MOD:


These returns remainder

DIV:
It returns devisor
Example:
DATA: n TYPE P DECIMALS 2 VALUE ‘5.55’,
m TYPE P DECIMALS 2.
m = FRAC(n). WRITE:/ m
m = TRUNC(n). WRITE:/ m
m = CEIL(n). WRITE:/ m
m = FLOOR(n). WRITE:/ m
m = ABS(n). WRITE:/ m
m = SIGN(n). WRITE:/ m

Question: What is the difference between SY-TABIX & SY-INDEX?


Answer:
SY-TABIX returns current loop pass for Internal Tables
i.e. LOOP………..ENDLOOP
SY-INDEX returns current loop pass other than LOOP……….ENDLOOP
i.e. DO…………..ENDDO.
WHILE……………ENDWHILE.

Assignments:
1. Formats
a. 1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

b. *
* *
* * *
* * * *
* * * * *

c. *
* * *
* * * * *
* * * * * * *

2. Write a program to print prime numbers between 1 to 100?


3. Write a program to accept two numbers and returns HCF & LCM of that
numbers?
4. Accept a string and check whether it is palindrome (or) not?
SAP and ABAP Memory:
ABAP Memory:
• Transferring data between two programs is done using ABAP memory
• Transferring data from ABAP report to other Standard Application is done
by using SAP memory

Export Import Functionality:


ZExport ZImport
S_matnr to S_matnr to
S_werks to S_werks to
S_budat to SUBMIT ZExport
SELECT……………. WITH matnr IN S_matnr
……………………. WITH werks IN S_werks
Exporting list to memory and return
EXPORT it TO MEMORY IMPORT it FROM MEMORY ID
‘material’.
ID ‘material’.

Question: Explain why you written Export Import Functionality in your real time?
Answer:
➢ The above functionality is for reusability
➢ Sometimes we needs to write a logic which already provided by a program
➢ Instead of writing the same program from beginning you can use the above
functionality for getting the data from other programs

➢ I written a program for calculating Ageing for Materials


➢ In that I need to get Closing Stack of a material which is already provided by
a standard report MB5B
➢ I simply passed input parameters from my Z-program to MB5B using
SUBMIT keyword
➢ In MB5B I written logic for Exporting data to my Z-program & I Imported
data using IMPORT keyword to my Z-program

Example:
➢ Go to SE38
➢ Program: YIMPORT_PROG
➢ Create
➢ Title: Import Program
➢ Type: Executable Program
➢ Save, Local Object
TABLES: EKPO.

SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE text-


000.
SELECT-OPTIONS: s_matnr FOR EKPO-matnr,
s_werks FOR EKPO-werks.
SELECTION-SCREEN: END OF BLOCK b1.

TYPES: BEGIN OF ty_ekpo,


ebeln TYPE ebeln,
matnr TYPE matnr,
menge TYPE menge_D,
netpr TYPE netpr,
END OF ty_ekpo.

DATA: wa_ekpo TYPE ty_ekpo,


It_ekpo TYPE TABLE OF ty_ekpo.

SUBMIT YEXPORT_PROG WITH matnr IN s_matnr


WITH werks IN s_werks
EXPORTING LIST TO MEMORY AND RETURN.

IMPORT it_ekpo FROM MEMORY ID ‘matt’.

LOOP AT it_ekpo INTO wa_ekpo.


WRITE:/10 wa_ekpo-ebeln,
30 wa_ekpo-matnr,
60 wa_ekpo-menge,
90 wa_ekpo-netpr.
ENDLOOP.

➢ Double click on YEXPORT_PROG


➢ Yes
➢ Remove With Top INCL check box
➢ Enter, Enter, Enter

TABLES: EKPO.
SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE text-
000.
SELECT-OPTIONS: s_matnr FOR EKPO-matnr,
s_werks FOR EKPO-werks.
SELECTION-SCREEN: END OF BLOCK b1.

TYPES: BEGIN OF ty_ekpo,


ebeln TYPE ebeln,
matnr TYPE matnr,
menge TYPE menge_D,
netpr TYPE netpr,
END OF ty_ekpo.

DATA it_ekpo TYPE TABLE OF ty_ekpo.

SELECT ebeln matnr menge netpr FROM EKPO INTO TABLE


it_ekpo
WHERE matnr IN s_matnr
AND werks IN s_werks.

EXPORT it_ekpo TO MEMORY ID ‘matt’.

Output:
Execute IMPORT_PROG
s_matnr 100-100 to 100-500
s_werks 1000 to 1100
Modularization Techniques:
There are 5 modularization techniques in SAP.
They are
1. Subroutines
2. Function Modules
3. Include
4. Macro
5. Field Symbol

• In real time you should ensure that every program should be highly readable
& reusable
• For achieving readability & reusability we have to work with modularization
techniques

Subroutines:
• Subroutine is like a mini program which can be called within the same
program (or) from other programs
• Using Subroutines you can perform Calculations, Call other function
modules, Write statements……….etc

Types of Subroutines:
1. Internal Subroutines
2. External Subroutines

Internal Subroutines:
In this both calling part & definition part are in same program

Syntax:
PERFORM <subroutine name> USING <param1> <param2>
………….

FORM <subroutine name> USING <param1> <param2> ………….


-----------------
-----------------
ENDFORM.

Note:
1. In above syntax PERFORM will call its FORM & simultaneously pass
Parameters to its FORM
2. Subroutine name can be any name. The same name should be used both in
PERFORM & FORM
3. The Parameters defined in PERFORM are called Actual Parameters. These
parameters have global visibility & must be defined in a program
4. The parameters maintained in FORM are called Formal Parameters. These
parameters have local visibility & are automatically created based on Actual
Parameters
5. Actual Parameters will pass values to Formal Parameters by using USING
option
6. The number of Actual Parameters should be same that of Formal Parameters

Example:
WRITE:/ ‘Welcome to Subroutine’.
PERFORM sub.
WRITE:/ ‘First Call’.
PERFORM sub.
WRITE:/ ‘Second Call’.
FORM sub.
WRITE:/ ‘Inside Sub’.
ENDFORM.

Output:
Welcome to Subroutine
Inside Sub
First Call
Inside Sub
Second Call
Debugging:
Break Point:
Using break point the execution of a program can be terminated & until the break
point is reached

Note:
In real time for large programs debugging each & every part of a program is time
consuming. If you know where the problem arises you can directly keep a break
point & test the program

Types of Break Points:


1. Dynamic Break Point
2. Static Break Point

Dynamic Break Point:


• Dynamic break point is kept automatically & can be removed during runtime
• It is automatically discarded whenever system is logged off

Note:
Dynamic break point is used if you are not aware of the exact problem in your
program. During debugging you can set dynamic break point at different, different
places & you can remove the same dynamically

Static Break Point:


It is kept by using a keyword BREAK-POINT (or) BREAK <user name>

Note:
Static break point is used if you know the exact problem in your program & you
want to debug the same code N number of times
Pass by Value & Pass by Reference:
Pass by Value:
• In pass by value, value (or) values is passed from Actual Parameters to
Formal Parameters
• Both calling part & definition part will share different memory locations
• In call by value nothing is reflected back to calling part

Example:
DATA v1 TYPE C VALUE ‘a’.
PERFORM sub USING v1.
WRITE:/ v1.
FORM sub USING VALUE(p1).
p1 = ‘b’.
WRITE:/ p1.
ENDFORM.

Output:
b
a

Pass by Reference:
• In pass by reference value is not passed to Formal Parameters instead a
Pointer (or) Reference (or) Address is passed to Formal Parameters
• Any changes done in Formal Parameters will be effected to Actual
Parameters also

Example:
DATA v1 TYPE C VALUE ‘a’.
PERFORM sub USING (or) CHANGING v1.
WRITE:/ v1.
FORM sub USING (or) CHANGING p1.
p1 = ‘b’.
WRITE:/ p1.
ENDFORM.

Output:
b
b
External Subroutines:
In external subroutines both PERFORM & FORM maintained in separate
programs

Example:
➢ Go to SE38
➢ Program: ZMM_GRM_DETAILS
➢ Create
➢ Title: GRM Details
➢ Type: Executable Program
➢ Save
➢ Local Object

TABLES: MKPF, MSEG.

SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE


text-000.
SELECT-OPTIONS: s_mblnr FOR MKPF-mblnr,
s_werks FOR MSEG-werks,
s_budat FOR MKPF-budat.
SELECTION-SCREEN: END OF BLOCK b1.

PERFORM extsub (ZMM_GRM_FORM) USING s_mblnr-low


s_mblnr-high s_werks-low s_werks-high s_budat-low s_budat-high.

➢ Double click on ZMM_GRM_FORM


➢ You will find a pop-up select Yes
➢ Save & Yes
➢ You will find a pop-up deselect With Top Incl check box the press Enter

TYPES: BEGIN OF ty_mseg,


mblnr TYPE mblnr,
budat TYPE budat,
menge TYPE menge_D,
matnr TYPE matnr,
dmbtr TYPE dmbtr,
END OF ty_mseg.

DATA: wa_mseg TYPE ty_mseg,


It_mseg TYPE TABLE OF ty_mseg.
FORM extsub USING VALUE(grm_low) VALUE(grm_high)
VALUE(plnt_low) VALUE(plnt_high) VALUE(date_low)
VALUE(date_high)
SELECT MKPF~mblnr MKPF~budat MSEG~menge MSEG~matnr
MSEG~dmbtr INTO TABLE it_mseg FROM MKPF
INNER JOIN MSEG
ON MKPF~mblnr = MSEG~mblnr
WHERE MKPF~mblnr BETWEEN grm_low AND grm_high
AND MSEG~werks BETWEEN plnt_low AND plnt_high
AND MKPF~budat BETWEEN date_low AND date_high
AND MSEG~bwart EQ ‘101’.

LOOP AT it_mseg INTO wa_mseg.


WRITE:/10 wa_mseg-mblnr,
30 wa_mseg-budat,
50 wa_mseg-menge,
70 wa_mseg-matnr,
90 wa_mseg-dmbtr.
ENDLOOP.
ENDFORM.

Output:
s_mblnr : 49000000 to 490005000
s_werks: 1000 to 1200
s_budat: 15.11.1994 to 01.06.2009
Execute (F8)
MKPF Table:
It holds Material Document Header Data

Fields:
1. Mblnr ---------- Material Document Number
2. Mjahr ---------- Material Document Year
3. Blart ---------- Material Document Type
4. Budat ---------- Material Document Date
5. Xblrn ---------- Reference Document (Purchase Order Document) Number

MSEG Table:
It holds Material Document Item Data

Fields:
1. Mblnr ------ Material Document Number
2. Mjahr ------ Material Document Year
3. Bwart ------ Moment Type
4. Werks ------ Plant
5. Lgort ------- Storage Location
6. Charg ------ Batch Number
7. Lifnr ------- Vendor Account Number

Link:
LFA1-lifnr
MSEG-lifnr

8. Shkzg ------- Debit / Credit Indicator


9. Dmbtr ------ Amount in Local Currency
[Link] ------ Quantity
[Link] ------- Unit of Measurement
[Link] ------- Purchase Order Number

Link:
EKPO-ebeln
MSEG-ebeln

[Link] -------- Item Number


Link:
EKPO-ebelp
MSEG-ebelp

[Link] ------- Number of Material Document Number


[Link] -------- Department
[Link] ------- Production Order Number
Function Module:
• Function Module is a piece of code which performs a specific task based on
given requirement
• Function Module is a responsible program which always accepts Parameters
& returns a Value

Types of Function Modules:


1. Normal Function Module
2. Remote Enabled Function Module
3. ALV Function Module (ABAP List Viewer)

Normal Function Module:


It is a piece of code which performs a specific task based on given Parameters

Remote Enabled Function Module:


• It is for distributed environment.
• You can call this function module within the system & across the system

ALV Function Module:


• These are part of Normal Function Module
• Has they improves performance of a programming so they are separated
from Normal Function Modules

Function Group:
• It is a collection of identical objects
• Without Function Group you cannot create a Function Module
• In one Function Group we can maintain 99 Function Modules
• Apart from this SAP providing more than 120000 Function Modules which
are surrounding more than 10000 Function Modules

Syntax:
CALL FUNCTION ‘<FUNCTION MODULE NAME>’. ------ calling part
(SE38)

FUNCTION <FUNCTION MODULE NAME>.


-------------
------------- ------- definition part (SE37)
ENDFUNCTION.
Function Module Interface:
These are Parameters which are pass & return from a Function Module

Types of Function Module Interfaces:


1. Exporting
2. Importing
3. Tables
4. Changing

Exporting:
These are Variables (or) Field Groups which are pass to a Function Module in
order to perform a task

Importing:
Function Modules always returns a value via Importing Parameters

Tables:
These are Internal Tables

Changing:
In latest versions Table Parameters are replaced by Changing

Note:
1. Exporting & Importing Parameters works based on Pass by Value
2. Tables & Changing Parameters works based on Pass by Reference

Steps for Create Function Module:


1. Work with SE80 (create Function Group)
2. Work with SE37 (create Function Module based on Function Group)
3. Work with SE38 (call Function Module)

Step-1:
➢ Go to SE80
➢ Under Test Repository select Function Group
➢ Provide Function Group name: ZCAL_GR
➢ Enter, Yes
➢ Provide Short Text: Function Group for Calculations
➢ Select Save, Local Object
Step-2:
➢ Go to SE37
➢ Function Module: ZCALC_MOD
➢ Create
➢ Function Group: ZCAL_GR
➢ Short Text: Function Module for Calculations
➢ Enter, Enter
➢ Import tab
Parameter Name Typing Associated Type
X TYPE I
Y TYPE I
➢ Export tab
Parameter Name Typing Associated Type
Z TYPE I
➢ Exception tab
Exception Short Text
No_Data Wrong Calculation

➢ Source Code tab


z = x + y.
IF SY-SUBRC NE 0.
RAISE No_Data.
ENDIF.

➢ Activate Function Module (F8)

Step-3:
➢ Go to SE38
➢ Program: ZCALC_CALL_SUM
➢ Create
➢ Title: Function Module for Calculations
➢ Type: Executable Program
➢ Save, Local Object

PARAMETERS: a TYPE I,
b TYPE I.

DATA c TYPE I.

* calling function module


CALL FUNCTION ‘ZCALC_MOD’.
EXPORTING.
x = a.
y = b.
(or)
IMPORTING.
z = c.
➢ Select Pattern option
➢ Call Function: ZCALC_MOD

WRITE:/ ‘The Addition is:’, c.

Working with Standard Function Module:


➢ Go to SE37
➢ Function Module: HR_RU_AGE_YEARS (standard function module name)
➢ Select F8
PERNR: 1000 --------- employee number
BSDTE: 18.9.2014 -------- To days date
➢ Select F8

Assignment:
Accept a number as input say 3900 & the Function Module should return the same
number in figures say Three Thousand and Nine Hundred
Include Program:
Include Program’s memory is available to any ABAP work bench tool
Example:
INCLUDE ZNC1
WRITE:/ ‘work’.

INCLUDE ZNC2
INCLUDE ZNC1.
WRITE:/ ‘hard’.

REPORT ZNC3
INCLUDE ZNC2.

Note:
Include Program is a program (or) reusable program without any Parameters
Field Symbol:
• Field Symbol is analog to pointer concept in ‘C’ language
• It holds Reference of other Variables & returns Value stored in the
Reference

Note:
Using Field Symbol you can improve performance of a program
Example:

➢ Go to SE38
➢ Program: ZMM_FIELDSYMBOL
➢ Create
➢ Title: Field Symbols
➢ Type: Executable Program
➢ Save, Local Object

TABLES: LFA1.
INCLUDE ZVEND_DECC.

➢ Double click on ZVEND_DECC


➢ Yes, Yes, Enter

SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAM TITLE


text-000.
SELECT-OPTIONS: s_lifnr FOR LFA1-lifnr,
s_land1 FOR LFA1-land1.
SELECTION-SCREEN: END OF BLOCK b1.

TYPES: BEGIN OF ty_lfa1,


lifnr TYPE lifnr,
land1 TYPE land1,
name1 TYPE name1,
ort01 TYPE ort01,
pstlz TYPE pstlz,
stras TYPE stras,
END OF ty_lfa1.

FIELD-SYMBLOS <l_fs> TYPE ty_lfa1.

DATA it_lfa1 TYPE TABLE OF ty_lfa1.


PERFORM get_data.
PERFORM display_data.
FORM get_data.
SELECT lifnr land1 name1 ort01 pstlz stras INTO TABLE
it_lfa1
FROM LFA1
WHERE lifnr IN s_lifnr
AND land1 IN s_land1.
ENDFORM.
FORM display_data.
LOOP AT it_lfa1 ASSIGNING <l_fs>.
WRITE:/10 <l_fs>-lifnr,
30 <l_fs>-land1,
50 <l_fs>-name1,
70 <l_fs>-ort01,
90 <l_fs>-pstlz,
110 <l_fs>-stras,
ENDLOOP.
ENDFORM.

Note:
Nowadays most of the companies are using Field Symbols instead of Work area

You might also like