sapdev logo background
sapdev logo sapdev logo
Comments

Concatenate ABAP statement syntax for concatenating data objects, sting values or rows of an SAP internal table



The concatenate statement has formed an essential part of the ABAP developers role for many years allowing them to connect/join/concatenate data strings together within one variable i.e.

CONCATENATE ld_string1 'some more text' ld_value2 into ld_result.


There is also basic additions such as the "SEPARATED BY SPACE" or any other character such as "SEPARATED BY 'HH'" which allows you to place a space or character between each of the concatenated objects i.e.

CONCATENATE ld_value1 'hello' into ld_result SEPARATED BY SPACE.
CONCATENATE ld_value1 'hello' into ld_result SEPARATED BY ','.


More recently a new way of concatenating data has emerged which can make the process much simpler, especially for the more complicated concatenations which required different space separations between each object. The new method has the following form and replicates the first example above but using the new method:

ld_result = |{ ld_string1 }| && |some more text| && |{ ld_value2 }|.


The above example of the modern syntax probably doesn't look much of an improvement and initially will just look a bit strange. As the concatenations become more complicated this method does come into its own and you will get used to it in about 5 mins. In-fact if you check out the following example code Iím sure you will see the benefits.

ld_concatnew = |Value 1= { p_value1 },| && | Value 2= { p_value2 }.|.
  vs
CONCATENATE 'Value 1=' p_value1 into ld_concatorig SEPARATED BY space.
CONCATENATE ld_concatorig ',' into ld_concatorig.
CONCATENATE ld_concatorig 'Value 2=' p_value2 into ld_concatorig SEPARATED BY space.
CONCATENATE ld_concatorig '.' into ld_concatorig.


Issues with the new modern method
Although the modern syntax can simplify some of the more complex concatenate requirements there are a few drawbacks. The first being that it is a new syntax to learn. But more problematic could be that it does not use the return code system field sy-subrc. For example if the result of the concatenate does not fit into the target variable the CONTACENATE statement will return value 4 in the sy-subrc field but using the new method sy-subrc will remain 0.

i.e.
data: ld_length10(10) type c,
      ld_length15(15) type c,
      ld_length5(5) type c,
      ld_length5_new(5) type c.

ld_length5_new = |{ ld_length10 }| && |{ ld_length15 }|.
*Return code will equal 0 even though ld_length5 is too short

CONCATENATE ld_length10 ld_length15 into ld_length5 SEPARATED BY space.
*Return code will equal 4 as ld_length5 is too short


Full code listing
You can also paste this full ABAP code listing into your SAP system and execute/debug it to get a more visual demonstration of the new modern concatenate technique and all the topics discussed above.

*&---------------------------------------------------------------------*
*& Report  Modern syntax for Concatenation statements
*&
*&---------------------------------------------------------------------*
*& SAP ABAP Concatenate statement including demonstration of new
*& modern syntax
*& By SAP Development
*&---------------------------------------------------------------------*
REPORT ZCONCATENATE_STATEMENT.

data: ld_concatnew type String,
      ld_concatorig type String,
      ld_concatdiff type String.

data: ld_length10(10) type c,
      ld_length15(15) type c,
      ld_length5(5) type c,
      ld_length5_new(5) type c.

PARAMETERS: p_value1 type string default '22',
            p_value2 type string default '33'.

*New way of Concatenating data with SAP ABAP
ld_concatnew = |Value 1= { p_value1 },| && | Value 2= { p_value2 }.|.

*Original way of Concatenating data with SAP ABAP
CONCATENATE 'Value 1=' p_value1 into ld_concatorig SEPARATED BY space.
CONCATENATE ld_concatorig ',' into ld_concatorig.
CONCATENATE ld_concatorig 'Value 2=' p_value2 into ld_concatorig SEPARATED BY space.
CONCATENATE ld_concatorig '.' into ld_concatorig.


* Just to hightlight what would happen if i didnt break the concatenate statement up
* into multiple concatenate statements this shows how the spacings would be different
CONCATENATE 'Value 1=' p_value1 ',' 'Value 2=' p_value2 '.' into ld_concatdiff SEPARATED BY space.

write:/  ld_concatnew, '-New method'.  "new
write:/  ld_concatorig, '-Orig method'. "orig/old
write:/  ld_concatdiff, '-New method difference'. "highlight differences
skip 2. "inserts 2 blank lines


*Return code test
* Just to demonstrate a few advantages of the CONCATENATE method in that it implements
* return code feedback when issue exist, i.e. when the concatenated objects will
* not fit into the result variable
*
* As you can see from the below example the reult is the same but you do not know that
* an issue has occured
ld_length10 = '1111111111'.
ld_length15 = '222222222222222'.

write:/ 'Return Code Results'.
write:/ '-------------------'.
skip.

ld_length5_new = |{ ld_length10 }| && |{ ld_length15 }|.
if sy-subrc ne 0.
  write:/ 'Error data will not fit(new method)'.
  write:/ 'Result:', ld_length5_new.
else.
  write:/ 'No error returned(new method)'.
  write:/ 'Result:', ld_length5_new.
endif.

skip. "skips 1 line

CONCATENATE ld_length10 ld_length15 into ld_length5 SEPARATED BY space.
if sy-subrc ne 0.
  write:/ 'Error data will not fit'.
  write:/ 'Result:', ld_length5.
else.
  write:/ 'No error'.
  write:/ 'Result:', ld_length5.
endif.

Also see ABAP Concatenate statement for details of full syntax options.



comments powered by Disqus


Can't find something on ERPWorkbench? A quick search should fix that: