ABAP Guide: Data Types, Strings and String Templates

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

Checked on 2026-11-13 against open-abap 2.14.30 (the abaplint transpiler and runtime), open-abap-core a9acc41, abaplint 2.120.103 and Node.js 24.21.

What you build

You work with one expense claim in text form. Along the way you declare variables of different types, convert values from one type to another, build sentences with string templates, clean and cut text with string functions, split a semicolon-separated record into parts and test the claim number with a regular expression (a pattern for text). At the end a small class, zdt_claim_text, turns the raw record C-1001; ada lovelace ;125.5;eur into the summary line Claim C-1001 | ADA LOVELACE | 125.50 EUR, and a report named zdt_summary prints it.

The claim number, the name and the amounts are made-up example values. The playground is open-abap, a community open-source project, not SAP software. It runs only the part of the ABAP language that it implements, so check a program on a real SAP system before you rely on it there. Output is written with WRITE, the classic list statement, because that is what the playground offers.

Terminal with the command npm run run:summary followed by 28 lines of output that end with the summary line Claim C-1001, ADA LOVELACE, 125.50 EUR and two messages for records that do not fit

Before you start

  • No account is needed: Node.js, the abaplint tools and open-abap install from the public npm registry without signing in. The licences you accept by installing (MIT for the abaplint packages) are listed in the README of the ABAP lab.

  • Set up the lab first, with the guide Set Up a Local ABAP Playground, and work through Your First ABAP Program: you need the folder abap-lab with npm install done, the habit of one .abap file plus one .xml file per object, and an editor such as Visual Studio Code open on it.

  • The lab already holds the finished files of this guide in modules/ROOT/examples/abap-lab/src/. Type them yourself rather than copying them, so that you meet the errors in this guide on your own screen.

  • Read Data Types and Data Objects for the types used here, Strings, String Templates and Regular Expressions for the text tools, and Expressions, Operators and Assignments for how a value changes type when it is stored or calculated.

  • The figures are drawings of the editor and the terminal, not screenshots. A highlighted box with a number marks the file or the output of the step with the same number. A figure of a run shows only the lines that the phase added, and no figure shows the lines the transpiler prints about its file counts.

Steps

The report zdt_summary grows from phase to phase. Every phase adds a block of lines at the end of the report and ends with a run, so the output of each run contains the lines of all earlier phases too. All names that start with zdt_ belong to this guide.

Declare data and convert values

  1. Create the report src/zdt_summary.prog.abap with the lines below, and its attribute file src/zdt_summary.prog.xml next to it. A variable is declared with DATA, a name and a type. The type string holds a text of any length and keeps every character, including blanks at the end. The type c LENGTH 20 is a text field of exactly twenty characters: a shorter value is filled up with blanks, and a longer value is cut off, as short_name shows, which has room for five characters only. The statement DATA(item_count) = 3. is an inline declaration: ABAP works out the type from the value, here an integer. Each WRITE line prints a template (the text between vertical bars, with values in { }) and strlen counts the characters.

    REPORT zdt_summary.
    
    START-OF-SELECTION.
      DATA claim_id TYPE string VALUE `C-1001`.
      DATA employee TYPE c LENGTH 20 VALUE 'Ada Lovelace'.
      DATA short_name TYPE c LENGTH 5.
      DATA(item_count) = 3.
    
      short_name = employee.
      WRITE / |{ claim_id }: { strlen( claim_id ) } characters|.
      WRITE / |{ employee }: { strlen( employee ) } characters|.
      WRITE / |{ short_name }: { strlen( short_name ) } characters|.
      WRITE / |{ item_count } items|.
    <?xml version="1.0" encoding="utf-8"?>
    <abapGit version="v1.0.0" serializer="LCL_OBJECT_PROG" serializer_version="v1.0.0">
     <asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
      <asx:values>
       <PROGDIR>
        <NAME>ZDT_SUMMARY</NAME>
        <SUBC>1</SUBC>
        <RLOAD>E</RLOAD>
        <UCCHECK>X</UCCHECK>
       </PROGDIR>
      </asx:values>
     </asx:abap>
    </abapGit>
    Visual Studio Code with the file src/zdt_summary.prog.abap open: a report with the declarations of claim_id as a string, employee and short_name as text fields and item_count as an inline declaration, followed by four WRITE statements; callout 1 marks the report file in the explorer
  2. Add these lines after the last WRITE line of the report. They convert values. 7 / 2 calculates with whole numbers, so the result is rounded to 4. CONV decfloat34( 7 ) first turns the 7 into a decimal floating-point number, so the division keeps its decimals. CONV i( '42' ) turns the text 42 into the integer 42, which can then be added to.

      DATA(share) = 7 / 2.
      DATA(exact_share) = CONV decfloat34( 7 ) / 2.
      DATA(units) = CONV i( '42' ) + 1.
      WRITE / |Integer division: { share }|.
      WRITE / |With CONV: { exact_share }|.
      WRITE / |Text to number plus 1: { units }|.
  3. In the terminal, in the abap-lab folder, run the script that transpiles and runs the report:

    npm run run:summary

    The first four lines are the declarations: C-1001 has 6 characters, Ada Lovelace has 12 (the blanks that fill the field are not counted), Ada L is what fitted into five characters, and 3 items. The next three are the conversions: 4 for the integer division, 3.5 with CONV, and 43 for the text turned into a number.

    Terminal after npm run run:summary: seven lines of output, from C-1001: 6 characters to Text to number plus 1: 43; callout 3 marks the seven lines

Build and clean text

  1. Add the templates. A template can hold a variable, a calculation ({ item_count * 10 }) or a function call between the braces. Write a space after the opening brace and before the closing brace. The option DECIMALS after a value sets the number of decimal places: with 2 the amount shows as 125.50, and with 0 it is rounded to 126.

      DATA(currency) = `EUR`.
      DATA(amount) = CONV decfloat34( '125.5' ).
      DATA(line) = |Claim { claim_id } for { employee }: { amount } { currency }|.
      WRITE / line.
      WRITE / |{ item_count } items, { item_count * 10 } { currency } pending|.
      WRITE / |Two decimals: { amount DECIMALS = 2 }|.
      WRITE / |No decimals: { amount DECIMALS = 0 }|.
  2. Add the string functions. A string function takes a text and returns a new one, and leaves its input as it was. condense removes the blanks at both ends and shortens each run of blanks inside to one, and to_upper makes capitals, so the 21-character name becomes ADA LOVELACE with 12. substring takes four characters after skipping two. substring_before cuts before the first dash. find returns the position of the first dash, where the first character is at position 0. replace swaps a piece of text for another.

      DATA(raw_name) = `   ada    lovelace   `.
      DATA(clean_name) = to_upper( condense( raw_name ) ).
      WRITE / |{ strlen( raw_name ) } characters before, { strlen( clean_name ) } after: { clean_name }|.
      WRITE / |Number part: { substring( val = claim_id off = 2 len = 4 ) }|.
      WRITE / |Before the dash: { substring_before( val = claim_id sub = `-` ) }|.
      WRITE / |Position of the dash: { find( val = claim_id sub = `-` ) }|.
      WRITE / |Replaced: { replace( val = claim_id sub = `C-` with = `CLAIM-` ) }|.
  3. Run npm run run:summary again. The nine new lines follow the seven lines of the first run.

    Terminal after npm run run:summary: nine new lines of output that start with the claim line Claim C-1001 for Ada Lovelace: 125.5 EUR and end with Replaced: CLAIM-1001; callout 6 marks the nine lines

Split a record and check it

  1. Add a raw record, as it might arrive from a file, and cut it at the semicolons. SPLIT ... INTO TABLE puts every piece in one row of an internal table (a list of values, here of texts). lines( parts ) counts the rows, and the LOOP prints each row in square brackets, so you see that the blanks around the name are still there: splitting does not clean.

      DATA(raw_line) = `C-1001; ada  lovelace ;125.5;eur`.
      DATA parts TYPE STANDARD TABLE OF string WITH EMPTY KEY.
      SPLIT raw_line AT ';' INTO TABLE parts.
      WRITE / |{ lines( parts ) } parts|.
      LOOP AT parts INTO DATA(part).
        WRITE / |[{ part }]|.
      ENDLOOP.
  2. Add the pattern checks. The pattern C-\d{4} reads: the letter C, a dash, then exactly four digits (\d is one digit). matches is true only when the whole text fits the pattern, so C-1001 fits and C-12 does not. In FIND PCRE, the parts of the pattern in parentheses are remembered and handed back with SUBMATCHES: here the first word and the amount behind it. replace with a pattern and occ = 0 replaces every match. Inside backquotes the backslashes stay as they are.

      IF matches( val = claim_id pcre = `C-\d{4}` ).
        WRITE / |{ claim_id } is a claim number|.
      ENDIF.
      IF NOT matches( val = `C-12` pcre = `C-\d{4}` ).
        WRITE / |C-12 is not a claim number|.
      ENDIF.
    
      DATA(note) = `Taxi 12.50 and hotel 80.00`.
      FIND PCRE `(\w+)\s+(\d+\.\d\d)` IN note SUBMATCHES DATA(item) DATA(price).
      WRITE / |First item: { item } costs { price }|.
      WRITE / replace( val = note pcre = `\d+\.\d\d` with = `<amount>` occ = 0 ).
  3. Run npm run run:summary. Nine more lines appear.

    Terminal after npm run run:summary: nine new lines of output, the split record as 4 parts with each part in square brackets, then the two checks of the claim number, the first item and the masked note; callout 9 marks the nine lines

Put the steps in a class

  1. Move what you learned into a class, so that any program can reuse it. Create src/zdt_claim_text.clas.abap and its attribute file src/zdt_claim_text.clas.xml. The method summary_line takes a raw record and returns one line. It splits the record, gives up with a message when there are not four parts or when the claim number does not fit the pattern (RETURN leaves the method), and otherwise cleans each part, converts the amount with CONV and builds the result with a template. A bar inside a template is written \|, and parts[ 1 ] reads the first row of the table.

    CLASS zdt_claim_text DEFINITION PUBLIC FINAL CREATE PUBLIC.
      PUBLIC SECTION.
        CLASS-METHODS summary_line
          IMPORTING raw         TYPE string
          RETURNING VALUE(line) TYPE string.
    ENDCLASS.
    
    CLASS zdt_claim_text IMPLEMENTATION.
      METHOD summary_line.
        DATA parts TYPE STANDARD TABLE OF string WITH EMPTY KEY.
        SPLIT raw AT ';' INTO TABLE parts.
        IF lines( parts ) <> 4.
          line = |Cannot read: { raw }|.
          RETURN.
        ENDIF.
    
        DATA(id) = condense( parts[ 1 ] ).
        IF NOT matches( val = id pcre = `C-\d{4}` ).
          line = |Not a claim number: { id }|.
          RETURN.
        ENDIF.
    
        DATA(name) = to_upper( condense( parts[ 2 ] ) ).
        DATA(amount) = CONV decfloat34( condense( parts[ 3 ] ) ).
        DATA(currency) = to_upper( condense( parts[ 4 ] ) ).
        line = |Claim { id } \| { name } \| { amount DECIMALS = 2 } { currency }|.
      ENDMETHOD.
    ENDCLASS.
    <?xml version="1.0" encoding="utf-8"?>
    <abapGit version="v1.0.0" serializer="LCL_OBJECT_CLAS" serializer_version="v1.0.0">
     <asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
      <asx:values>
       <VSEOCLASS>
        <CLSNAME>ZDT_CLAIM_TEXT</CLSNAME>
        <LANGU>E</LANGU>
        <DESCRIPT>Claim text helper</DESCRIPT>
        <STATE>1</STATE>
        <FIXPT>X</FIXPT>
        <UNICODE>X</UNICODE>
       </VSEOCLASS>
      </asx:values>
     </asx:abap>
    </abapGit>
    Visual Studio Code with the file src/zdt_claim_text.clas.abap open: the class zdt_claim_text with the static method summary_line that splits a record, checks the claim number with a pattern, cleans the parts and builds the summary line with a string template; callout 10 marks the class file in the explorer
  2. Add three calls at the end of the report: the raw record from the split step, a record with a wrong claim number and a record with too few parts.

      WRITE / zdt_claim_text=>summary_line( raw_line ).
      WRITE / zdt_claim_text=>summary_line( `X-1; ada lovelace;10;eur` ).
      WRITE / zdt_claim_text=>summary_line( `C-1002;Ben` ).
  3. Run npm run run:summary. The last three lines are the summary line Claim C-1001 | ADA LOVELACE | 125.50 EUR and the two messages Not a claim number: X-1 and Cannot read: C-1002;Ben.

Read your first error

  1. In src/zdt_summary.prog.abap, remove the spaces inside the braces of the first WRITE statement (line 10), so that { claim_id } becomes {claim_id}. Save the file.

  2. Run npm run run:summary. The transpiler checks the syntax first and stops at the first problem. The line names the check that failed (parser_error), says that the statement is not valid, gives the statement it could not read (WRITE) and ends with the file and line. Node.js does not run the program. The message does not name the real cause, the missing spaces, so look at the braces on that line.

    Terminal after npm run run:summary with the spaces removed inside the braces of the first template: the transpiler stops with the line Error: parser_error, Statement does not exist in the configured ABAP version or a parser error, WRITE, zdt_summary.prog.abap:10 and the program does not run; callout 14 marks the error line
  3. Put the spaces back, save and run npm run run:summary again. The 28 lines return.

Check the result

npm run run:summary ends without an error and prints 28 lines, from C-1001: 6 characters to Cannot read: C-1002;Ben. npm run lint prints abaplint: 0 issue(s) found, and npm test still passes with the test of the lab’s greeter class.

Variations

  • Change the declaration of short_name to c LENGTH 8 and run again to see how much of the name fits.

  • Ask find for a piece of text that is not there, for example find( val = claim_id sub = `X` ): it returns -1.

  • Change the amount in the raw record to 7 and read 7.00 in the summary line.

  • The template options WIDTH, ALIGN and PAD format text to a fixed width. They are described on Strings, String Templates and Regular Expressions; the playground does not apply the alignment the way an SAP system does, so this guide leaves them out.

What can go wrong

Symptom Cause Fix

Error: parser_error, Statement does not exist in the configured ABAP version(or a parser error), "WRITE", zdt_summary.prog.abap:10

There is no space after { or before } inside a template.

Open the file and line named at the end of the message and write { name } with a space on both sides.

A calculation shows 4 where you expected 3.5

Both operands of 7 / 2 are integers, so the calculation is done in whole numbers.

Convert one operand first: CONV decfloat34( 7 ) / 2.

A long block of text starts with cx_sy_conversion_no_number [Error] and the exit code is 1

CONV i( ... ) was given a text that holds no number, such as abc, and nothing handles the exception.

Check the text first, for example with matches( val = text pcre = `\d+` ), or handle the exception (the guide on classes, interfaces and exceptions shows how).

A long block of text starts with cx_sy_range_out_of_bounds [Error]

substring was asked for characters behind the end of the text, for example off = 4 len = 10 on the six characters of C-1001.

Compare the length with strlen first, or use substring_before and substring_after.

A text that you wrote as 'ab ' is 2 characters long, not 5

A text between single quotes is a text field literal, and the blanks at the end are not kept when it is stored in a string.

Write a text that must keep trailing blanks between backquotes: `ab `.

npm run lint prints Variable "n" not used (unused_variables) [E]

A variable was declared with DATA or an inline declaration and never read.

Use the variable or delete the declaration.

Where to go next