Processing Data

Objective

After completing this lesson, you will be able to Assign values to data objects.

Assign Values to Data Objects

A variable can be assigned a value or an arithmetic expression using the equal sign

Use value assignments to change the value of variables.

In ABAP, the operator for value assignments is a simple equals sign ( = ). The variable on the left-hand side is filled with the value of the expression on the right-hand side.

The simplest possible expression is a single data object, for example, a literal or another variable. However, ABAP supports more sophisticated expressions like the arithmetic expression in the example above.

Resetting Variables

A variable can be set to its initial state with the CLEAR keyword

The CLEAR statement resets a data object's content to the type-related initial value. For detailed information about the initial values for a particular type, refer to the keyword documentation for the CLEAR statement. In general, if the data type is numeric its initial value will be zero while if the data type is character its initial value is a space.

Arithmetic Calculations

ABAP supports arithmetic operators and built-in numeric functions

Arithmetic expressions are ABAP expressions with a combination of value, operators, and functions the runtime system processes to calculate a result. For arithmetic expressions, the result type depends on the type of the operands used as input to the expression.

You can use an arithmetic expression in any reading operand position, for example, the right-hand side of the value assignment.

The first example is a simple addition. The contents of amount1 and amount2 are added together, and the results are placed in variable total.

The second example is a bit more sophisticated. Before adding them, the contents of amount1 and amount2 are weighed with factors 2 and 3. The result of this addition is then divided by 5 to calculate a weighted average.

The basic arithmetic ABAP provides the operators + for addition, - for subtraction, * for multiplication, and / for division. Additionally, you can use the operator DIV for whole-number division and MOD for the whole-number remainder of a division. Thus, 6 DIV 4 is 1, and 6 MOD 4 is 2.

ABAP has a range of built-in functions for various tasks. Many of these are used for string processing, but here you can see some examples for numeric functions. You use sqrt( ) function to pull the square root and ipow( ) function to raise a number to a whole-numbered power.

Different examples of using arithmetic operators

In complex expressions involving more than one operator, multiplication and division take precedence over addition and subtraction. Expressions with identical precedence are processed left to right.

ABAP syntax requires at least one blank between operators and operands. 1 + 1 is correct. 1+1 leads to a syntax error.

Blanks are also needed after opening brackets and before closing brackets.

Use System Information in a Program

The class CL_ABAP_CONTEXT_INFO offers various methods for determining environmental data for the current application. Some of the methods are listed below.

System Information

MethodDescription
CL_ABAP_CONTEXT_INFO->GET_SYSTEM_DATE( ).Current date
CL_ABAP_CONTEXT_INFO->GET_SYSTEM_TIME( ).Current time
CL_ABAP_CONTEXT_INFO->GET_USER_TECHNICAL_NAME( ).User ID of current user
CL_ABAP_CONTEXT_INFO->GET_USER_LANGUAGE_ABAP_FORMAT( ).User login language

So far, you have declared all variables you work on within your ABAP applications. However, the system contains special system information that you can use in your source code. The ABAP runtime system manages this system information, available to any ABAP application that wants to use it.

System information provides information about the actual system status. The ABAP runtime system populates and changes the values when necessary.

Log in to track your progress & complete quizzes