Sams Teach Yourself ABAP/4® in 21 Days

Day 16

Formatting Techniques, Part 2

Chapter Objectives

After you have completed this chapter, you will be able to

List Formatting Statements

There are several statements commonly needed to format a list:

Using the new-line Statement

Use the new-line statement to cause the output of the next write statement to begin on a new line. Unlike skip or write /, consecutive new-line statements do not cause a blank line to appear in the output list.

Syntax for the new-line Statement

new-line [no-scrolling].

The no-scrolling addition locks the next line in place. Horizontal scrolling has no effect on lines that have been locked in this way.

Listing 16.1 illustrates the use of the new-line statement.

Listing 16.1  The NEW-LINE Statement

1  report ztx1601 line-size 255.
2  tables: ztxlfa1, ztxlfc3.
3  select * from ztxlfa1.
4      new-line no-scrolling.
5      write: ztxlfa1-lifnr, ztxlfa1-name1. "this line won't scroll right
6      select * from ztxlfc3 where lifnr = ztxlfa1-lifnr.
7          write: /14 ztxlfc3-bukrs, ztxlfc3-gjahr, ztxlfc3-shbkz,
8                  40 ztxlfc3-saldv, ztxlfc3-solll, ztxlfc3-habnl.
9          endselect.
10     endselect.

The code in Listing 16.1 produces the following output:

1000       Parts Unlimited
             1000 1990 A                    1,000.00         500.00
             1000 1991 A                        0.00       5,000.00
             1000 1992 A                    1,000.00      11,000.00
             1000 1993 A                        0.00      10,000.00
             1000 1994 A                        0.00       1,000.00
             1000 1995 A                    1,000.00       1,150.00
             1000 1995 Z                        0.00       9,000.00
             1000 1996 A                    1,000.00      50,000.00
             1000 1996 Z                    5,000.00           0.00
             1000 1998 Z                    4,000.00           0.00
             3000 1998 A                        0.00      10,000.00
1010       Industrial Pumps Inc.
             1000 1994 A                        0.00       1,000.00
             1000 1995 A                        0.00       1,500.00
             1000 1996 A                    1,500.00           0.00
1020       Chemical Nation Ltd.
             1000 1995 A                        0.00       2,000.00
             1000 1996 A                        0.00           0.00
1030       ChickenFeed Ltd.
1040       Motherboards Inc.
             1000 1990 A                      100.00       3,000.00
             1000 1997 A                      100.00       1,500.00
             2000 1997 A                        0.00         500.00
             2000 1998 A                    1,000.00         300.00
             3000 1998 A                        0.00       2,400.00

Lines 7 and 8 write vendor detail lines that are wider than the screen. Scrolling to the right allows you to see the right portion of the lines, but the vendor name and number stay within view. This happens because the write statement on line 5 is preceded by the new-line no-scrolling statement on line 4.

Using the new-page Statement

Use the new-page statement to

Syntax for the new-page Statement

new-page [no-title | with-title]
         [no-heading | with-heading]
         [line-count n(m)]
         [line-size k]
         [print on | print off]


The following points apply:

If you want to cause a page break and trigger the end-of-page event, code reserve sy-linct lines. sy-linct contains the current number of lines per page. The output from the next write will appear at the top of a new page, and the end-of-page event will be triggered beforehand causing a page footer to be created.

Using new-page print

At times there is a need for a report that both writes to the output list and sends output to the printer at the same time. For example, you may wish to write a report that shows a summary to the user online, and send a detail report to the printer at the same time. Use new-page print on to do this. The output from all write statements that follow new-page print on will not appear in the list. Instead, it will be sent to the spool. Issuing new-page print off reverses this effect. The output from all write statements that follow it will again be seen in the list.

Each time new-page print on is executed

Syntax for the new-page print on Statement

new-page print on
    [no dialog]
    [new section]
    [immediately        immedflag]
    [destination        destid]
    [copies             numcopies]
    [layout             layoutid]
    [list name          listname]
    [sap cover page     coverpageflag]
    [cover text         title]
    [department         dept]
    [receiver           recievername]
    [keep in spool      keepflag]
    [dataset expiration numdays]
    [line-count         linesperpage]
    [line-size          numcolumns]


The following points apply:

Additional parameters are documented in the ABAP/4 keyword documentation.

When you send output directly to the spool using new-page print on, you can also use two additional statements. The set margins statement allows you to control the margins during printing. The print-control statement allows you to send printer control codes directly to the printer. By using print-control, you can control any printer characteristic, such as CPI (character per inch), LPI (lines per inch), or font. These statements have no effect on lists printed via the Print button or via the List Print menu option; they only affect lists printed via new-page print on. They do not affect the way the list is displayed on screen. See the ABAP/4 keyword documentation for more details.

Listing 16.2 illustrates the use of the new-page print statement.

Listing 16.2   The NEW-PAGE PRINT Statement

1  report ztx1602.
2  tables ztxlfa1.
3  parameters: prt_id like estwo-spooldest obligatory.
5  new-page print on
6      no dialog
7      immediately 'X'
8      destination prt_id.
10 select * from ztxlfa1.
11     write: / ztxlfa1-lifnr.
12     endselect.
14 new-page print off.
15 write: / sy-dbcnt, 'lines sent to printer', prt_id.

The code in Listing 16.2 produces the following output:

        23  lines sent to printer BOS4           

Displaying the ABAP/4 Keyword Documentation for new-page Ppint

Normally, F1 help will not display the new-page print documentation. To display it, use the following procedure:

  1. Begin at the ABAP/4 Editor: Initial Screen.
  2. Choose the menu path Utilities-> ABAP/4 Key Word Doc. You then see the Display Structure: ABAP/4-SAP's 4GL Programming Language screen.
  3. Click the Find button on the standard toolbar. The Search Titles screen is displayed.
  4. In the Find field, type new-page print.
  5. Click the Continue button. You are returned to the previous screen and your cursor is positioned on the new-page print line.
  6. Double-click that line. The documentation for new-page print is displayed.

Using the skip Statement

The skip statement allows you to

Syntax for the skip Statement

skip [n | to line n].


Without any additions, skip generates a blank line. Skip n generates n blank lines. If n is less than 1, no blank lines are generated. skip at the end of the current page will cause a page break. skip will generate blank lines after any write statement, at the top of the first page, and at the top of any page begun by the new-page statement. It will not generate blank lines at the beginning of any other new page or at the end of the last page of the list. For example, if there is space for two more lines at the bottom of the current page, skip 5 will generate two blank lines at the bottom of the current page, but none at the top of the next page. This happens because the new page was not the first page and was not generated by a new-page statement.

If you want a footer on the last page of your report, code skip sy-linct as the last statement in your program. This statement will write footers. If you want to trigger a new page and you want a footer at the bottom the cur-rent page, code skip sy-linct instead of new-page.

skip to line n causes the current output position to be set to the beginning of line n of the current page. The output from the next write statement will begin at that position. For example, skip to line 3 causes the output from the next write statement to begin in column 1 of line 3. A skip to line statement that contains a line number greater than the current number of lines per page is ignored.

Listing 16.3 illustrates the use of the skip statement.

Listing 16.3  The SKIP Statement

1  report ztx1603.
2  tables ztxlfa1.
3  data n type i.
4  select * from ztxlfa1 order by land1.
5      on change of ztxlfa1-land1.
6          if sy-dbcnt > 1.
7              n = sy-linno - 1.
8              skip to line n.
9              write 35 '*** Last Vendor With This Country Code ***'.
10             endif.
11         write / ztxlfa1-land1.
12         endon.
13     write: /4 ztxlfa1-lifnr, ztxlfa1-name1.
14     endselect.

The code in Listing 16.3 produces the following output:

   1000       Parts Unlimited                                                        
   1010       Industrial Pumps Inc.                                                  
   1020       Chemical Nation Ltd.                                                   
   1030       ChickenFeed Ltd.     
   1050       The Bit Bucket                                                         
   1060       Memory Lane Ltd.                                                       
   1070       Flip My Switch Inc. *** Last Vendor With This Country Code ***
CC V9         Code Now, Specs Later Ltd.                                                    
   V10        Duncan's Mouse Inc. *** Last Vendor With This Country Code ***
DE V6         Anna Banana Ltd.
   V11        Wiener Schnitzel Inc.
   V12        Sauerkraut AG      *** Last Vendor With This Country Code ***
US V8         Smile When You Say That Ltd.
   1040       Motherboards Inc.
   1080       Silicon Sandwich Ltd.
   1090       Consume Ltd.
   2000       Monitors and More Ltd.
   V1         Quality First Ltd.
   V2         OverPriced Goods Inc.
   V3         Fluffy Bunnies Ltd.
   V4         Moo like a Cow Inc.
   V5         Wolfman Sport Accessories Inc.
   V7         The Breakfast Club Inc.

Each time the value of ztxlfa1-land1 changes

Using the back Statement

Use the back statement to return the current output position to

Syntax for the back Statement

The following is the syntax for the back statement:



Explaining further, if you specified no standard page heading on the report statement, it returns to the first line of the page. If you have a standard page heading, the output position is set to line first line following the standard header.

A sample program is shown in Listing 16.4.

Listing 16.4  The BACK Statement

1  report ztx1604 no standard page heading.
2  data n type i.
3  do 4 times.
4      write: /(4) sy-index.
5      enddo.
6  back.
7  do 4 times.
8      n = sy-index * 10.
9      write: /10(4) n.
10     enddo.
12 top-of-page.
13     write: / 'This was my title'.
14     uline.
15     back.
16     write 6 'is '.
17     skip.

The code in Listing 16.4 produces the following output:

This is  my title   
  1       10        
  2       20        
  3       30        
  4       40        

Using the position Statement

Use the position statement to specify the next output column on the current line. For example, skip to line 2. position 3. write 'Hi'. would write Hi on line 2 beginning in column 3.

Syntax for the position Statement

position v1.


Listing 16.5 illustrates the use of the position statement.

Listing 16.5   The POSITION Statement

1  report ztx1605.
2  data n type i.
3  do 4 times.
4      do 4 times.
5          position sy-index.
6          write 'A'.
7          enddo.
8      new-line.
9      enddo.
11 skip.
12 do 4 times.
13     do 4 times.
14         position sy-index.
15         write 'A'.
16         enddo.
17     skip.
18     enddo.

The code in Listing 16.5 produces the following output:


Using the position statement followed immediately by a write is equivalent to issuing a single write at statement (covered in the previous chapter).

Using the set blank lines Statement

Normally, if your program issues a write statement that results in a blank line being output, this blank line will be suppressed. For example, when writing out a variable that contains blanks, you will not see a blank line in the list. Use the set blank lines statement to change this behavior.

Syntax for the set blank lines Statement

set blank lines on | off.

The following points apply:

Listing 16.6 illustrates the set blank lines statement.

Listing 16.6  The SET BLANK LINES Statement

1  report ztx1606.
2  data: f1, f2, f3.
3  write: / 'Text 1'.
4  write: / f1, / f2, / f3.
5  write: / 'Text 2'.
6  skip.
8  set blank lines on.
9  write: / 'Text 3'.
10 write: / f1, / f2, / f3.
11 write: / 'Text 4'.
13 set blank lines off.
14 skip.
15 write: / 'Text 5'.
16 write: / f1, / f2, / f3.
17 write: / 'Text 6'.

The code in Listing 16.6 produces the following output:

  Text 1          
  Text 2          
  Text 3          
  Text 4          
  Text 5          
  Text 6           


DO use symbols and icons in your report to increase readability. DON'T hard-code a page size. Allow the users to specify it when they print the report.
 DON'T create reports wider than 132 characters if the user may print it. Most printers cannot print pages wider than 132 characters.


I don't really understand the difference between skip and new-line. Don't they both cause the next write to go to a new line?
The purpose of skip is to generate a blank line in the output. new-line causes the next line of output to be written to a new line, and does not generate blank lines.


The Workshop provides two ways for you to affirm what you've learned in this chapter. The Quiz section poses questions to help you solidify your understanding of the material covered and the Exercise section provides you with experience in using what you have learned. You can find answers to the quiz questions and exercises in Appendix B, "Answers to Quiz Questions and Exercises."


  1. What statement can you use to overwrite the same line?
  2. When would you want to use skip sy-linct instead of new-page if you want a footer?

Exercise 1

Explain the flow of control in the following program, and how it works.

Listing 16.7  top-of-page, position, and skip

report zty1607.
data n type i.
do 4 times.
    do 4 times.
        position sy-index.
        write 'A'.
    add 1 to n.
    skip to line n.

    n = sy-linno.

The code in Listing 16.7 produces the following output:


© Copyright, Macmillan Computer Publishing. All rights reserved.