8. Assigning values to variables
An Assignment statement is used to assign value to a variable in an ABAP program. There are 4 different types of assignment statements:-- MOVE
- MOVE-CORRESPONDING
- WRITE TO
- CLEAR
1. MOVE statement
The MOVE statement is used to assign value to a variable.
MOVE 5 TO VAR1.
MOVE ‘a’ TO VAR2.
In above examples, value 5 is assigned to VAR1 and a is assigned to VAR2.
The above statements can also be written as:-
VAR1 = 5. VAR2 = ‘a’. So, "MOVE val TO f1" and "f1 = val" are both functionally equivalent. You can use either of the 2 statements in your program, but stick to 1 form throughout the program.
The MOVE statement is also used to assign the value of one variable to another variable, but the value of original variable remains unchanged. It means MOVE statement can also be used to transfer the contents of one variable to another variable. The syntax for MOVE statement is as follows:-
MOVE <f1> TO <f2>.
In above syntax, the content of <f1> is transferred to <f2>. It is not necessary for <f1> to be a variable; it can be a literal, constant or text symbol.
Following programs demonstrate the use of MOVE statement:-
Program 1:
REPORT ZMOVE_DEMO.
*-------------------------------------------------------*
* data declarations
*-------------------------------------------------------*
DATA: var1 TYPE c,
var2 TYPE i,
var3(3) TYPE p DECIMALS 2,
var4 TYPE i,
var5 TYPE i.
CONSTANTS: c1 TYPE i VALUE 10.
*-------------------------------------------------------*
* using MOVE statement
*-------------------------------------------------------*
var1 = 'B'."functionally equivalent to MOVE statement
MOVE 1 TO var2."using MOVE statement to assign literal 1 to variable var2
MOVE '3.14' TO var3."using MOVE statement to assign literal 3.14 to variable var3
MOVE c1 TO var4."using MOVE statement to assign constant c1 to variable var4
MOVE var2 TO var5."using MOVE statement to assign value of var2 to var5
*--------------------------------------------------------------------*
* displaying data
*--------------------------------------------------------------------*
WRITE: 'var1= ',var1.
WRITE: / 'var2= ',var2.
WRITE: / 'var3= ',var3.
WRITE: / 'var4= ',var4.
WRITE: / 'var5= ',var5.
*--------------------------------------------------------------------*
The output of above program is as follows:-
var1= B var2= 1 var3= 3.14 var4= 10 var5= 1
Program 2:
REPORT zmove_demo1.
*--------------------------------------------------------------------*
* data declarations
*--------------------------------------------------------------------*
DATA: BEGIN OF rec1,
rec1_var1(3) TYPE c VALUE 'ABC',
rec1_var2 TYPE i VALUE 100,
END OF rec1.
DATA: BEGIN OF rec2,
rec2_var1(3) TYPE c,
rec2_var2 TYPE i,
END OF rec2.
*--------------------------------------------------------------------*
* using MOVE statement
*--------------------------------------------------------------------*
*rec2 = rec1.
MOVE rec1 TO rec2."Functionally equivalent to rec2 = rec1
*--------------------------------------------------------------------*
* displaying data
*--------------------------------------------------------------------*
WRITE: rec2-rec2_var1,
/ rec2-rec2_var2.
*--------------------------------------------------------------------*
The output of above program is as follows:-ABC
100
2. MOVE-CORRESPONDING statement
The MOVE-CORRESPONDING statement is used to assign values between the components of 2 structures. The syntax for the MOVE-CORRESPONDING statement is as follows:-MOVE-CORRESPONDING <struct1> TO <struct2>.
The contents of components of <struct1> structure are copied to the components of <struct2> structure, with identical names.
Following programs demonstrate the use of MOVE-CORRESPONDING statement:-
Program 1:
REPORT zmove_corres_demo.
*--------------------------------------------------------------------*
* data declarations
*--------------------------------------------------------------------*
DATA: BEGIN OF struct1,
var1 TYPE i VALUE 50,
var2 TYPE c VALUE 'A',
END OF struct1.
DATA: BEGIN OF struct2,
var1 TYPE i,
var2 TYPE c,
var3,
END OF struct2.
*--------------------------------------------------------------------*
* using MOVE-CORRESPONDING statement
*--------------------------------------------------------------------*
MOVE-CORRESPONDING struct1 TO struct2.
*-------------------------------------------*
* displaying data
*-------------------------------------------*
WRITE: struct2-var1,struct2-var2.
*--------------------------------------------------------------------*
The output of above program is as follows:-50 A
Program 2:
REPORT zmove_corres_demo1.
*--------------------------------------------------------------------*
* data declarations
*--------------------------------------------------------------------*
DATA: BEGIN OF struct1,
var1 TYPE i VALUE 50,
var2 TYPE c VALUE 'A',
END OF struct1.
DATA: BEGIN OF struct2,
var1(4) TYPE p DECIMALS 2,"data type of var1 is p whereas of var1 is i.
var2 TYPE c,
var3,
END OF struct2.
*--------------------------------------------------------------------*
* using MOVE-CORRESPONDING statement
*--------------------------------------------------------------------*
MOVE-CORRESPONDING struct1 TO struct2.
*--------------------------------------------------------------------*
* displaying data
*--------------------------------------------------------------------*
* source data is converted into target format
WRITE: struct2-var1,
struct2-var2.
The output of above program is as follows:-50.00 A
3. WRITE TO statement
WRITE TO statement converts the content of source field to type c and then place the resulting string into the target field. The syntax for WRITE TO statement is as follows:-WRITE <f1> TO <f2>.
In above syntax, <f1> is the source field and <f2> is the target field. Statement converts the content of <f1> into a string and then stores the string in <f2>.
Following program demonstrates the use of WRITE TO statement:-
REPORT zwrite_to_demo.
*--------------------------------------------------------------------*
* data declarations
*--------------------------------------------------------------------*
DATA: source1(4) TYPE c VALUE 'aman',
source2 TYPE i VALUE 50,
target1(4) TYPE c,
target2(2) TYPE c.
*--------------------------------------------------------------------*
* using WRITE TO statement
*--------------------------------------------------------------------*
WRITE source1 TO target1.
WRITE source2 TO target2.
*--------------------------------------------------------------------*
* displaying data
*--------------------------------------------------------------------*
WRITE: 'source name:',source1.
WRITE: / 'target name:',target1.
WRITE: / 'source number:',source2.
WRITE: / 'target number:',target2.
*--------------------------------------------------------------------*
The output of above program is as follows:-
source name: aman target name: aman source number: 50 target number: 50Note:
The data type of <f1> must be convertible into a character field. If it is not convertible, a syntax or runtime error occurs. The contents of <f1> remain unchanged, but the content of <f2> is always regarded as a character string. So, target field must be of type c. otherwise, syntax error occurs.
4. CLEAR statement
The CLEAR statement is used to reset the value of a variable. It means it resets the value of variable to initial value. Initial value of the variable depends on the data type of variable. For example, variable of type C is set to null and variable of type I is set to zero.The syntax for CLEAR statement is as follows:-
CLEAR <F>.
Note:
Initial value does not mean the starting value, which is set using VALUE option of the DATA statement.
IMPACT OF CLEAR STATEMENT ON DIFFERENT DATA TYPES
Data Type
|
Impact of clear statement
|
Elementary Data Type
|
Resets value to
initial value.
|
Reference
|
Resets a reference
variable to its initial value so that it does not point to any object.
|
Structure
|
Resets the individual
components of a structure to their respective initial values.
|
Internal Table
|
Deletes the entire
contents of the internal table.
|
Following program demonstrates the use of CLEAR statement:-
REPORT zclear_demo.
*--------------------------------------------------------------------*
* data declarations
*--------------------------------------------------------------------*
DATA: var1 TYPE i VALUE 5,
var2 TYPE c VALUE 'A',
var3(3) TYPE p DECIMALS 2 VALUE '3.14'.
*--------------------------------------------------------------------*
* displaying data before using CLEAR
*--------------------------------------------------------------------*
WRITE: 'BEFORE CLEARING VARIABLES'.
WRITE: / 'var1:',var1.
WRITE: / 'var2:',var2.
WRITE: / 'var3:',var3.
*--------------------------------------------------------------------*
* using CLEAR statement
*--------------------------------------------------------------------*
CLEAR: var1,
var2,
var3.
*--------------------------------------------------------------------*
* displaying data after CLEAR
*--------------------------------------------------------------------*
WRITE: / 'AFTER CLEARING VARIABLES'.
WRITE: / 'var1:',var1.
WRITE: / 'var2:',var2.
WRITE: / 'var3:',var3.
*--------------------------------------------------------------------*
The output of above program is as follows:-
BEFORE CLEARING VARIABLES var1: 5 var2: A var3: 3.14 AFTER CLEARING VARIABLES var1: 0 var2: var3: 0.00