8.1 Detail Reporting with PROC PRINT
Key Takeaways
- PROC PRINT generates detail listing reports, displaying dataset observations in table format with a default Obs column unless suppressed by the NOOBS option.
- The VAR statement specifies which variables to display and their exact order; omitting VAR causes all dataset variables to print in program data vector order.
- The ID statement replaces the default Obs column with specified variable values as row identifiers, preventing duplicate observation numbering.
- Using a BY statement requires input data to be sorted first by the BY variables, creating distinct subgroup tables, and optionally pairing with PAGEBY for forced page breaks.
- The LABEL option on the PROC PRINT statement instructs SAS to display variable labels as column headers instead of variable names, while SPLIT= prints raw unformatted values.
8.1 Detail Reporting with PROC PRINT
Detail reporting is an essential requirement in data analysis and business reporting. In SAS, PROC PRINT serves as the primary tool for producing listing reports that print observations from a SAS dataset. By default, PROC PRINT displays all observations and all variables in the order they exist within the dataset, accompanied by an observation number column (Obs). However, SAS Base certification exam candidates must master the statements and options that transform default listings into customized, publication-ready reports.
1. Core Architecture and Default Behavior
When executed without additional statements or options, PROC PRINT performs a complete scan of the dataset specified in the DATA= option (or the most recently created dataset if DATA= is omitted) and outputs every row and column.
proc print data=sashelp.shoes;
run;
Default Features of PROC PRINT Output:
- Observation Column (
Obs): SAS automatically generates a column namedObson the far left that displays the observation row number (1, 2, 3, ...). - Variable Order: Variables are displayed from left to right in the exact order they appear in the dataset descriptor portion.
- Column Headings: Column headers default to the SAS variable names rather than variable labels unless explicitly requested otherwise.
- Formatting: Assigned permanent formats (such as
DOLLAR12.2orDATE9.) are honored and applied to column values.
2. Key Statements in PROC PRINT
To control which variables appear, how rows are identified, and how summaries or page breaks are handled, SAS provides several dedicated statements within PROC PRINT.
| Statement | Syntax | Primary Function & Exam Rules |
|---|---|---|
VAR | var variable-1 variable-2; | Controls which variables are printed and their left-to-right column sequence. Variables omitted from VAR are excluded from the report. |
SUM | sum variable-1 variable-2; | Calculates and displays column grand totals at the end of the report for specified numeric variables. If VAR is omitted, SUM prints all variables and totals the SUM variables. |
ID | id variable-1; | Replaces the default Obs column with the specified variable(s). The ID variable values anchor the left margin of each row. |
BY | by variable-1; | Divides report output into separate subgroup tables based on values of BY variables. Mandatory Rule: Input data MUST be pre-sorted by the BY variables! |
PAGEBY | pageby by-variable; | Forces a physical page break whenever the value of the specified BY variable changes. Mandatory Rule: Requires an accompanying BY statement! |
WHERE | where expression; | Selects which observations are printed. Because it is applied by the input engine, it can reference only variables that exist in the data set. |
Detailed Statement Mechanics
The VAR Statement
The VAR statement defines the precise list and order of variables to display. If you specify a variable in the VAR statement that does not exist in the dataset, SAS produces an error in the log and halts procedure execution.
proc print data=sashelp.shoes;
var Region Subsidiary Sales Returns;
run;
The SUM Statement
The SUM statement computes arithmetic totals for numeric variables. When used in combination with a BY statement, SUM produces subgroup totals for each BY group as well as a grand total at the bottom of the complete report.
The ID Statement
The ID statement suppresses the default Obs column and uses one or more dataset variables to identify each row. This is particularly useful when reports are long or split across multiple pages, as the ID variable(s) repeat on the left edge of every formatted line.
proc print data=sashelp.shoes;
id Product;
var Region Sales Inventory;
run;
The WHERE Statement
The official objective for PROC PRINT includes "select observations with a WHERE statement." Subsetting inside the procedure is preferable to building a filtered intermediate data set: nothing is copied, and the report reflects the live data.
proc print data=sashelp.shoes noobs;
where Region = 'Africa' and Sales > 10000;
var Product Subsidiary Sales Returns;
run;
WHERE in a PROC step supports the same extended operators it supports in a DATA step — BETWEEN ... AND, CONTAINS, LIKE, IS MISSING, and IN — and it filters at the input engine, so it can reference only variables that already exist in the data set.
The BY and PAGEBY Statements
Using a BY statement creates distinct sub-tables for each unique BY group. Prior to executing PROC PRINT with a BY statement, you must execute PROC SORT to order the dataset by those identical BY variables. Attempting to run PROC PRINT with a BY statement on unsorted data results in a fatal error: ERROR: Data set WORK.MYDATA is not sorted in order of BY variables.
When reports are large, PAGEBY controls pagination. Note that PAGEBY can only reference a variable that is listed in the BY statement.
proc sort data=sashelp.shoes out=work.shoes_sorted;
by Region Subsidiary;
run;
proc print data=work.shoes_sorted;
by Region Subsidiary;
pageby Region;
var Product Sales Returns Profit;
sum Sales Returns Profit;
run;
3. Essential PROC PRINT Options
Options are placed directly on the PROC PRINT header statement after the dataset specification.
proc print data=dataset-name <options>;
Summary of Critical PROC PRINT Options:
-
NOOBS- Suppresses the automatic
Obscolumn from appearing in the printed output. - Used when row numbers are unnecessary or uninformative for the report audience.
- Exam Note: If both
IDandNOOBSare specified, SAS suppresses theObscolumn and uses theIDvariable as the leftmost column.
- Suppresses the automatic
-
LABEL- Instructs SAS to use variable labels as column headers instead of variable names.
- If a variable has no label assigned, SAS defaults back to displaying the variable name for that specific column header.
- Exam Note: Simply assigning labels in a
LABELstatement insidePROC PRINTis NOT sufficient to display them—you MUST include theLABELoption on thePROC PRINTstatement line!
-
SPLIT='character'- Names a split character that forces a line break inside a column heading, letting long labels wrap onto several lines instead of stretching the column.
SPLIT=implies theLABELoption, so you do not code both. Everything to the left of the split character becomes line 1 of the heading, everything after it line 2, and so on.- The split character is consumed by the heading and never printed.
proc print data=work.employees split='*';
var EmployeeID LastName Salary;
label Salary = "Annual Gross*Salary (USD)"
LastName = "Employee*Surname";
run;
/* The Salary heading prints on two lines: Annual Gross
Salary (USD) */
Exam Note: To show labels you need either
LABELorSPLIT=on thePROC PRINTstatement. ALABELstatement alone changes nothing in the printed output. To display raw values instead of formatted ones there is noUNFORMATTEDoption inPROC PRINT— remove the format instead, either withformat Variable;inside the step (which clears the format for that step) or by not assigning one.
-
N="string"orN- Displays the total number of observations at the end of the report or at the end of each BY group. You can supply an optional text string, e.g.,
N="Total Accounts: ".
- Displays the total number of observations at the end of the report or at the end of each BY group. You can supply an optional text string, e.g.,
-
BLANKLINE=n- Inserts
nblank lines between observations in the printed output to enhance visual readability.
- Inserts
proc print data=sashelp.shoes noobs label n="Rows printed: ";
var Region Product Sales;
label Sales="Total Gross Sales ($)";
run;
4. Synthesis and Common Exam Scenarios
Let's examine how statements and options interact in typical SAS Base certification questions:
/* Advanced Detail Reporting Example */
proc sort data=sashelp.class out=work.class_sorted;
by Sex;
run;
proc print data=work.class_sorted noobs label;
by Sex;
id Name;
var Age Height Weight;
sum Weight;
label Height="Height (Inches)" Weight="Weight (Pounds)";
run;
Key Exam Traps to Remember:
- Missing SORT Step: Any
BYstatement inPROC PRINTrequires a priorPROC SORTstep. If the dataset is not sorted or indexed by the BY variable, the step terminates with an error. - Label Option Omission: If a student writes
label Height="Height in Inches";insidePROC PRINTbut omits theLABELoption on theproc print data=...line, SAS prints variable names (Height), NOT variable labels. - PAGEBY Without BY: Specifying
pageby Region;without aby Region;statement causes a syntax error. - ID vs NOOBS: Specifying
IDwithoutNOOBSstill suppresses theObscolumn automatically because theIDvariable replacesObs.
A SAS programmer executes the following code block, but SAS returns an error in the log: proc print data=work.sales_data; pageby Region; var Salesperson Sales Amount; run; What is the cause of this error?
Which PROC PRINT statement suppresses the default observation column and uses the values of a specified variable as row headers at the left margin of the report?
Consider the following SAS code: proc print data=work.employees; var EmployeeID LastName Salary; label Salary="Annual Gross Salary"; run; What column header will appear above the Salary column in the printed output?
Which PROC PRINT statement option lets a long variable label wrap onto two lines in the column heading, and also turns on label display without coding LABEL separately?