Category: ABAP

ABAP

  • 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.
  • Program to check vowel or consonant in ABAP

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

    Program to check vowel or consonant in ABAP or in any other computer language utilizes its ASCII values. ASCII stands  for American Standard Code for Information Interchange. It is defined to represent every character on electronic communication in an encoded form. You can find list of all ASCII characters here.

    Steps to check vowel or consonant in a program

    To find whether a character is vowel or not we have two steps:

    1. Get ASCII of the character using Function Module URL_ASCII_CODE_GET and compare the ASCII code with 65, 69, 73, 79, 85 (A, E, I, O, U) and 97, 101, 105, 111, 117 (a,e,i,o,u).
    2. Directly compare the character with a set of characters as shown in program below:Program to check vowel or consonant in ABAP
      PARAMETERS: p_inp type c.
      IF p_inp  CA 'A,E,I,O,U'.
        Write: 'A Vowel'.
        ELSE.
          Write: 'A Consonant'.
          ENDIF.
      

       

    Explanation

    Lines mentioned below have line by line code explanation:

    1. In first line we have defined a parameter p_inp of type c. So, it takes only one character as input.
    2. In this line, we have compared the input using ABAP keyword “CA”. It is compared with vowels i.e. a, e, i, o and u.
    3. If the above line is successful then it is a vowel and thus ‘A Vowel’ is printed as output.
    4. If the second line is not successful then it is not a vowel and thus ‘A Consonant’ is printed as output.
  • Swapping of Two Numbers in ABAP

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

    Swapping of Two Numbers in ABAP  is a way to interchange the value of two variables with each other. It is rarely used in coding but it is an important topic for interviews. In this article we will cover both: swapping using third variable and swapping without using third variable in ABAP.

    Introduction

    Everyone is aware what a swap is. A Swap is a way to exchange the value of two variables with each other. There are many ways to do swapping in ABAP:

    Swapping using third variable:

    Swapping of Two Numbers

    Swapping of Two Numbers Image Illustration

    Here we save the value of 1st variable into a new 3rd variable. Now the 1st variable saves the value of 2nd variable and the 2nd variable saves the value of 3rd variable. In this way the value is exchanged.

    *------Do not copy this code, this is just for illustration-------*
    Data: lv_var1, lv_var2, lv_var3. "Here we have 3 variables
    lv_var1 = 2. "Assinging values to variables
    lv_var2 = 5.
    * swapping code
    lv_var3 = lv_var1. "Saving value of var 1 in var 3
    lv_var1 = lv_var2. "Saving value of var 2 in var 1
    lv_var2 = lv_var3. "Saving value of var 3 in var 1
    
    *** Now the output will show lv_var1 = 5 and lv_var2  = 2

    Swapping without using third variable:

    This can be achieved using either addition-subtraction or multiplication-division methods. We will show how in below illustration code:

    *------Do not copy this code, this is just for illustration-------*
    Data: lv_var1, lv_var2. "Here we have 2 variables
    lv_var1 = 2. "Assinging values to variables
    lv_var2 = 5.
    * swapping code
    lv_var1 = lv_var1 + lv_var2 . "Now lv_var1 = 2+5 = 7.
    lv_var2 = lv_var1 - lv_var2. "Now lv_var2 = 7-5 = 2.
    lv_var1 = lv_var1 - lv_var2. "Now lv_var1 = 7-2 = 5.
    
    *** Now the output will show lv_var1 = 5 and lv_var2  = 2

    Similarly using multiplication-division, we will achieve the same.

    Now the ABAP program to achieve the swap is shown below:

    PARAMETERS : lv_num1(4) type i,
                 lv_num2(4) type i.
    lv_num1 = lv_num1 + lv_num2.
    lv_num2 = lv_num1 - lv_num2.
    lv_num1 = lv_num1 - lv_num2.
    Write: 'Swapped Values are :', lv_num1, lv_num2.
    

     

  • PDF Download in ABAP

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

    In this article, you will learn how to convert xstring to pdf in abap and how to use GUI_DOWNLOAD Function Module to perform pdf download in ABAP.

    Introduction

    In our previous article we have uploaded a PDF file. To view it click here. Now we will be Downloading that file into our system. To do that we need to convert the XSTRING file into a PDF file. The program shown below uses Function Module GUI_DOWNLOAD to download the file and Function Module SCMS_XSTRING_TO_BINARY to convert the XSTRING File to a Binary file which is further converted to a PDF file using GUI_DOWNLOAD Function Module.
    Program to Download PDF in ABAP

    Parameters:
      p_fname type z1127582_upload-filename lower case,
      p_path type string lower case.
    
    
    Data:
          gs_store_file type z1127582_upload,
          xstr_content type xstring,
          gt_content type standard table of tdline,
          len type i,
          str_fname type string.
    
    Start-of-selection.
    
      select single * from z1127582_upload
        into gs_store_file
        where filename = p_fname.
    
      xstr_content  = gs_store_file-file_content.
    
      "Convert xstring/rawstring to binary ITAB
      call function 'SCMS_XSTRING_TO_BINARY'
        exporting
          buffer        = xstr_content
        importing
          output_length = len
        tables
          binary_tab    = gt_content.
      .
    
      if sy-subrc <> 0.
        message 'Unable to convert xstring to binary'
          type 'E'.
      endif.
    
      str_fname = p_fname.
    
      call function 'GUI_DOWNLOAD'
        exporting
          bin_filesize            = len
          filename                = p_path
          filetype                = 'BIN'
        tables
          data_tab                = gt_content
        exceptions
          file_write_error        = 1
          no_batch                = 2
          gui_refuse_filetransfer = 3
          invalid_type            = 4
          no_authority            = 5
          unknown_error           = 6
          header_not_allowed      = 7
          separator_not_allowed   = 8
          filesize_not_allowed    = 9
          header_too_long         = 10
          dp_error_create         = 11
          dp_error_send           = 12
          dp_error_write          = 13
          unknown_dp_error        = 14
          access_denied           = 15
          dp_out_of_memory        = 16
          disk_full               = 17
          dp_timeout              = 18
          file_not_found          = 19
          dataprovider_exception  = 20
          control_flush_error     = 21
          others                  = 22.
      if sy-subrc <> 0.
        message 'Unable to download file from SAP'
          type 'E'.
      endif.

    Explanation

    Initially, we have defined variables in two parts:

    • Parameter: p_fname is the name of file and p_path stores location of file
    • gs_store_file, gt_content, len, xstr_content: data related to file i.e. where file will be store, how the content will be stored, length of the file and type of content.

    Function Module used:

    • SCMS_XSTRING_TO_BINARY
    • GUI_DOWNLOAD

    What exactly is stored in the table that will be accessed by the Function Module to perform PDF download in ABAP:

    1. PDF file location
    2. PDF file name
    3. File type
    4. File X-STRING Data [This is the actual file that is converted in x-string]

    Later on we have called different Function Modules. We have added respective comment in the code itself.

    Tutorial Video

    You can watch the video below to learn implementation:

    [embedyt] https://www.youtube.com/watch?v=AWsp0EnHKTo[/embedyt]