Thursday, 05 September 2019 16:34

How to SPLIT Data in FOR LOOP Using Modern ABAP Syntax?

Written by https://sapyard.com/how-to-split-data-in-for-loop-using-modern-abap-syntax/
Rate this item
(0 votes)

SAP ABAP Developers are familiar with LOOP — ENDLOOP syntax. FOR Loop is relatively new to ABAPers, though other programming language use it very commonly. Every developer has

seen this syntax in some or other programming language: for (i=1; i<=3; i++).Why would ABAP stay behind?

But old habits die heard. The lazy developers who were comfortable with traditional ABAP programming find it challenging to use the new syntax. In this article today, we would discuss one requirement which we can easily achieve in old ABAP, but needed some help for the new syntax.

This post is again the result of the question we had in our SAP Technical Telegram Group. One of our very active and helpful members, Wesley Massini had a program where he was reading data from a file with fixed length. It was working fine and with no issue. But later on, the business decided to separate the data with underscore “_” and the file data is not fixed any more. They want to separate the data into columns at the delimiter underscore. This delimiter can be anything viz comma(,), tilds (~), pipe (|) etc.

Sounds an easy requirement right? But can you do using the new ABAP Syntax? ?

Also TakeFree Video Course on New Syntax in ABAP

Existing Code for Fixed Length Data

The data in the file would look like this:

It is a file with heterogeneous data. The data in the first row and second row are different. For example, the first two fields (yellow and blue) in both the rows are Plant and Material. But the third field in red is Vendor is first row and while it is an indicator in second row. Similarly fourth field in green is a custom type in first row and some category in second row. The idea is, all the data rows in the file for type first should go to one internal table and all rows like that of second type should go into another internal table.

We used SWITCH statement in new syntax and separated the rows based on the length of the data in the file (which is fixed). Row type one has 26 characters while row type two has 18 characters. Also, we do not need to show the extension .txt.

As author and ABAPer Paul Hardy says, CDS is ABAP in Steriods. We say, not only CDS, the new ABAP itself looks like super active ABAP. ?

Check the above screenshot. We have In-line data declaration, usage of VALUE, FOR Loop in internal table, SWITCH and #. Do a F4 on each of these keywords and try to understand the concept.

For our case, we have know the TYPE, therefore they come after the VALUE operator. Also after SWITCH there is #. The table types are declared like below.

In order to display the output, we can write the below syntax.

Output

How to Handle Blank Rows?

We are not sure, how to avoid the blank rows in both the tables using the modern syntax. We used a DELETE statement explicitly after the data was populated in the internal tables.

If you know how to remove the blank lines in the FOR LOOP with SWITCH, please provide the solution in the comments section. We will update this article with your solution.

1
2
3
4
<code>*DeleteBlankLines
DELETEit_vm_data_tabWHEREwerksISINITIAL.
DELETEit_pm_data_tabWHEREwerksISINITIAL.</code>

Also Read My First Program in S/4HANA

New Requirement

Earlier our file was fixed length. With the business change, we started receiving file with a delimiter underscore “_”. So, we did not need to worry about the length of each fields. But again, we struggled to figure out the best way to SPLIT at “_” using new ABAP.

Solution 1

After some research and with our new knowledge, we achieved the functionality using STRLEN, SUBSTRING_BEFORE, SUBSTRING_AFTER , sub, occ string functions along with the VALUE and FOR Loop and COND keywords.

Note: The key word occ is for Occurrence. You might see a negative number for occ = -1. If occ is positive value, the occurrences are counted from the left while if occ is a negative number, it is counted from the right side.

Similarly check the keyword sub. It is for Substring. Sub looks for the characters in the string.

1
2
3
4
5
6
7
<code>DATA  result TYPE string.
result = substring( val = 'ABCDEFGH' off = 2 len = 2 ).
result = substring_from( val = 'ABCDEFGH' sub = 'CD' ).
result = substring_after( val = 'ABCDEFGH' sub = 'CD' ).
result = substring_before( val = 'ABCDEFGH' sub = 'CD' ).
result = substring_to( val = 'ABCDEFGH' sub = 'CD' ).</code>

The above snippet is from SAP Help to show the usage of SUB and different SUBSTRING functions. The output for each result is “CD”, “CDEFGH”, “EFGH”, “AB”, and “ABCD” in the same order.

The above code also gave a blank line and we were forced to use the DELETE statement explicitly. Also, we had COND in the syntax which could be avoided. We achieved the business requirement, but we were still wondering if there is a better way to achieve it. Without the DELETE statement and COND. And guess what? Stephan gave a flawless solution yet again!!

Also CheckHow I Created My First SAP OData Services

Solution 2 from Stephan

Stephan recommended to put an identifier in the beginning of the data row for the file. For our case, it is VM and PM. Also, with his new solution logic there is no need to use COND.

Check the solution above. He has used WHERE clause in the FOR Loop. There is no need for WHEN and THEN. Also, the STRLEN check of each row is prevented along with COND #.

Code Snippet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<code>*TypesDeclaration
TYPES:
BEGIN OFty_vm_file,
werksTYPEwerks_d,
matnrTYPEmatnr,
lifnrTYPElifnr,
ztypeTYPEchar1,
END OFty_vm_file,
BEGIN OFty_pm_file,
werksTYPEwerks_d,
matnrTYPEmatnr,
htypeTYPEchar1,
zcagnTYPEchar2,
END OFty_pm_file,
BEGIN OFty_filename,
filenameTYPEtext1024,
END OFty_filename,
* Table Type declaration
tt_vm_tabTYPETABLEOFty_vm_fileWITHEMPTYKEY,
tt_pm_tabTYPETABLEOFty_pm_fileWITHEMPTYKEY,
tt_filename_tabTYPETABLEOFty_filenameWITHEMPTYKEY.
* My recommendation is to put an identifier at the beginning of the filename like in this sample.
* With this logic there is no need for usage of COND like in the first sample.
DATA(it_fillename_data_tab)=VALUEtt_filename_tab((filename='VM_CA02_0074203_0000102207_H.txt')
(filename='PM_CA02_0074203_C_HA.txt')).
DATA(it_vm_data_tab)=VALUEtt_vm_tab(FORls_filenameINit_fillename_data_tab
WHERE(filename(3)EQ'VM_')
(werks=substring_after(val=substring_before(val=ls_filename-filenamesub='_'occ=2)sub='_'occ=1)
matnr=substring_after(val=substring_before(val=ls_filename-filenamesub='_'occ=3)sub='_'occ=2)
lifnr=substring_after(val=substring_before(val=ls_filename-filenamesub='_'occ=4)sub='_'occ=3)
ztype=substring_after(val=substring_before(val=ls_filename-filenamesub='.'occ=-1)sub='_'occ=-1)
)).
DATA(it_pm_data_tab)=VALUEtt_pm_tab(FORls_filenameINit_fillename_data_tab
WHERE(filename(3)EQ'PM_')
(werks=substring_after(val=substring_before(val=ls_filename-filenamesub='_'occ=2)sub='_'occ=1)
matnr=substring_after(val=substring_before(val=ls_filename-filenamesub='_'occ=3)sub='_'occ=2)
htype=substring_after(val=substring_before(val=ls_filename-filenamesub='_'occ=4)sub='_'occ=3)
zcagn=substring_after(val=substring_before(val=ls_filename-filenamesub='.'occ=-1)sub='_'occ=-1)
)).
* Add both table data for display
cl_demo_output=>write_data(it_vm_data_tab).
cl_demo_output=>write_data(it_pm_data_tab).
** Show the output
cl_demo_output=>display().</code>

Download SPLIT in New ABAP Program for all the three methods shown in this article and practice it.

If you have a better solution, please do share. Your feedback and comments are welcome. Please let them coming.

You may connect with with Stephan at LinkedIn or visit his company website KCO for any SAP Consulting Work.

5450+ SAP Technical Practitioners from 6 Continents of the SAP World. Install Telegram app and click this link to join – Telegram SAP Technical Discuss Group.

Please SUBSCRIBE to SAPYard’s Youtube Channel for Free End to End SAP Video Course and Training.

Check Some Useful Code Snippets

Read 121 times

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.