Example 2.5. Summarizing Columns and Rows

Goal

List observations in a data set, one observation per row, and summarize selected numeric variables. Summarize over all observations and summarize observations within groups. Summarize specific variables within an observation and add these summaries as columns in the report.

Report

												   Regional Energy
                                 Quarterly Use by
 Residential and Commercial Customers

                                 ------------First
 Quarter-------------  ------------Second
 Quarter------------
                                                  
   Quarter                                 Quarter
              Service           January  February 
    March     Total     April       May      June  
   Total      Total
 Commercial   Area Lights         6,526    11,999 
   17,533    36,058    10,221    17,218     8,857  
  36,296     72,354
              Flood Lights       10,911    12,648 
   15,502    39,061     9,120     8,624    18,338  
  36,082     75,143
              General Service     1,203       641 
      728     2,572     1,039     1,156       782  
   2,977      5,549
              Off Peak           15,062    15,635 
    9,509    40,206    11,717    11,456    12,461  
  35,634     75,840
              Other Service       1,390     1,672 
    1,638     4,700     1,282     1,654     1,915  
   4,851      9,551
              Space Heating         111        85 
      121       317       109       125       103  
     337        654
              Water Heating         160       168 
      130       458       187       101       101  
     389        847
 ===========                    ========  ========
  ========  ========  ========  ========  ======== 
 ========  ========
 Commercial                      35,363    42,848 
   45,161   123,372    33,675    40,334    42,557  
 116,566    239,938
 ===========                    ========  ========
  ========  ========  ========  ========  ======== 
 ========  ========

 Residential  Area Lights           118       116 
       91       325        92        95       137  
     324        649
              Flood Lights           96        89 
       75       260        87        75        82  
     244        504
              General Service    22,281    21,505 
   22,556    66,342    22,784    25,977    25,371  
  74,132    140,474
              Off Peak            1,152     1,362 
      698     3,212     1,047       534     1,492  
   3,073      6,285
              Other Service         286       238 
      109       633        33       158       465  
     656      1,289
              Space Heating       8,280    10,984 
   10,111    29,375    13,234    13,723    11,072  
  38,029     67,404
              Water Heating       9,589    10,625 
   14,160    34,374    18,130     8,592     7,654  
  34,376     68,750
 ===========                    ========  ========
  ========  ========  ========  ========  ======== 
 ========  ========
 Residential                     41,802    44,919 
   47,800   134,521    55,407    49,154    46,273  
 150,834    285,355
 ===========                    ========  ========
  ========  ========  ========  ========  ======== 
 ========  ========

                                ========  ========
  ========  ========  ========  ========  ======== 
 ========  ========
                                 77,165    87,767 
   92,961   257,893    89,082    89,488    88,830  
 267,400    525,293
                                ========  ========
  ========  ========  ========  ========  ======== 
 ========  ========


Example Features

Data SetPOWERUSE
Featured StepPROC REPORT
Featured Step Statements and OptionsDEFINE statement: ANALYSIS, COMPUTED, ORDER, and SUM options

COMPUTE blocks to create new variables

BREAK AFTER statement: SUMMARIZE option

RBREAK AFTER statement: SUMMARIZE option
Formatting FeaturesPlacing a heading over the headings of several variables

Padding column headings with a character when sending output to the LISTING destination

DEFINE statement: WIDTH option when sending output to the LISTING destination.

Controlling line breaks in column headings

BREAK and RBREAK statements: DOL, DUL, and SKIP options when sending output to the LISTING destination
A Closer LookWriting COMPUTE Blocks

Referencing Report Items in COMPUTE Blocks

Processing Missing Values in COMPUTE Blocks

Understanding How PROC REPORT Processes COMPUTE Blocks
Other Examples That Use This Data SetExamples 2.4, 2.6, and 6.10

Example Overview

Example 2.4 presents monthly power usage by type of service for commercial and residential customers from January to June. This report includes the same information as Example 2.4 plus three new columns: two that summarize power usage for each of the two quarters represented in the report and one that summarizes power usage over the six-month reporting period. To produce these new summary columns, the program creates temporary report variables during execution of the report step.

Each detail row in the report—those with a value for SERVICE—corresponds to one observation in the POWERUSE data set. The other rows, which are summaries of the detail rows, were created by the PROC REPORT statements BREAK and RBREAK.

All the report columns except for the quarterly totals and overall total correspond to one variable in the POWERUSE data set. PROC REPORT defines the columns containing the totals. Statements in COMPUTE blocks in PROC REPORT calculate the totals by summing variables in the POWERUSE data set.

Program

Create formats for TYPE and SERVICE.
proc format;
  value $type    'res'='Residential'
                 'com'='Commercial';
  value $service 'gen'='General Service'
                 'wtr'='Water Heating'
                 'op' ='Off Peak'
                 'spc'='Space Heating'
                 'fld'='Flood Lights'
                 'area'='Area Lights'
                 'oth'='Other Service';
run;

options ls=120 ps=45;

title 'Regional Energy';
title2 'Quarterly Use by Residential and Commercial
        Customers';

proc report data=poweruse nowindows
     split='/';

Specify the split character to control line breaks in column headings. 
List the columns in the order in which they should appear in the report. Include the computed variable in the
  column type service

list. Ensure that the columns used to calculate a computed variable precede the computed variable. 
Place a column heading above a group of variables. Enclose within parentheses the quoted column heading and the list of variables that the heading text should be placed above. If sending the output to the LISTING destination, pad the heading to the left and to the right of the text with dashes. (Remove the pad character when sending output to a nonlisting destination.)
       ('-First Quarter-' jan feb mar quarter1)
       ('-Second Quarter-' apr may jun quarter2)
       total;

Order the detail rows according to the ascending, formatted values of the order variables. Establish the order of the detail rows by sorting the order variables from left to right as specified by their position in the COLUMN statement.
  define type    / order format=$type. width=11 ' ';
  define service / order format=$service. width=15
                  'Service';

Enhance the report. For each column variable: assign a format; specify a column width (LISTING destination only); and specify a column header. Enclose a blank within single quotation marks to suppress a column header. 
Calculate the SUM statistic for each of the analysis variables.
  define jan     / analysis sum 'January' width=8
                   format=comma8.;
  define feb     / analysis sum 'February' width=8
                   format=comma8.;
  define mar     / analysis sum 'March' width=8
                   format=comma8.;
  define apr     / analysis sum 'April' width=8
                   format=comma8.;
  define may     / analysis sum 'May' width=8
                   format=comma8.;
  define jun     / analysis sum 'June' width=8
                   format=comma8.;

Identify the computed columns. Split the column headers for QUARTER1 and QUARTER2 over two lines.
  define quarter1 / computed 'Quarter/Total' width=8
                    format=comma8.;
  define quarter2 / computed 'Quarter/Total' width=8
                    format=comma8.;
  define total    / computed 'Total' width=8
                    format=comma8.;

Calculate the values of each computed variable. Define a COMPUTE block for each variable.
  compute quarter1;

Sum specific analysis variables. Represent the analysis variables with a compound name. Use the SUM function so that the computed variable is equal to the sum of the nonmissing arguments; this prevents generation of missing values when at least one of the arguments is nonmissing.
    quarter1=sum(jan.sum,feb.sum,mar.sum);
  endcomp;
  compute quarter2;
    quarter2=sum(apr.sum,may.sum,jun.sum);
  endcomp;

  compute total;

Compute the value of the new report variable by summing the values of two computed variables. Do not specify the arguments to the SUM function with compound names since they are not analysis variables.
    total=sum(quarter1,quarter2);
  endcomp;

Summarize the analysis variables and computed variables over all observations with the same value of TYPE. Write the summaries on the line after the last row for each value of TYPE. When sending output to the LISTING destination, place a double overline and a double underline above and below the summary row, and skip a line after the double underline.
  break after type / summarize dol dul skip;

Sum the analysis variables and computed variables over all observations. Write the summary at the end of the report. When sending output to the LISTING destination, place a double overline and a double underline above and below the summary row, and skip a line after the double underline.
  rbreak after / summarize dol dul skip;
run;


A Closer Look

Writing COMPUTE Blocks

A COMPUTE block is a section of code that contains one or more SAS language programming statements. In the nonwindowing PROC REPORT environment, the COMPUTE and ENDCOMP statements enclose the code. In the windowing PROC REPORT environment, you enter the programming statements into the COMPUTE window.

A COMPUTE block can reference either a report item or a location. Report items include data set variables, statistics, and computed variables. The locations that a COMPUTE block can reference are the top of a report; the bottom of a report; before a group of observations; and after a group of observations. The second usage is usually associated with break lines and enables you to customize break lines.

You can have more than one COMPUTE block per PROC REPORT step, as shown in the previous example, but you cannot nest the COMPUTE blocks.

COMPUTE blocks can incorporate most features of the SAS language, including the following items.

DM statement
%INCLUDE statement
DATA step statements:
  ARRAYIF-THEN/ELSE
  AssignmentLENGTH
  CALLRETURN
  DO (all forms)SELECT
  ENDSum
comments
null statements
macro variables and macro invocations
all DATA step functions.

Referencing Report Items in COMPUTE Blocks

The way you reference a report item in a COMPUTE block depends on how you’ve defined its usage. You can reference a report item in four ways:

by name
by a compound name that identifies both the variable and the name of the statistic that you calculate with it. A compound name has this form
   variable-name.statistic

by an alias that you create in the COLUMN statement or in the DEFINITION window
by column number, in the form

   '_Cn_'

where n is the number of the column (from left to right) in the report

When you reference columns by number, remember to include in your count the columns that you define with NOPRINT and NOZERO, even though these columns do not appear in your report.

Table 2.5a shows how to use each type of reference in a COMPUTE block.

Table 2.5a. Referencing Variables in COMPUTE Blocks
If the variable that you reference is this type...Then refer to it by...For example...
GroupName [*]department
OrderName [*]department
ComputedName [*]department
DisplayName [*]department
Display sharing a column with a statisticA compound name [*]sales.sum
AnalysisA compound name [*]sales.mean
Any type sharing a column with an across variableColumn number [**]‘_c3_’

[*] If the variable has an alias, then you must reference it with the alias.

[**] Even if the variable has an alias, you must reference it by column number.

Processing Missing Values in COMPUTE Blocks

As in DATA step programming, when you reference in a COMPUTE block a variable that has a missing value, the result is a missing value. PROC REPORT displays the current missing value designation, which by default is a blank for character variables and a period for numeric variables. Where appropriate, consider using functions in COMPUTE blocks to prevent the generation of missing values when at least one of the arguments is nonmissing. The program above uses the SUM function for this reason.

Understanding How PROC REPORT Processes COMPUTE Blocks

You can associate a COMPUTE block with a report item or with a location. The way PROC REPORT processes a COMPUTE block depends on its usage.

When a COMPUTE block is associated with a report item, PROC REPORT executes the COMPUTE block on every row of the report when it comes to the column for that report item. The value of a computed variable in any row of a report is the last value assigned to that variable during that execution of the programming statements in the COMPUTE block. PROC REPORT assigns values to the columns in a row of a report from left to right. Consequently, you cannot base the calculation of a computed variable on any variable that appears to its right in the report.

When a COMPUTE block is associated with a location, PROC REPORT executes the COMPUTE block only at that location. Because PROC REPORT calculates statistics for groups before it actually constructs the rows of the report, statistics for sets of detail rows are available before or after the rows are displayed, as are values for any variables based on these statistics.

Table 2.5b describes the processing of both types of COMPUTE blocks in a PROC REPORT step.

Table 2.5b. How PROC REPORT Processes COMPUTE Blocks


Where to Go from Here

PROC REPORT reference, usage information, and additional examples. See, “The REPORT Procedure” in the “Procedures” section of Base SAS 9.1 Procedures Guide.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.91.153