Processing Data

Objectives

After completing this lesson, you will be able to:

  • Perform arithmetic calculations
  • Apply string processing

Arithmetic Calculations

Arithmetic expressions are ABAP expressions with a combination of values, operators, and functions that 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 a value assignment.

The following video illustrates the basics of arithmetic expressions.

Try It Out: Arithmetic Calculations

  1. Like in the first exercise of this course, create a new global class that implements interface IF_OO_ADT_CLASSRUN.
  2. Copy the following code snippet to the implementation part of method if_oo_adt_classrun~main( ):
    Code snippet
    
    * Declarations
    **********************************************************************
    
        " comment/uncomment these line for different result types
        TYPES t_result TYPE p LENGTH 8 DECIMALS 2.
    *    TYPES t_result TYPE p LENGTH 8 DECIMALS 0.
    *    TYPES t_result TYPE i.
    
        DATA result TYPE t_result.
    
    * Calculations
    **********************************************************************
        " comment/uncomment these lines for different calculations
    
        result = 2 + 3.
    *    result = 2 - 3.
    *    result = 2 * 3.
    *    result = 2 / 3.
    *
    *    result = sqrt( 2 ).
    *    result = ipow( base = 2 exp = 3 ).
    *
    *    result = ( 8 * 7 - 6 ) / ( 5 + 4 ).
    *   result = 8 * 7 - 6 / 5 + 4.
    
    * Output
    **********************************************************************
    
        out->write( result ).
    
    Expand
  3. Press CTRL + F3 to activate the class and F9 to execute it as a console app.
  4. Play around with the source code to get familiar with the concepts.
    • Uncomment different calculations and type definitions.
    • Implement your own calculations.

String Processing

String templates are ABAP expressions of result type string. You can use string templates in any reading operand position, for example, the right-hand side of a value assignment.

A string template begins and ends with a pipe-symbol ( | ). The simplest possible string template contains nothing but literal text. In this form a string template is not really different from a string literal.

What distinguishes a string template from a string literal is the ability to embed expressions. An embedded expression is an ABAP expression surrounded by a pair of curly brackets ( { and } ). At runtime, ABAP evaluates the embedded expression and translates the result into a string. In the final result, this string replaces the embedded expression (together with the surrounding curly brackets).

Note
ABAP syntax requires at least one blank after the opening bracket and at least one blank before the closing bracket.

Of course one string template can contain more than one embedded expression.

Inside the curly brackets you can place any kind of ABAP expression: arithmetic expressions, like in the example above, but single variables or even literals can serve as embedded expressions.

One important use case for string templates is the controlled formatting of data for output.

In the first example, variable the_date is of type d and contains a date in the internal (raw) format YYYYMMDD (where YYYY stands for the year, MM for the two-digit month and DD for the two-digit date). When you use variable the_date as embedded expression in a string template the result will be the same as the internal format. But when you add format option DATE = <date_format> within the curly brackets, the system will format the value as a date. If you add DATE = ISO, the output will be in ISO format. With DATE = USER the output format depends on the user settings of the current user.

The second example illustrates some of the options you can use for formatting numbers. Using NUMBER you control the general formatting of numbers, for example, whether a decimal point is used or a decimal comma. Using SIGN you control the position of the sign and whether a plus sign (+) is displayed or not. Using STYLE you can choose from several pre-defined styles, like a scientific style or an engineering style.

You can join fields together using the concatenation operator &&. You can join any combination of data objects and string expressions.

The constituent parts of the expression are joined with no space or other separator between them. If you need spaces or another separator character, you must remember to insert it yourself as part of the expression as shown in the second example.

Try It Out: String Processing

  1. Like in the first exercise of this course, create a new global class that implements interface IF_OO_ADT_CLASSRUN.
  2. Copy the following code snippet to the implementation part of method if_oo_adt_classrun~main( ):
    Code snippet
    
     METHOD if_oo_adt_classrun~main.
    
    * Declarations
    **********************************************************************
        TYPES t_amount TYPE  p LENGTH 8 DECIMALS 2.
    
        DATA amount   TYPE t_amount VALUE '3.30'.
        DATA amount1  TYPE t_amount VALUE '1.20'.
        DATA amount2  TYPE t_amount VALUE '2.10'.
    
        DATA the_date  TYPE d                     VALUE '19891109'.
        DATA my_number TYPE p LENGTH 3 DECIMALS 2 VALUE '-273.15'.
    
        DATA part1 TYPE string VALUE `Hello`.
        DATA part2 TYPE string VALUE `World`.
    
    * String Templates
    **********************************************************************
    
        " comment/uncomment the following lines for different examples
        DATA(text) = |Hello World|.
    *    DATA(text) = |Total: { amount } EUR|.
    *    DATA(text) = |Total: { amount1 + amount2 } EUR|.
    
    * Format Options
    **********************************************************************
    
        "Date
    *    DATA(text) = |Raw Date: { the_date             }|.
    *    DATA(text) = |ISO Date: { the_date Date = ISO  }|.
    *    DATA(text) = |USER Date:{ the_date Date = USER }|.
    
        "Number
    *    DATA(text) = |Raw Number { my_number                    }|.
    *    DATA(text) = |User Format{ my_number NUMBER = USER      }|.
    *    DATA(text) = |Sign Right { my_number SIGN = RIGHT       }|.
    *    DATA(text) = |Scientific { my_number STYLE = SCIENTIFIC }|.
    
    * String expression (concatenation Operator)
    **********************************************************************
    
    *    DATA(text) = part1 && part2.
    *    DATA(text) = part1 && | | && part2.
    *    DATA(text) = |{ amount1 } + { amount2 }| &&
    *                 | = | &&
    *                 |{ amount1 + amount2 }|.
    
    
    * Output
    **********************************************************************
    
        out->write( text ).
    
    Expand
  3. Press CTRL + F3 to activate the class and F9 to execute it as a console app.
  4. Play around with the source code to get familiar with the concepts.
    • Uncomment different value assignments for inline-declared variable text to see different result.
    • Try your own assignements.

Declare Variables and Process Data

In this exercise, you create a program that does a calculation based on two numbers, then formats and outputs the result.

Template:

  • none

Solution:

  • /LRN/CL_S4D400_BTS_COMPUTE (global Class)

Task 1: Variable Declaration

In your own package, create a new global class and define numeric variables for the input.

Steps

  1. Create a new global class that implements the interface IF_OO_ADT_CLASSRUN (suggested name: ZCL_##_COMPUTE, where ## stands for your group number).

    1. Choose FileNewABAP Class.

    2. Enter the name of your package in the Package field. In the Name field, enter the name ZCL_##_COMPUTE, where ## stands for your group number.

    3. Enter a description.

    4. In the Interfaces group box, choose Add.

    5. Enter IF_OO_ADT_CLASSRUN. When the interface appears in the hit list, double-click it to add it to the class definition.

    6. Choose Next.

    7. Select your transport request and choose Finish.

  2. In the IF_OO_ADT_CLASSRUN~MAIN method, declare two variables for integer values (suggested names: number1 and number2).

    1. Add the following code:

      Code snippet
      
          DATA number1 TYPE i.
          DATA number2 TYPE i.
      
      Expand
  3. Implement a value assignment for each of the two numeric variables. Assign a negative value to the first variable and a positive value to the second variable.

    1. Add the following code:

      Code snippet
      
          number1 = -8.
          number2 =  3. 
      
      Expand

Task 2: Data Processing

Calculate a ratio and write the result to the console.

Steps

  1. Implement an arithmetic expression that calculates the ratio of the two numbers. Store the result in a variable that you declare using an inline declaration (suggested name: result).

    1. Add the following code:

      Code snippet
      
          DATA(result) = number1 / number2.
      
      Expand
  2. Implement a string template that puts together a text like this:"6 / 3 = 2", with the actual values of the variables instead of 6, 3, and 2. Store the result in a variable that you declare using an inline declaration (suggested name: output).

    1. Add the following code:

      Code snippet
      
          DATA(output) = |{ number1 } / { number2 } = { result }|.
      Expand
  3. Call method out->write( … ) to write the text to the console.

    1. Proceed as you have done in the previous exercise.

Task 3: Activate and Test

Activate and test the console app.

Steps

  1. Activate the class.

    1. Press Ctrl + F3 to activate the class.

  2. Execute the console app.

    1. Press F9 to execute the console app.

  3. Test the app with different input values.

    1. Edit the value assignments, then activate and execute as before.

Task 4: Control the Result Precision

Make sure the result is rounded to a value with 2 decimal places, and not to an integer value.

Steps

  1. Instead of an inline declaration, use an explicit declaration of variable result. Use a numeric type that allows to specify the number of decimal places.

    1. At the beginning of method if_oo_adt_classrun~main, add the following code:

      Code snippet
      
          DATA result TYPE p LENGTH 8 DECIMALS 2.
      
      Expand
    2. In the code line with the inline declaration, replace DATA(result) with result.

  2. Activate the class and test again.

    1. Proceed as you have done before.

    2. Compare your code to the following extract from the model solution:

      Code snippet
      
       METHOD if_oo_adt_classrun~main.
      
      * Declarations
      **************************
      
          DATA number1 TYPE i.
          DATA number2 TYPE i.
      
          DATA result TYPE p LENGTH 8 DECIMALS 2.
      
          
      * Input Values
      **************************
      
          number1 = -8.
          number2 =  3.
      
      * Calculation
      **************************
      
      *    DATA(result) = number1 / number2.   "with inline declaration 
       
         result = number1 / number2.
      
          DATA(output) = |{ number1 } / { number2 } = { result }|.
      
      * Output
      **************************
      
          out->write( output ).
      
        ENDMETHOD.
      
      Expand

Log in to track your progress & complete quizzes