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 Take – Free 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.
<code>* Delete Blank Lines DELETE it_vm_data_tab WHERE werks IS INITIAL. DELETE it_pm_data_tab WHERE werks IS INITIAL.</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.
<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 Check – How 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
<code>* Types Declaration TYPES: BEGIN OF ty_vm_file, werks TYPE werks_d, matnr TYPE matnr, lifnr TYPE lifnr, ztype TYPE char1, END OF ty_vm_file, BEGIN OF ty_pm_file, werks TYPE werks_d, matnr TYPE matnr, htype TYPE char1, zcagn TYPE char2, END OF ty_pm_file, BEGIN OF ty_filename, filename TYPE text1024, END OF ty_filename, * Table Type declaration tt_vm_tab TYPE TABLE OF ty_vm_file WITH EMPTY KEY, tt_pm_tab TYPE TABLE OF ty_pm_file WITH EMPTY KEY, tt_filename_tab TYPE TABLE OF ty_filename WITH EMPTY KEY. * 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) = VALUE tt_filename_tab( ( filename = 'VM_CA02_0074203_0000102207_H.txt' ) ( filename = 'PM_CA02_0074203_C_HA.txt' ) ). DATA(it_vm_data_tab) = VALUE tt_vm_tab( FOR ls_filename IN it_fillename_data_tab WHERE ( filename(3) EQ 'VM_' ) ( werks = substring_after( val = substring_before( val = ls_filename-filename sub = '_' occ = 2 ) sub = '_' occ = 1 ) matnr = substring_after( val = substring_before( val = ls_filename-filename sub = '_' occ = 3 ) sub = '_' occ = 2 ) lifnr = substring_after( val = substring_before( val = ls_filename-filename sub = '_' occ = 4 ) sub = '_' occ = 3 ) ztype = substring_after( val = substring_before( val = ls_filename-filename sub = '.' occ = -1 ) sub = '_' occ = -1 ) ) ). DATA(it_pm_data_tab) = VALUE tt_pm_tab( FOR ls_filename IN it_fillename_data_tab WHERE ( filename(3) EQ 'PM_' ) ( werks = substring_after( val = substring_before( val = ls_filename-filename sub = '_' occ = 2 ) sub = '_' occ = 1 ) matnr = substring_after( val = substring_before( val = ls_filename-filename sub = '_' occ = 3 ) sub = '_' occ = 2 ) htype = substring_after( val = substring_before( val = ls_filename-filename sub = '_' occ = 4 ) sub = '_' occ = 3 ) zcagn = substring_after( val = substring_before( val = ls_filename-filename sub = '.' 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
- Dynamic Where Condition usage in Database queries
- UNIX Command in SAP ABAP
- Send e-mail with subject line greater than 50 characters
- Pop Up Screen with Selection Option using FM POPUP_WITH_TABLE_DISPLAY
- String wrap
- Write Application Log
- Sample program to attach any file from application server to any Business Object
- Sample program to unlock the editor of a program.
- Why are developers so fond of ‘REUSE_ALV_GRID_DISPLAY’?
- DELETING rows of the internal table within the LOOP. Is it a Taboo? A big NO NO?
- Creating Dynamic Internal Table
- Selection Screen in SAP
- How to update custom field of PRPS table
- Get Latitude and Longitude of any place using Google Map API in SAP
- GPS like tool in SAP using Google Map API
- Know Who is doing What in your SAP System
- OOPs Report Using Splitter and ALV Tree Combination
- ALV with an Editable Row
- Extensive Tips and Tricks for Interactive SAP ALV
- How to Get Accurate Pricing in SD for Customer and Material?
- T-Guard: Transport Dependency Validation Tool
- Utility Program to Auto Format the Texts into Meaningful Sentence
- ABAP for SAP HANA. Part XX. ALV Report On SAP HANA – Opportunities And Challenges
- Selective Handling of the Buttons in ALV Grid Toolbar
- Working with Shell Scripts in SAP ABAP Environment
- CDS Part 7. Basic Expressions & Operations Available for CDS View – II
- Sudoku Solver and Generator Tool Using Vanilla SAP ABAP
- Calculator in SAP using New ABAP Syntax
- SO10 Enhancement to Add Texts to the Transport Request Automatically
- How to pass Reversal Date & Reason to BAPI_ACC_DOCUMENT_POST?
- ISU – Bankruptcy Overview and Write-Off Process using BAPI_CTRACDOCUMENT_WRITEOFF
- How to Create or Update Variant for a Report from another Program?
- A to Z of OLE Excel in ABAP 7.4
- SORTing Algorithm – Interview Question
- SOLMAN – Mail Forms Custom Enhancement Guide
- SORTing Algorithm – Performance Comparision
- How to Get Data from Web Page using SAP ABAP?
- Dynamic Code Processor
- Useful Trick to Search “Text” in Smartforms/Text modules
- Dynamically Download Data From Any SAP Table in ABAP-740 – Part 1
- How to SPLIT Data in FOR LOOP Using Modern ABAP Syntax?
- Dynamic Table Download in ABAP 751 – Part 2