Previous Page | Next Page

Understanding and Customizing SAS Output: The Basics

Controlling the Appearance of Pages


Input Data Set for Examples of Multiple-page Reports

In the sections that follow, you learn how to customize multiple-page reports.

The following program creates and prints a SAS data set that contains newspaper circulation figures for morning and evening editions. Each record lists the state, morning circulation figures (in thousands), evening circulation figures (in thousands), and year that the data represents.

data circulation_figures;
   length state $ 15;
   input state $ morning_copies evening_copies year;
   datalines;
Colorado  738.6 210.2 1984
Colorado  742.2 212.3 1985
Colorado  731.7 209.7 1986
Colorado  789.2 155.9 1987
Vermont   623.4 566.1 1984
Vermont   533.1 455.9 1985
Vermont   544.2 566.7 1986
Vermont   322.3 423.8 1987 
Alaska     51.0  80.7 1984
Alaska     58.7  78.3 1985
Alaska     59.8  70.9 1986
Alaska     64.3  64.6 1987
Alabama   256.3 480.5 1984
Alabama   291.5 454.3 1985
Alabama   303.6 454.7 1986
Alabama      .  454.5 1987
Maine        .     .  1984
Maine        .   68.0 1985
Maine     222.7  68.6 1986
Maine     224.1  66.7 1987
Hawaii    433.5 122.3 1984 
Hawaii    455.6 245.1 1985
Hawaii    499.3 355.2 1986
Hawaii    503.2 488.6 1987
;

proc print data=circulation_figures;
run;

The following output shows the results:

SAS Data Set CIRCULATION_FIGURES

                                 The SAS System                                1

                                   morning_    evening_
                Obs     state       copies      copies     year

                  1    Colorado      738.6       210.2     1984
                  2    Colorado      742.2       212.3     1985
                  3    Colorado      731.7       209.7     1986
                  4    Colorado      789.2       155.9     1987
                  5    Vermont       623.4       566.1     1984
                  6    Vermont       533.1       455.9     1985
                  7    Vermont       544.2       566.7     1986
                  8    Vermont       322.3       423.8     1987
                  9    Alaska         51.0        80.7     1984
                 10    Alaska         58.7        78.3     1985
                 11    Alaska         59.8        70.9     1986
                 12    Alaska         64.3        64.6     1987
                 13    Alabama       256.3       480.5     1984
                 14    Alabama       291.5       454.3     1985
                 15    Alabama       303.6       454.7     1986
                                 The SAS System                                2

                                   morning_    evening_
                 Obs     state      copies      copies     year

                  16    Alabama         .        454.5     1987
                  17    Maine           .           .      1984
                  18    Maine           .         68.0     1985
                  19    Maine        222.7        68.6     1986
                  20    Maine        224.1        66.7     1987
                  21    Hawaii       433.5       122.3     1984
                  22    Hawaii       455.6       245.1     1985
                  23    Hawaii       499.3       355.2     1986
                  24    Hawaii       503.2       488.6     1987

Writing Centered Title and Column Headings

Producing centered titles with TITLE statements is easy, because centering is the default for the TITLE statement. Producing column headings is not so easy. You must insert the correct number of blanks in the TITLE statements so that the entire title, when centered, causes the text to fall in the correct columns. The following example shows how to write centered lines and column headings. The titles and column headings appear at the top of every page of output.

options linesize=80 pagesize=20 nodate;

data report1;
   infile 'your-data-file';
   input state $ morning_copies evening_copies year;
run;

title 'Morning and Evening Newspaper Circulation';
title2;
title3
      'State          Year                     Thousands of Copies';
title4
      '                                          Morning      Evening';

data _null_;
   set report1;
   by state notsorted;
   file print;
   if first.state then
      do;
         morning_total=0;
         evening_total=0;
         put / @7 state @;
      end;
   put @26 year @53 morning_copies 5.1 @66 evening_copies 5.1;
   morning_total+morning_copies;
   evening_total+evening_copies;
   if last.state then
      do;
         all_totals=morning_total+evening_total;
         put @52 '------' @65 '------' /
             @26 'Total for each category'
             @52 morning_total 6.1 @65 evening_total 6.1 /
             @35 'Combined total' @59 all_totals 6.1;
      end;
run;

The following output shows the results:

Centered Lines and Column Headings in SAS Output

                   Morning and Evening Newspaper Circulation                   1

          State          Year                     Thousands of Copies
                                                   Morning      Evening

      Colorado           1984                       738.6        210.2
                         1985                       742.2        212.3
                         1986                       731.7        209.7
                         1987                       789.2        155.9
                                                   ------       ------
                         Total for each category   3001.7        788.1
                                  Combined total          3789.8

      Vermont            1984                       623.4        566.1
                         1985                       533.1        455.9
                         1986                       544.2        566.7
                         1987                       322.3        423.8
                                                   ------       ------
                         Total for each category   2023.0       2012.5
                                  Combined total          4035.5
                   Morning and Evening Newspaper Circulation                   2

          State          Year                     Thousands of Copies
                                                   Morning      Evening

      Alaska             1984                        51.0         80.7
                         1985                        58.7         78.3
                         1986                        59.8         70.9
                         1987                        64.3         64.6
                                                   ------       ------
                         Total for each category    233.8        294.5
                                  Combined total           528.3

      Alabama            1984                       256.3        480.5
                         1985                       291.5        454.3
                         1986                       303.6        454.7
                         1987                          .         454.5
                                                   ------       ------
                         Total for each category    851.4       1844.0
                                  Combined total          2695.4
                   Morning and Evening Newspaper Circulation                   3

          State          Year                     Thousands of Copies
                                                   Morning      Evening

      Maine              1984                          .            .
                         1985                          .          68.0
                         1986                       222.7         68.6
                         1987                       224.1         66.7
                                                   ------       ------
                         Total for each category    446.8        203.3
                                  Combined total           650.1

      Hawaii             1984                       433.5        122.3
                         1985                       455.6        245.1
                         1986                       499.3        355.2
                         1987                       503.2        488.6
                                                   ------       ------
                         Total for each category   1891.6       1211.2
                                  Combined total          3102.8

When you create titles and column headings with TITLE statements, consider the following:


Writing Titles and Column Headings in Specific Columns

The easiest way to program headings in specific columns is to use a PUT statement. Instead of calculating the exact number of blanks that are required to make text fall in particular columns, you move the pointer to the appropriate column with pointer controls and write the text. To write headings with a PUT statement, you must execute the PUT statement at the beginning of each page, regardless of the observation that is being processed or the iteration of the DATA step. The FILE statement with the HEADER= option specifies the headings you want to write.

Use the following form of the FILE statement to specify column headings.

FILE PRINT HEADER=label;

PRINT is a reserved fileref that directs output that is produced by any PUT statements to the same print file as the output that is produced by SAS procedures. The label variable defines a statement label that identifies a group of SAS statements that execute each time SAS begins a new output page.

The following program uses the HEADER= option of the FILE statement to add a header routine to the DATA step. The routine uses pointer controls in the PUT statement to write the title, skip two lines, and then write column headings in specific locations.

options linesize=80 pagesize=24;

data _null_;
   set circulation_figures;
   by state notsorted;
   file print notitles header=pagetop; 1 
   if first.state then 
         do;
            morning_total=0;
            evening_total=0;
            put / @7 state @; 
         end;
   put @26 year @53 morning_copies 5.1 @66 evening_copies 5.1;
   morning_total+morning_copies;
   evening_total+evening_copies;
   if last.state then 
      do;
         all_totals=morning_total+evening_total;
         put @52 '------' @65 '------' / 
             @26 'Total for each category'
             @52 morning_total 6.1 @65 evening_total 6.1 /
             @35 'Combined total' @59 all_totals 6.1;
      end;
   return; 2 
   pagetop: 3 
      put @16 'Morning and Evening Newspaper Circulation' // 
          @7 'State' @26 'Year' @51 'Thousands of Copies'/
          @51 'Morning      Evening';
   return; 4 
   run;

The following list corresponds to the numbered items in the preceding program:

[1] The PRINT fileref in the FILE statement creates Listing output. The NOTITLES option eliminates title lines so that the lines can be used by the PUT statement. The HEADER= option defines a statement label that points to a group of SAS statements that executes each time SAS begins a new output page. (You can use the HEADER= option only for creating print files.)

[2] The RETURN statement that is located before the header routine marks the end of the main part of the DATA step. It causes execution to return to the beginning of the step for another iteration. Without this return statement, the statements in the header routine would be executed during each iteration of the DATA step, as well as at the beginning of each page.

[3] The pagetop: label identifies the header routine. Each time SAS begins a new page, execution moves from its current position to the label pagetop: and continues until SAS encounters the RETURN statement. When execution reaches the RETURN statement at the end of the header routine, execution returns to the statement that was being executed when SAS began a new page.

[4] The RETURN statement ends the header routine. Execution returns to the statement that was being executed when SAS began a new page.

The following output shows the results:

Title and Column Headings in Specific Locations

               Morning and Evening Newspaper Circulation

      State              Year                     Thousands of Copies
                                                  Morning      Evening

      Colorado           1984                       738.6        210.2
                         1985                       742.2        212.3
                         1986                       731.7        209.7
                         1987                       789.2        155.9
                                                   ------       ------
                         Total for each category   3001.7        788.1
                                  Combined total          3789.8

      Vermont            1984                       623.4        566.1
                         1985                       533.1        455.9
                         1986                       544.2        566.7
                         1987                       322.3        423.8
                                                   ------       ------
                         Total for each category   2023.0       2012.5
                                  Combined total          4035.5

      Alaska             1984                        51.0         80.7
                         1985                        58.7         78.3
                         1986                        59.8         70.9
               Morning and Evening Newspaper Circulation

      State              Year                     Thousands of Copies
                                                  Morning      Evening
                         1987                        64.3         64.6
                                                   ------       ------
                         Total for each category    233.8        294.5
                                  Combined total           528.3

      Alabama            1984                       256.3        480.5
                         1985                       291.5        454.3
                         1986                       303.6        454.7
                         1987                          .         454.5
                                                   ------       ------
                         Total for each category    851.4       1844.0
                                  Combined total          2695.4

      Maine              1984                          .            .
                         1985                          .          68.0
                         1986                       222.7         68.6
                         1987                       224.1         66.7
                                                   ------       ------
                         Total for each category    446.8        203.3
                                  Combined total           650.1

Changing a Portion of a Heading

You can use variable values to create headings that change on every page. For example, if you eliminate the default page numbers in the procedure output file, you can create your own page numbers as part of the heading. You can also write the numbers differently from the default method. For example, you can write "Page 1" rather than "1." Page numbers are an example of a heading that changes with each new page.

The following program creates page numbers using a Sum statement and writes the numbers as part of the header routine.

options linesize=80 pagesize=24;

data _null_;
   set circulation_figures;
   by state notsorted;
   file print notitles header=pagetop;
   if first.state then
         do;
            morning_total=0;
            evening_total=0;
            put / @7 state @;
         end;
   put @26 year @53 morning_copies 5.1 @66 evening_copies 5.1;
   morning_total+morning_copies;
   evening_total+evening_copies;
   if last.state then
      do;
         all_totals=morning_total+evening_total;
         put @52 '------' @65 '------' /
             @26 'Total for each category'
             @52 morning_total 6.1 @65 evening_total 6.1 /
             @35 'Combined total' @59 all_totals 6.1;
      end;
   return;

   pagetop:
      pagenum+1; 1 
      put @16 'Morning and Evening Newspaper Circulation'
              @67 'Page ' pagenum // 2 
          @7 'State' @26 'Year' @51 'Thousands of Copies'/
          @51 'Morning      Evening';
   return; 
run;

The following list corresponds to the numbered items in the preceding program:

[1] In this Sum statement, SAS adds the value 1 to the accumulator variable PAGENUM each time a new page begins.

[2] The literal Page and the current page number print at the top of each new page.

The following output shows the results:

Changing a Portion of a Heading

               Morning and Evening Newspaper Circulation          Page 1

      State              Year                     Thousands of Copies
                                                  Morning      Evening

      Colorado           1984                       738.6        210.2
                         1985                       742.2        212.3
                         1986                       731.7        209.7
                         1987                       789.2        155.9
                                                   ------       ------
                         Total for each category   3001.7        788.1
                                  Combined total          3789.8

      Vermont            1984                       623.4        566.1
                         1985                       533.1        455.9
                         1986                       544.2        566.7
                         1987                       322.3        423.8
                                                   ------       ------
                         Total for each category   2023.0       2012.5
                                  Combined total          4035.5

      Alaska             1984                        51.0         80.7
                         1985                        58.7         78.3
                         1986                        59.8         70.9
               Morning and Evening Newspaper Circulation          Page 2

      State              Year                     Thousands of Copies
                                                  Morning      Evening
                         1987                        64.3         64.6
                                                   ------       ------
                         Total for each category    233.8        294.5
                                  Combined total           528.3

      Alabama            1984                       256.3        480.5
                         1985                       291.5        454.3
                         1986                       303.6        454.7
                         1987                          .         454.5
                                                   ------       ------
                         Total for each category    851.4       1844.0
                                  Combined total          2695.4

      Maine              1984                          .            .
                         1985                          .          68.0
                         1986                       222.7         68.6
                         1987                       224.1         66.7
                                                   ------       ------
                         Total for each category    446.8        203.3
                                  Combined total           650.1

Controlling Page Divisions

The report in Changing a Portion of a Heading automatically split the data for Alaska over two pages. To make attractive page divisions, you need to know that there is sufficient space on a page to print all the data for a particular state before you print any data for it.

First, you must know how many lines are needed to print a group of data. Then you use the LINESLEFT= option in the FILE statement to create a variable whose value is the number of lines remaining on the current page. Before you begin writing a group of data, compare the number of lines that you need to the value of that variable. If more lines are required than are available, use the _PAGE_ pointer control to advance the pointer to the first line of a new page.

In your report, the maximum number of lines that you need for any state is eight (four years of circulation data for each state plus four lines for the underline, the totals, and the blank line between states). The following program creates a variable named CKLINES and compares its value to eight at the beginning of each BY group. If the value is less than eight, SAS begins a new page before writing that state.

options pagesize=24;

data _null_;
   set circulation_figures;
   by state notsorted;
   file print notitles header=pagetop linesleft=cklines;
   if first.state then
         do;
            morning_total=0;
            evening_total=0;
            if cklines<8 then put _page_;
            put / @7 state @;
         end;
   put @26 year @53 morning_copies 5.1 @66 evening_copies 5.1;
   morning_total+morning_copies;
   evening_total+evening_copies;
   if last.state then
      do;
         all_totals=morning_total+evening_total;
         put @52 '------' @65 '------' /
             @26 'Total for each category'
             @52 morning_total 6.1 @65 evening_total 6.1 /
             @35 'Combined total' @59 all_totals 6.1;
      end;
   return;

   pagetop:
      pagenum+1;
      put @16 'Morning and Evening Newspaper Circulation'
              @67 'Page ' pagenum //
          @7 'State' @26 'Year' @51 'Thousands of Copies'/
          @51 'Morning      Evening';
   return; 
run;

The following output shows the results:

Output with Specific Page Divisions

               Morning and Evening Newspaper Circulation          Page 1

      State              Year                     Thousands of Copies
                                                  Morning      Evening

      Colorado           1984                       738.6        210.2
                         1985                       742.2        212.3
                         1986                       731.7        209.7
                         1987                       789.2        155.9
                                                   ------       ------
                         Total for each category   3001.7        788.1
                                  Combined total          3789.8

      Vermont            1984                       623.4        566.1
                         1985                       533.1        455.9
                         1986                       544.2        566.7
                         1987                       322.3        423.8
                                                   ------       ------
                         Total for each category   2023.0       2012.5
                                  Combined total          4035.5
               Morning and Evening Newspaper Circulation          Page 2

      State              Year                     Thousands of Copies
                                                  Morning      Evening

      Alaska             1984                        51.0         80.7
                         1985                        58.7         78.3
                         1986                        59.8         70.9
                         1987                        64.3         64.6
                                                   ------       ------
                         Total for each category    233.8        294.5
                                  Combined total           528.3

      Alabama            1984                       256.3        480.5
                         1985                       291.5        454.3
                         1986                       303.6        454.7
                         1987                          .         454.5
                                                   ------       ------
                         Total for each category    851.4       1844.0
                                  Combined total          2695.4
               Morning and Evening Newspaper Circulation          Page 3

      State              Year                     Thousands of Copies
                                                  Morning      Evening

      Maine              1984                          .            .
                         1985                          .          68.0
                         1986                       222.7         68.6
                         1987                       224.1         66.7
                                                   ------       ------
                         Total for each category    446.8        203.3
                                  Combined total           650.1

Previous Page | Next Page | Top of Page