PRINT Procedure

Example 5: Summing Numeric Variables with Multiple BY Variables

Features:
PROC PRINT statement options: :
N=
NOOBS
STYLE
SUMLABEL

BY statement

SUM statement

Other features:

ODS HTML statement

LABEL statement

FORMAT statement

SORT procedure

TITLE statement

Data set: EXPREV

Details

This example demonstrates the following tasks:
  • sums quantities and retail prices for the following items:
    • each order date
    • each sale type with more than one row in the report
    • all rows in the report
  • shows the number of observations in each BY group and in the whole report
  • displays the BY group label in place of the BY group variable name on the summary line
  • creates a default HTML report
  • creates a stylized HTML report

Program: Creating an HTML Report

options nodate pageno=1 obs=10;
ods html file='your_file.html';
proc sort data=exprev;
   by sale_type order_date;
run;
proc print data=exprev n noobs sumlabel;
   by sale_type order_date;
   sum price quantity;
   label  sale_type='Sale Type' order_date='Sale Date';
   format price dollar10.2 cost dollar10.2;
   title 'Retail and Quantity Totals for Each Sale Date and Sale Type';
run;

Program Description

options nodate pageno=1 obs=10;
Produce HTML output and specify the file to store the output in.The HTML destination is open by default. The ODS HTML FILE= statement creates a file that contains HTML output. The FILE= argument specifies the external file that contains the HTML output.
ods html file='your_file.html';
proc sort data=exprev;
   by sale_type order_date;
run;
proc print data=exprev n noobs sumlabel;
   by sale_type order_date;
   sum price quantity;
   label  sale_type='Sale Type' order_date='Sale Date';
   format price dollar10.2 cost dollar10.2;
   title 'Retail and Quantity Totals for Each Sale Date and Sale Type';
run;

Output: HTML

Summing Numeric Variables with Multiple BY Variables: In Store Sales: Default HTML Output
Summing Numeric Variables with Multiple BY Variables: In Store Sales: Default HTML Output
Continuation of the Output

Program: Creating an HTML Report with the STYLE Option

options nodate pageno=1 obs=10
ods html file='your_file.html';
proc sort data=exprev;
   by sale_type order_date;
run;
proc print data=exprev n noobs sumlabel;
   by sale_type order_date;
sum price / style(GRANDTOTAL) = [background =white color=blue];
sum quantity / style(TOTAL) = [background =dark blue color=white];
   label  sale_type='Sale Type' order_date='Sale Date';
   format price dollar10.2 cost dollar10.2;
   title 'Retail and Quantity Totals for Each Sale Date and Sale Type';
run;
ods html close;

Program Description

options nodate pageno=1 obs=10
ods html file='your_file.html';
proc sort data=exprev;
   by sale_type order_date;
run;
proc print data=exprev n noobs sumlabel;
Create stylized HTML output. The STYLE option in the first SUM statement specifies that the background color of the cell containing the grand total for the variable Price be changed to white and the font color be changed to blue. The STYLE option in the second SUM statement specifies that the background color of cells containing totals for the variable Quantity be changed to dark blue and the font color be changed to white.
   by sale_type order_date;
sum price / style(GRANDTOTAL) = [background =white color=blue];
sum quantity / style(TOTAL) = [background =dark blue color=white];
   label  sale_type='Sale Type' order_date='Sale Date';
   format price dollar10.2 cost dollar10.2;
   title 'Retail and Quantity Totals for Each Sale Date and Sale Type';
run;
ods html close;

Output: HTML with Styles

Summing Numeric Variables with Multiple BY Variables: Catalog Sales: HTML Output Using Styles
Summing Numeric Variables with Multiple BY Variables: Catalog Sales: HTML Output Using Style
Continuation of output

Program: Creating a Listing Report

options nodate pageno=1 linesize=80 pagesize=40;
ods html close;
ods listing;
proc sort data=exprev;
   by sale_type order_date;
run;
proc print data=exprev n noobs sumlabel;
   by sale_type order_date;
   sum price quantity;
   label  sale_type='Sale Type'
          order_date='Sale Date';
   format price dollar10.2 cost dollar10.2;
   title 'Retail and Quantity Totals for Each Sale Date and Sale Type';
run;
ods listing close;
ods html;

Program Description

Set the SAS system options. The NODATE option suppresses the display of the date and time in the output. The PAGENO= option specifies the starting page number. The LINESIZE= option specifies the output line length, and the PAGESIZE= option specifies the number of lines on an output page.
options nodate pageno=1 linesize=80 pagesize=40;
Close the HTML destination and open the Listing destination.The HTML destination is open by default.
ods html close;
ods listing;
Sort the data set. PROC SORT sorts the observations by Sale_Type and Order_Date.
proc sort data=exprev;
   by sale_type order_date;
run;
Print the report, suppress the printing of observation numbers, print the total number of observations for the selected variables and use the BY variable labels in place of the BY variable names in the summary line. The N option prints the number of observations in a BY group at the end of that BY group and prints the total number of observations used in the report at the bottom of the report. NOOBS suppresses the printing of observation numbers at the beginning of the rows. The SUMLABEL option prints the BY variable labels in the summary line in place of the BY variables.
proc print data=exprev n noobs sumlabel;
Create a separate section of the report for each BY group, and sum the values for the selected variables. The BY statement produces a separate section of the report for each BY group. The SUM statement alone sums the values of Price and Quantity for the entire data set. Because the program contains a BY statement, the SUM statement also sums the values of Price and Quantity for each BY group that contains more than one observation.
   by sale_type order_date;
   sum price quantity;
Establish a label for selected variables, format the values of specified variables, and create a title. The LABEL statement associates a label with the variables Sale_Type and Order_Date for the duration of the PROC PRINT step. The labels are used in the BY line at the beginning of each BY group and in the summary line in place of BY variables. The FORMAT statement assigns a format to the variables Price and Cost for this report. The TITLE statement specifies a title.
   label  sale_type='Sale Type'
          order_date='Sale Date';
   format price dollar10.2 cost dollar10.2;
   title 'Retail and Quantity Totals for Each Sale Date and Sale Type';
run;
Close the Listing destination and re-open the HTML destination.
ods listing close;
ods html;

Output: Listing

The report uses default column headings (variable names) because neither the SPLIT= nor the LABEL option is used. Nevertheless, the BY line at the top of each section of the report shows the BY variables' labels and their values. The BY variables' labels identifies the subtotals in the report summary line.
PROC PRINT sums Price and Quantity for each BY group that contains more than one observation. However, sums are shown only for the BY variables whose values change from one BY group to the next. For example, in the first BY group, where the sale type is Catalog Sale and the sale date is >1/1/12, Quantity and Price are summed only for the sale date because the next BY group is for the same sale type.
         Retail and Quantity Totals for Each Sale Date and Sale Type          1

---------------------- Sale Type=Catalog Sale Date=1/1/12 ----------------------

                              Ship_
   Country         Emp_ID      Date     Quantity         Price          Cost

   Puerto Rico    99999999    1/5/12       14           $51.20        $12.10
   Aruba          99999999    1/4/12       30          $123.70        $59.00
   Bahamas        99999999    1/4/12        8          $113.40        $28.45
   Bermuda        99999999    1/4/12        7           $41.00         $9.25
   -----------                          --------    ----------
   Sale Date                               59          $329.30

                                     N = 4


---------------------- Sale Type=Catalog Sale Date=1/2/12 ----------------------

                                    Ship_
Country                   Emp_ID     Date    Quantity        Price         Cost

British Virgin Islands   99999999   1/5/12       11         $40.20       $20.20
Canada                   99999999   1/5/12      100         $11.80        $5.00
El Salvador              99999999   1/6/12       21        $266.40       $66.70
Brazil                   120127     1/2/12       12         $73.40       $18.45
French Guiana            120935     1/2/12       15         $96.40       $43.85
Grenada                  120931     1/2/12       19         $56.30       $25.05
Paraguay                 120603     1/2/12       17        $117.60       $58.90
Peru                     120845     1/2/12       12         $93.80       $41.75
----------------------                       --------   ----------
Sale Date                                       207        $755.90
Sale Type                                       266      $1,085.20

                                     N = 8
          Retail and Quantity Totals for Each Sale Date and Sale Type          2

--------------------- Sale Type=In Store Sale Date=1/1/12 ----------------------

                                    Ship_
        Country           Emp_ID     Date    Quantity        Price         Cost

 Virgin Islands (U.S.)   99999999   1/4/12      25          $31.10       $15.65

                                     N = 1


--------------------- Sale Type=In Store Sale Date=1/2/12 ----------------------

                                   Ship_
 Country                 Emp_ID     Date    Quantity        Price         Cost

 Belize                 120458     1/2/12        2        $146.40       $36.70
 Cayman Islands         120454     1/2/12       20         $71.00       $32.30
 Guatemala              120931     1/2/12       13        $144.40       $65.70
 Jamaica                99999999   1/4/12       23        $169.80       $38.70
 Mexico                 120127     1/2/12       30        $211.80       $33.65
 Montserrat             120127     1/2/12       19        $184.20       $36.90
 Anguilla               99999999   1/6/12       15        $233.50       $22.25
 Antigua/Barbuda        120458     1/2/12       31         $99.60       $45.35
 Argentina              99999999   1/6/12       42        $408.80       $87.15
 Barbados               99999999   1/6/12       26         $94.80       $42.60
 Bolivia                120127     1/2/12       26         $66.00       $16.60
 Chile                  120447     1/2/12       20         $19.10        $8.75
 Ecuador                121042     1/2/12       11        $100.90       $50.55
 Falkland Islands       120932     1/2/12       15         $61.40       $30.80
 Guyana                 120455     1/2/12       25        $132.80       $30.25
 Martinique             120841     1/3/12       16         $56.30       $31.05
 Netherlands Antilles   99999999   1/6/12       31         $41.80       $19.45
 --------------------                       --------   ----------
 Sale Date                                     365      $2,242.60
 Sale Type                                     390      $2,273.70

                                     N = 17
          Retail and Quantity Totals for Each Sale Date and Sale Type          3

--------------------- Sale Type=Internet Sale Date=1/1/12 ----------------------

                              Ship_
     Country       Emp_ID      Date     Quantity         Price          Cost

    Antarctica    99999999    1/7/12        2           $92.60        $20.70

                                     N = 1


--------------------- Sale Type=Internet Sale Date=1/2/12 ----------------------

                                    Ship_
 Country                 Emp_ID     Date     Quantity        Price         Cost

 Costa Rica             99999999   1/6/12        31         $53.00       $26.60
 Cuba                   121044     1/2/12        12         $42.40       $19.35
 Dominican Republic     121040     1/2/12        13         $48.00       $23.95
 Haiti                  121059     1/2/12         5         $47.90       $23.45
 Honduras               120455     1/2/12        20         $66.40       $30.25
 Nicaragua              120932     1/2/12        16        $122.00       $28.75
 Panama                 99999999   1/6/12        20         $88.20       $38.40
 Saint Kitts/Nevis      99999999   1/6/12        20         $41.40       $18.00
 St. Helena             120360     1/2/12        19         $94.70       $47.45
 St. Pierre/Miquelon    120842     1/16/12       16        $103.80       $47.25
 Turks/Caicos Islands   120372     1/2/12        10         $57.70       $28.95
 United States          120372     1/2/12        20         $88.20       $38.40
 Colombia               121059     1/2/12        28        $361.40       $90.45
 Dominica               121043     1/2/12        35        $121.30       $57.80
 Guadeloupe             120445     1/2/12        21        $231.60       $48.70
 St. Lucia              120845     1/2/12        19         $64.30       $28.65
 --------------------                        --------   ----------
 Sale Date                                      305      $1,632.30

                                     N = 16
          Retail and Quantity Totals for Each Sale Date and Sale Type          4

--------------------- Sale Type=Internet Sale Date=1/3/12 ----------------------

                            Ship_
      Country     Emp_ID     Date     Quantity         Price          Cost

     Suriname     120538    1/3/12        22         $110.80        $29.35
     ---------                        --------    ----------
     Sale Type                           329       $1,835.70
                                      ========    ==========
                                         985       $5,194.60

                                    N = 1
                              Total N = 48