Author: Rudramani Pandey

  • ABAP Excel Upload

    Preface – This post is part of the ABAP Programs series.

    To perform ABAP excel upload using ABAP report, we need to call a FM ‘TEXT_CONVERT_XLS_TO_SAP’. This FM converts the excel data to ABAP data. Now we can store the same in our internal table and later in our database table. The only mandatory requirement is that the table fields should match the header of excel in a synchronous way, else wrong data will be saved.

    Introduction

    Many times we need to upload an excel data from our local storage on PC and insert those data into our ABAP tables. This can be easily achieved using ABAP Function Module ‘TEXT_CONVERT_XLS_TO_SAP’. In the given program we convert our excel data into ABAP data and insert those data in our ztable.

    ABAP Excel Upload
    ABAP Excel Upload – Image Illustration of process

    ABAP Program

    TYPE-POOLS truxs.
    PARAMETERS p_file TYPE rlgrap-filename.
    TYPES : BEGIN OF t_tab,
              filename TYPE char100sm,
            END OF t_tab.
    DATA : t_upload  TYPE STANDARD TABLE OF t_tab, 
           wa_upload TYPE t_tab,
           it_type   TYPE truxs_t_text_data.
    
    AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
      CALL FUNCTION 'F4_FILENAME'
        EXPORTING
          field_name = 'p_file'
        IMPORTING
          file_name  = p_file.
    
    START-OF-SELECTION.
      CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
        EXPORTING
          i_tab_raw_data       = it_type
          i_filename           = p_file
        TABLES
          i_tab_converted_data = t_upload[]
        EXCEPTIONS
          conversion_failed    = 1
          OTHERS               = 2.
      IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                WITH sy-msgv1 sy-msgv2 .
      ENDIF.
    
    END-OF-SELECTION.
      DATA : ls_cc_impl TYPE zBarry_upload,
             lt_cc_impl TYPE STANDARD TABLE OF  zBarry_upload.
      LOOP AT t_upload INTO wa_upload.
        ls_cc_impl-mandt      = sy-mandt.
        ls_cc_impl-filename   = wa_upload-filename.
        APPEND ls_cc_impl TO lt_cc_impl.
      ENDLOOP.
      MODIFY zBarry_upload FROM TABLE lt_cc_impl.
    

    Table and Excel

    Excel Data for Upload in ABAP Table
    Excel Data for Upload in ABAP Table

     

    Upload Excel Data
    Upload Excel Data

    Explanation

    In the above program, we have implemented the following steps:

    1. Initially, we have defined TYPE-POOLS truxs. TYPE-POOLS have predefined methods that can be reused in multiple programs. Here we are using truxs, that is used later as truxs_t_text_data, a data type.
    2. Now, we have define parameters: p_file. This will be used to take the file location as input.
    3. Later, we have defined variables: t_tab which is a table type, t_upload, wa_upload and it_type.
    4. Now, we have called a function module “F4_FILENAME” to process the file upload.
    5. Then, we have called another function module ‘TEXT_CONVERT_XLS_TO_SAP’ to get the ABAP form of data into an internal table.
    6. Once we have our data in an internal table, then we can easily append it to our data base table using loop.

    Tutorial Video

    You can watch the video below to learn implementation:

    [embedyt] https://www.youtube.com/watch?v=oU4lRrbcoIY[/embedyt]
  • Truncate, Round Up & Round Down Decimals in SAP ABAP

    Preface – This post is part of the ABAP Programs series.

    Many times in SAP ABAP, there is a requirement to Truncate, Round Down and Round Up a Decimal Number. In that case, we may use the predefined ABAP keywords to achieve the same. In this article, we will be discussing them altogether.

    Introduction

    We can truncate (remove decimal values), round down (5.5 to 6) and round up (5.5 to 5) in ABAP. To achieve these functionalities, ABAP has provided keywords like FLOOR, CEIL and TRUNC. These keywords are implemented in given ABAP program:

    Round Down and Round Up Decimal Number in SAP ABAP
    Round Down and Round Up Decimal Number in SAP ABAP – Image Illustration

     

    Truncate, Round Down and Round Up Decimal Number in SAP ABAP
    Truncate, Round Down and Round Up Decimal Number in SAP ABAP – Image Illustration

    ABAP Program

    Data: tp_deci3 type p decimals 3,
    tp_deci2 type p decimals 2.
    tp_deci3 = '123.456'.
    tp_deci2 = ( floor( tp_deci3 * 100 ) ) / 100. "always rounded down
    Write tp_deci2.
    tp_deci2 = ( ceil( tp_deci3 * 100 ) ) / 100. "Always rounded up
    Write tp_deci2.
    tp_deci2 = ( trunc( tp_deci3 * 100 ) ) / 100. "Truncate a number
    Write tp_deci2.
    

    Explanation

    In the program, mentioned above, we have implemented following, step by step:

    1. Initially, we have defined two variables: tp_deci3 and tp_deci2. Both of these variables are of type i.e. packed (like float, it can have decimal values too).  tp_deci3 can have 3 decimal places while  tp_deci2 can have 2 decimal places.
    2. Now, we have simply implemented the ABAP keywords: FLOOR, CEIL and TRUNC; one by one. [How it works has been illustrated via images, above].
    3. Later, we have printed the result as output.
  • Program to find Armstrong Number in ABAP

    Preface – This post is part of the ABAP Programs series.

    Sometimes, there is a need to know if the number provided by user is Armstrong number. In that case we program to find Armstrong Number in ABAP. It is not a question that is merely asked in an interview but an important keyword that is used in day to day programming.

    Introduction

    Armstrong Number is a number whose sum of cube of individual number is equal to the entire number itself.

    For example: 153 = 13 + 53 + 33

    This can be achieved in ABAP with the help of loop in ABAP and truncate in ABAP. The given program implements the same:

    Program to find Armstrong Number
    Armstrong Number – Image Illustration

    ABAP Program to find Armstrong Number

    PARAMETERS: lv_data1(10) type p.
    Data: lv_digit(10) type i,
          lv_final(1) type i,
          lv_arm(10) type i,
          lv_data2(10) type p.
    lv_data2 = lv_data1.
    while lv_data2 <> 0.
        lv_digit = lv_data2 MOD 10.
        lv_arm = lv_arm + ( lv_digit  * lv_digit  * lv_digit ).
        lv_data2 = trunc( lv_data2 / 10 ).
        ENDWHILE.
    
    If lv_data1 EQ lv_arm.
      Write: 'It is an Armstrong Number'.
      Else.
        Write: 'It is not an Armstrong Number'.
        ENDIF.
    

    Explanation

    In the program, mentioned above, we have written a mathematical algorithm. The above program is explained below, step by step:

    1. Initially, we have defined a parameter lv_data1 of type i.e. packed (another form of float) and length 10. This parameter will be used to take the input.
    2. Later, we have defined four variables: lv_digit, lv_final, lv_arm, lv_data2. These variables will be used in the algorithm discussed in further step.
    3. To find if the number is Armstrong number or not, we will have to take out each individual number from the input. Then, get the summation of their cube.
    4. If the summation achieved in above step is equal to the original number, then we print ‘It is an Armstrong Number’ else we print ‘It is not an Armstrong Number’.
  • Program to Check Palindrome in ABAP

    Preface – This post is part of the ABAP Programs series.

    Sometimes, there is a need to check palindrome of a number provided by user. In that case we program to check palindrome in ABAP. It is not a question that is merely asked in an interview but an important keyword that is used in day to day programming.

    Introduction

    Palindrome Characters/String/Number are those characters which are same if reversed. 12321, MADAM are some example of Palindromes. To achieve this thing, we need to reverse that string and compare with the old value. The given program implements the same:

    Program to Check Palindrome
    Program to Check Palindrome- Image Illustration

    ABAP Program

    PARAMETERS: lv_data1(10) type c.
    Data : lv_data2(10) type c.
    CALL FUNCTION 'STRING_REVERSE'
      EXPORTING
        string          =  lv_data1
        lang            = sy-langu
     IMPORTING
       RSTRING         = lv_data2
    * EXCEPTIONS
    *   TOO_SMALL       = 1
    *   OTHERS          = 2
              .
    IF lv_data1 EQ lv_data2.
    Write: 'Palindrome Number'.
    Else.
      Write: 'Not a Palindrome Number'.
    ENDIF.
    

    Explanation

    In the program mentioned above, we have implemented following step by step:

    1. Initially, we have defined a parameter lv_data1 of type i.e. character of length 10. This parameter will be used to take the input.
    2. Later, we have declared a variable lv_data2 of type i.e. character of length 10. This variable will be used to store the reverse value of input string.
    3. Now, we will call a function module ‘STRING_REVERSE’ that will give us the reversed string.
    4. If the original string and reverse string are same, then output will be ‘Palindrome Number’ else it will be ‘Not a Palindrome Number’.
  • Calculate Power of Number in ABAP

    Preface – This post is part of the ABAP Programs series.

    Sometimes, there is a need to know the power of a number provided by user. In that case we program to calculate power of number in ABAP. It is not a question that is merely asked in an interview but an important keyword that is used in day to day programming.

    Introduction

    To calculate power of a number, you need to multiply that number by itself n times. Here n is the power. To achieve this thing we need to use Loops in ABAP. Following program implements the same:

    Calculate Power of Number in ABAP
    Power of Number in ABAP- Illustration Image

    ABAP Program

    PARAMETERS: lv_data1(10) type i,
                lv_data2(10) type i.
    Data lv_co_data = lv_data1 "to preserve the value.
    While  lv_data2 > 1 .
    lv_data1 = lv_data1 * lv_co_data.
    lv_data2 = lv_data2 - 1.
    ENDWHILE.
    Write: lv_data1.
    

    Explanation

    In the program, mentioned above, we have implemented a while loop to get the required result. The same is explained below, step by step:

    1. Initially, we have defined two parameters: lv_data1 and lv_data2. Both of these parameters are of type i.e. integer and length 10. These parameters will be used to take the number (also called base number) and the power ( also called exponent of power).
    2. Then, we will use a while loop which will run from the power value to 1.
    3. We will keep multiplying the number with itself till the loop runs.
    4. In this line, we will keep decreasing the value of power till it is equal to 1.
    5. Now, we have calculated the power of the number. The output will be then printed using ABAP keyword “WRITE”.
  • Program to Reverse A String in ABAP

    Preface – This post is part of the ABAP Programs series.

    Sometimes, there is a need to know the reverse of a string provided by user. In that case we program to reverse a string in ABAP. It is not a question that is merely asked in an interview but an important keyword that is used in day to day programming. This even helps us to find if a string is palindrome or not.

    Introduction

    To reverse a string, we need to call a Function Module STRING_REVERSE in ABAP program. The given program implements the same:

    Program to Reverse A String
    Program to Reverse A String- Image Illustration

    ABAP Program

    PARAMETERS: lv_data1(10) type c.
    Data : lv_data2(10) type c.
    CALL FUNCTION 'STRING_REVERSE'
      EXPORTING
        string          =  lv_data1
        lang            = sy-langu
     IMPORTING
       RSTRING         = lv_data2
    * EXCEPTIONS
    *   TOO_SMALL       = 1
    *   OTHERS          = 2
              .
    IF sy-subrc EQ 0.
    Write: lv_data2.
    ENDIF.

    Explanation

    Well, this program is self explanatory. Still, I will explain it line by line below:

    1. Initially, we have defined a parameter lv_data1 of type that is character of length 10. This parameter will be used to take input from user.
    2. In the very next line, we have defined a variable lv_data2 of type that is character of length 10. This variable will be used to store the reversed string.
    3. Now, we will call a Function Module ‘STRING_REVERSE’ exporting lv_data1 i.e. our string and importing the RSTRING in our variable lv_data2.
    4. That’s it, we have to now just print it as output.
  • String Length in ABAP

    Preface – This post is part of the ABAP Programs series.

    Sometimes, there is a need to know the length of string provided by user. In that case we program to find string length in ABAP. It is not a question that is merely asked in an interview but an important keyword that is used in day to day programming.

    Introduction

    To find length of String in ABAP, you need to use a keyword STRLEN which means string length. The following program implements the same:

    String Length in ABAP
    String Length Image Illustration

    ABAP Program

    PARAMETERS: lv_data1 type string.
    Data : lv_data2(10) type i.
    lv_data2 = strlen( lv_data1 ) . " The proper Syntax is strlen and then 'bracket open without space'
                                    "  then space then Your Number/String then space then close bracket else it will give an error
    Write: lv_data2.
    

    Explanation

    This program is self explanatory, still I am explaining the same step by step below:

    1. Initially, we have defined a parameter lv_data1 of data type string. This parameter will be used to take input from user.
    2. Then, we have defined a variable lv_data2 of data type i i.e. integer and length 10.
    3. In this line, we have used ABAP keyword STRLEN to find the length of the string saved in variable lv_data1. The length that we have calculated is saved in variable lv_data2.
    4. Now, here we have just printed the output.
  • Concatenate in ABAP

    Preface – This post is part of the ABAP Programs series.

    Concatenate in ABAP is used to join two different strings together. It can be used to join any two string, number, special character or all together. In this article we will learn about the keyword CONCATENATE  and its implementation in an example program.

    Introduction

    Sometime we need to join two string, special character or numeric value in our code. In this case we can use ABAP keyword CONCATENATE and achieve our goal.

    Concatenate in ABAP
    Concatenate in ABAP Image Illustration

    ABAP Program

    Following program has implemented the same:

    PARAMETERS: lv_data1 type string, "VALUE 'BARRY'
                lv_data2 type string, "VALUE 'ALLEN'
    Data: lv_data type string.
    CONCATENATE lv_data1 lv_data2 into lv_data.
    WRITE: lv_data. "Output is 'BARRY ALLEN'
    

    Explanation

    This is a very easy program and self explanatory. Still given points explain the same:

    1. Initially, we have defined two parameters to take two different inputs: lv_data1 and lv_data2. We have given initial value to them too in comment.
    2. There after, we have defined a variable which will store the concatenated value.
    3. Now, we have done concatenation.
    4. Later, we have printed the concatenated output.
  • Leap Year Program in ABAP

    Preface – This post is part of the ABAP Programs series.

    Leap Year Program in ABAP is a mathematical expression based algorithm that is common across all other programming languages. In this article, we will discuss what is a leap year and how to write ABAP program to find if a year is a leap year or not.

    Introduction

    Leap Year is a year that has 29 days in the month of February. It means 366 days in year. If a year is divisible by 100, then it must be also divisible by 400 to become a leap year. Also, if the year is not divisible by 100 then in case it is divisible by 4, then it is also a leap year.

    Following are the past years that were leap year:

    Leap Year Program in ABAP
    Leap Year Program in ABAP Image Illustration

    ABAP Program

    Following ABAP program will find a year is a leap year or not:

    PARAMETERS: p_date TYPE dats.
    DATA : lv_date TYPE num4.
    lv_date = p_date.
    IF lv_date MOD 4 = 0.
      IF lv_date MOD 100 = 0.
        IF lv_date MOD 400 = 0.
          WRITE : 'Leap Year '.
        ELSE.
          WRITE : 'Not a Leap Year '.
        ENDIF.
      ELSE.
        WRITE : 'Leap Year '.
      ENDIF.
    ELSE.
      WRITE : 'Not a Leap Year '.
    ENDIF.
    

    Explanation

    To understand this program, we will discuss each lines one by one:

    1. Initially we have defined a parameter p_date of type DATS that is date type in ABAP. Also, we have defined a variable which will convert the date in a numeric date type using type conversion.
    2. Now If a year is divisible by 4, 100 and 400 then it is a leap year. We have added IF-ELSE condition accordingly.
  • Type Conversion in ABAP

    Preface – This post is part of the ABAP Programs series.

    Introduction

    Type conversion in ABAB means converting the type of a variable. In ABAP programs sometime we need to convert a variable from one type to other. This can be achieved with the help of type conversion or type casting. The same concept is illustrated in the image given below:

    Type Conversion in ABAP
    Type Conversion in ABAP Image Illustration

    Note: If you are searching for type casting/conversion of ABAP Classes,  click here.

    The given programs shows examples:

    1. Direct assignment based conversion

    Data : lv_string type string value '123' ,
            lv_num type num4.
    lv_num = lv_string.
    write : lv_num.
    

    Explanation

    The above code is explained line by line below:

    1. In the first line, we have defined two variables of different data types: one is lv_string of type string with value “123” and another is lv_num of type num4.
    2. This statement is used for type conversion. Here, we have directly assigned the value of lv_string to lv_num. Directly assigning the value causes conversion of the type.
    3. Now, we will directly print the converted value as output. In actual programming, it is further used according to the requirement.

    2. ABAP keyword based conversion

    Note: Type conversion  can also be performed explicitly using the ABAP keyword for conversion operator CONV.

    DATA lv_text TYPE c LENGTH 255 VALUE 'ABC'.
    DATA lv_xstr  TYPE xstring.
    DATA(lv_xstr) = cl_abap_codepage=>convert_to( source = CONV #( lv_text ) ).
    write : lv_xstr.

    Explanation

    The above code is explained line by line below:

    1. In the first and second line, we have defined two variables of different data types: one is lv_string of type string with value “ABC” and another is lv_xstr of type xstring.
    2. This statement is used for type conversion. Here, we have used CONV keyword to perform conversion.
    3. Now, we will directly print the converted value as output. In actual programming, it is further used according to the requirement.