E. Report-Writer Report Examples : POP2 Example
 
Share this page                  
POP2 Example
The POP2 example shows an alternative set of formatting statements for producing the same output as POPULATION. This report makes use of the .block and .endblock statements, as well as the .within and .endwithin statements, in producing the report. These statements are useful for reports which contain several columns for which the same set of statements is repeated as is the case with the tot, tot_18to65, tot_under18, and tot_over65 columns in POPULATION.
All of the statements in POP2 are identical to the statements in POPULATION with the exception of those in the .footer region and .footer report sections. In these sections, instead of spelling out the format of the subtotals line by line, the block and column formatting statements can be used to duplicate the same set of statements for each of several columns.
In detail, the statements are:
The .block statement sets the Report-Writer into block mode, which allows you to write a two‑dimensional block of text, in which you can write text on several lines, return to the first line in the block, and then write more text on the first lines in the block.
The .within statement sets the Report-Writer into column formatting mode. Because the statement is followed by four column names (tot, tot_18to65, tot_under18, and tot_over65), Report-Writer executes all statements between the .within and its corresponding .endwithin statement four times, using the margins for each of the columns in turn.
The string of hyphens ("----------") prints, right justified, within each of the four columns in the first line of the block. Because this is a .printline statement, the current output line moves down one line in the block after the string prints. A sum prints on the second line of the block, right justified within each of the columns. Because this sum uses the special name w_column, Report-Writer calculates and prints a separate sum for each of the columns in turn.
The .endwithin statement ends the set of formatting statements to be done within each column. Because block mode is in effect, a .top statement automatically executes immediately before the .endwithin statement. This ensures that the statements for each of the columns prints across the page, rather than stair‑stepping down the page.
Immediately following the .endwithin statement is the .top statement, which moves the current output line back to the first line in the current block (which contains all of the strings of hyphens ("-----------"). The .newline statement moves the current position to the second line in the block (as block mode is still in effect), and Report-Writer prints the Totals: region string. The .endblock statement causes Report-Writer to print the block, consisting of two lines, and to leave block mode. The last .newline statement inserts a blank line.
The statements within the .footer section are identical, except for the string, USA Totals. Because of the context, "sum(w_column)" refers to totals for the entire report, rather than for the region.
The statements in this example are not quite as intuitive as those in the POPULATION report specification, but they show an important capability for formatting reports with column‑oriented statements.
/* POP2 - Population Report using .WITHIN */
.NAME pop2
.OUTPUT pop2.out
.LONGREMARK
POP2 shows an alternative set of formatting statements
(.BLOCK and .ENDBLOCK and .WITHIN and .ENDWITHIN)
for producing the same output as the POPULATION report. The same set of
statements is repeated for the columns: totpop, tot_18to65, tot_under18, 
tot_over65. All statements in POP2 are the same as POPULATION with the 
exception of those in the ".FOOT region" and ".FOOT report" sections.
.ENDREMARK
.QUERY
      select region.region, state.state,
          pop.tot_18to65 + pop.tot_under18 + pop.tot_over65 as tot,
              pop.tot_18to65, pop.tot_under18, pop.tot_over65
      from region, state, pop
      where state.statabbrev = pop.statabbrev
          and state.regabbrev = region.regabbrev
          and pop.year = $year
.SORT region, state
.DECLARE year = varchar(4) with prompt 'Enter Year:'
.FORMAT tot, tot_18to65, tot_under18, tot_over65 (' zzz,zzz,zzz')
.FORMFEEDS
.HEADER report
      .NEWLINE 3
      .UL .CE .PR 'Population of the United States, by Age Group' .NOU
      .NEWLINE .CE .PR 'Data for the Year - ', $year(c4) .NL 2
      .U .RT tot .PR 'Total Pop' .RT tot_18to65 .PR '18 to 65'
          .RT tot_under18 .PR 'Under 18' .RT tot_over65 .P 'Over 65'
      .NOU .NL 2
.HEADER region
  .NEED 4
  .PR 'Region: ', region .NL
.DETAIL
      .NEED 2 .T5
      .PR state(c20) .T+11 .PR tot, tot_18to65, tot_under18, tot_over65
      .NL
.FOOTER region
      .NEED2
      .BLOCK        .WITHIN tot, tot_18to65, tot_under18, tot_over65
                    .RT .PRINTLINE '----------'
                    .RT .PRINTLN sum(w_column)
              .ENDWITHIN
              .TOP .NEWLINE .PR 'Totals: ', region(c0)
      .ENDBLOCK .NEWLINE
.FOOTER report
      .NEED2
      .BLOCK  .WITHIN tot, tot_18to65, tot_under18, tot_over65
                 .RT .PRINTLINE '-------------'
                 .RT .PRINTLN sum(w_column)
              .END WITHIN
              .TOP .NEWLINE .PR 'USA Totals'
      .ENDBLOCK .NEWLINE
.HEADER page
      .NL 3 .PR 'Population by State and Region: ', $year .NL 2
      .U .RT tot .P 'Total Pop' .RT tot_18to65 .P '18 to 65'
      .RT tot_under18 .P 'Under 18' .RT tot_over65 .P 'Over 65'
      .NOU .NL 2
.FOOTER page
      .NL
      .PR 'Source: US Department of the Interior, Bureau of the
            Census.'
      .RIGHT .PR 'Page', page_number('zN') .NL 4