PRINT Procedure

Example 4: Summing Numeric Variables with One BY Group

Features:
PROC PRINT statement options::
N=
SUMLABEL

BY statement

SUM statement

Other features:

ODS CSVALL statement

SORT procedure

TITLE statement : #BYVAL specification

SAS system options: :
BYLINE
NOBYLINE
Data set: EXPREV

Details

This example demonstrates the following tasks:
  • sums expenses and revenues for each region and for all regions.
  • shows the number of observations in each BY group and in the whole report.
  • creates a customized title, containing the name of the region. This title replaces the default BY line for each BY group.
  • creates a default HTML file.
  • creates a CSV file.

Program: Creating an HTML Report

options nodate pageno=1 linesize=80 pagesize=40 obs=10 nobyline;
proc sort data=exprev;
   by sale_type;
run;
proc print data=exprev noobs label sumlabel
           n='Number of observations for the order type: '
           'Number of observations for the data set: ';
   var country order_date quantity price;
   label  sale_type='Sale Type'
          price='Total Retail Price* in USD'
          country='Country' order_date='Date' quantity='Quantity';
   sum price quantity;
   by sale_type;
   format price dollar7.2;
   title 'Retail and Quantity Totals for #byval(sale_type) Sales';
run;
options byline;

Program Description

The HTML destination is open by default. This program uses the default filename for the HTML output. No ODS HTML statement is needed.
Start each BY group on a new page and suppress the printing of the default BY line. The SAS system option NOBYLINE suppresses the printing of the default BY line. When you use PROC PRINT with the NOBYLINE option, each BY group starts on a new page. 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 obs=10 nobyline;
Sort the data set. PROC SORT sorts the observations by Sale_Type.
proc sort data=exprev;
   by sale_type;
run;
Print the report, suppress the printing of observation numbers, and print the total number of observations for the selected variables. NOOBS suppresses the printing of observation numbers at the beginning of the rows. SUMLABEL prints the BY variable label on the summary line of each. N= prints the number of observations in a BY group at the end of that BY group and (because of the SUM statement) prints the number of observations in the data set at the end of the report. The first piece of explanatory text that N= provides precedes the number for each BY group. The second piece of explanatory text that N= provides precedes the number for the entire data set.
proc print data=exprev noobs label sumlabel
           n='Number of observations for the order type: '
           'Number of observations for the data set: ';
Select the variables to include in the report. The VAR statement creates columns for Country, Order_Date, Quantity, and Price, in that order.
   var country order_date quantity price;
Assign the variables' labels as column headings. The LABEL statement associates a label with each variable for the duration of the PROC PRINT step.
   label  sale_type='Sale Type'
          price='Total Retail Price* in USD'
          country='Country' order_date='Date' quantity='Quantity';
Sum the values for the selected variables. The SUM statement alone sums the values of Price and Quantity for the entire data set. Because the PROC PRINT step contains a BY statement, the SUM statement also sums the values of Price and Quantity for each sale type that contains more than one observation.
   sum price quantity;
   by sale_type;
Format the numeric values for a specified column. The FORMAT statement assigns the DOLLAR10.2. format to Price for this report.
   format price dollar7.2;
Specify and format a dynamic (or current) title. The TITLE statement specifies a title. The #BYVAL specification places the current value of the BY variable Sale_Type in the title. Because NOBYLINE is in effect, each BY group starts on a new page, and the title serves as a BY line.
   title 'Retail and Quantity Totals for #byval(sale_type) Sales';
run;
Generate the default BY line. The SAS system option BYLINE resets the printing of the default BY line.
options byline;

Output: HTML

Summing Numeric Variables with One BY Group HTML Output
Summing Numeric Variables with One BY Group HTML Output, First HTML Output
Summing Numeric Variables with One BY Group HTML Output, Second HTML Output
Summing Numeric Variables with One BY Group HTML Output, Third HTML Output

Program: Creating a CSV File

options nodate pageno=1 obs=10 nobyline;
ods csvall file='your_file.csv';
proc sort data=exprev;
   by sale_type;
run;
proc print data=exprev noobs label sumlabel
           n='Number of observations for the order type: '
           'Number of observations for the data set: ';
var country order_date quantity price;
   label  price='Total Retail Price* in USD'
          country='Country' order_date='Date' quantity='Quantity';
   sum price quantity;
   by sale_type;
   format price dollar7.2;
   title 'Retail and Quantity Totals for #byval(sale_type) Sales';
run;
options byline;
ods csvall close;

Program Description

options nodate pageno=1 obs=10 nobyline;
Produce CSV formatted output and specify the file to store it in. The ODS CSVALL statement opens the CSVALL destination and creates a file containing tabular output with titles, notes, and bylines. The FILE= argument specifies the external file that contains the CSV output.
ods csvall file='your_file.csv';
proc sort data=exprev;
   by sale_type;
run;
proc print data=exprev noobs label sumlabel
           n='Number of observations for the order type: '
           'Number of observations for the data set: ';
var country order_date quantity price;
   label  price='Total Retail Price* in USD'
          country='Country' order_date='Date' quantity='Quantity';
   sum price quantity;
   by sale_type;
   format price dollar7.2;
   title 'Retail and Quantity Totals for #byval(sale_type) Sales';
run;
options byline;
Close the CSVALL destination. The ODS CSVALL CLOSE statement closes the CSVALL destination.
ods csvall close;

Output: CSV File

Summing Numeric Variables with One BY Group: CSV Output Viewed with Microsoft Excel
Summing Numeric Variables with One BY Group: CSV Output Viewed with Microsoft Excel