PROC PRINT In SAS (With 10+ Examples)

You can use the PROC PRINT procedure to print observations in a SAS data set using some or all of the variables. It’s one of the oldest yet relevant SAS procedures which is being used widely.

With the new more advanced procedure like PROC REPORT, ODS system, PROC PRINT kind of left behind in the race. Though it is a handy and widely used procedure when it comes to printing the dataset values. 

What Does the PRINT Procedure Do?

The PRINT procedure prints the observations in a SAS data set or rows from a SAS Cloud Analytic Services (CAS) table using all or some of the variables.

You can create a variety of reports ranging from a simple table to a highly customized report that groups the data and calculates totals and subtotals for numeric variables.

Basic Syntax:

				/* syntax */
proc print data=SAS-data-set; 
run;
			

The following sample dataset will be created from existing dataset sashelp.cars to demonstrate different use cases of PROC PRINT procedure in SAS.

				/* create sample dataset */

data work.my_cars;
	set sashelp.cars(obs=10);
run;
			

Example 1: Print Entire Dataset Observations

The fundamental of this procedure is to print observations from the SAS dataset. It can be done simply by invoking the PRINT procedure by passing the dataset name.

Here is a simple example to print all the observations from work.my_cars.

				/*print entire dataset*/

proc print data=my_cars; 
run;
			
SAS print entire dataset

Example 2: How To Print First N Observations In SAS

In the previous example we have printed the entire dataset. Now in this example we will print only the first N observations from the dataset.

You can use the where clause to filter out the data if you know the exact data and column on which you can apply where clause. 

But alternatively, you can use OBS= option to extract first N observation without even knowing much details about the dataset. The OBS is a sequential hidden variable which can be used to print the first N observations.

				/* How To Print First-N Observations */

proc print data=my_cars(obs=5); 
run;
			
How To Print First-N Observations in SAS

Example 3: PROC PRINT: Select Variable For Reporting

In the PROC PRINT procedure you get the option to choose variables to be displayed in the reports.

You can use a VAR statement followed by a list of variables you want to add in the proc print output report. You must list the variables in the same order the way you wanted to be displayed in the report.

				/* PROC PRINT: Selected Variables in a Report*/

proc print data=my_cars;
	var make model type msrp;
run;
			
PROC PRINT Selected Variables in a Report

Example 4: How To Exclude Obs Number From The PROC PRINT Output

If you have observed in the previous examples, the output generated has one additional column “Obs”.

If you want to exclude this observation number column from the proc print output report then NOOBS option can be used. It suppresses the observation number in the output.

				/* How to exclude Observation Number column in PROC PRINT */

proc print data=my_cars noobs; 
	var make model type msrp;
run;
			
How to exclude Observation Number column in PROC PRINT

Example 5: PROC PRINT With Conditional Printing

Instead of printing all the rows or first N rows, you can print observations based on specific conditions using the WHERE clause.

You can add one more multiple conditions in the where clause. It filters out the data and creates proc print output reports. 

In the following example we want a report only for Car type: Sedan. PROC PRINT is used with WHERE clause to generate this report.

				/* PROC PRINT: Conditional Printing in a Report */

proc print data=my_cars noobs;
	var make model type msrp; 
	where type='Sedan';
run;

			
PROC PRINT Conditional Printing in a Report In SAS

Example 6: How To Add TITLE and FOOTER Note In The PROC PRINT Report

The report generated in the previous examples doesn’t explicitly say anything about the report.

You can title some extra information such as TITLE description and footer note for clear and better understanding of the report.

  • TITLE keyword specifies title lines for SAS output.
  • FOOTNOTE keyword writes up to 10 lines of text at the bottom of the procedure or DATA step output.
				/* PROC PRINT: How to add Title and Footer Note in the report */

proc print data=my_cars noobs;
	var make model type msrp; 
	title "Sample Data set: sashelp.cars";
	footnote 'Cars Data set printed using PROC PRINT';
run;
			
PROC PRINT - How to add Title and Footer Note in the report

Example 7: PROC PRINT With Grouped Variables

To group the variables in the proc print procedure you can use BY keyword. It produces a separate section of the report for each BY group. You can group data based on one BY-variable or multiple BY-variables.

Before you use BY-variables in the PROC PRINT, you must sort the dataset based on BY- variables.

Group BY One Variable

Here is an example of a group by one variable.

				/* sort dataset */
proc sort data=my_cars;
	by make; 
run;

/* group by one variable in proc print */
proc print data=my_cars noobs;
	var make model type msrp; 
	by make;
run;
			
Group by one variable in proc print

Group BY One Variable

Here is an example of a group by multiple variables.

				/* sort dataset */
proc sort data=my_cars;
	by make type; 
run;

/* group by multiple variables in proc print */
proc print data=my_cars noobs;
	var make model type msrp; 
	by make type;
run;

			
SAS Group by multiple variables in proc print

Example 8: Summing Numeric Variables In PROC PRINT Report

You can use SUM statements in the proc print procedure to calculate the SUM (total values of numeric variables) and display it in the report.

In the below example we are only calculating SUM for selected numeric variables, msrp and invoice.

Additionally, a label is also added for the grand total line using grandtotal_label=option.

				/* Summing Numeric Variables In PROC PRINT Report */

proc print data=my_cars grandtotal_label='Grand Total';
	sum msrp invoice;
run;
			
Summing Numeric Variables In PROC PRINT Report

Example 9: Calculate Grand Total For All Numeric Variables

In order to calculate the grand total for numeric variables you need to use a SUM statement followed by numeric variable names.

To calculate the grand total for all the numeric variables, either you can list all the numeric variables or you can use _NUMERIC_ keyword.

				/* calculate grand total for all numeric variables */

proc print data=my_cars grandtotal_label='Grand Total';
	sum _numeric_;
run;

			
Calculate grand total for all numeric variables in SAS

Example 10: How To Style PROC PRINT Output Report

To create customized style proc report output in sas you need to use STYLE=options.

You can use the STYLE=option to set the style element for the entire report, for all the report columns, for the column headings, or on the specific report cell with conditional formatting. 

  • style(report): Set the style element for the report
  • style(column): Set the style element for the report columns
  • style(header): Set the style element for the column headings

Here is the simple example of how to style proc print output report in SAS.

				/* How To style PROC PRINT Output Report */

proc print data=my_cars noobs
   style(HEADER)={fontstyle=italic backgroundcolor=green foreground=white}
   style(DATA)={backgroundcolor=gray foreground=white};
   id make;
   var model type origin msrp ;
   var invoice enginesize / style(header)={backgroundcolor=blue foreground=white} 
   							style(column)={backgroundcolor=blue};
run;
			
How To style PROC PRINT Output Report

Example 11: PROC PRINT: Conditional User Defined Formatting

You can style the proc print output report using conditional user defined formatting. In the following example user defined format my_color is created and applied on the column “msrp”.

The cells with MSRP blank values will be highlighted with white color. The cells with MSRP values up to 30000 will be highlighted with green color and the cells with MSRP values more than 30000 will be highlighted with light red color.

With the above information let’s first create a user defined format called “my_color”.

				/* create user defined format */

proc format;
	value my_color
			  .    = 'white'
		Low-30000  = 'green'
		30001-High = 'lightred';
run;
			

This user defined format my_color is available now and ready to apply it on the MSRP variable in proc print output.

				/* PROC PRINT: Conditional User Defined Formatting */

proc print data=my_cars noobs
   style(HEADER)={fontstyle=italic backgroundcolor=green foreground=white}
   style(DATA)={backgroundcolor=gray foreground=white};
   id make;
   var make model type origin;
   var msrp / style(column)={backgroundcolor=my_color.} ;
   var invoice enginesize / style(header)={backgroundcolor=blue foreground=white} 
   							style(column)={backgroundcolor=blue};
run;
			
PROC PRINT - Conditional User Defined Formatting

Example 12: Create RTF File With PROC PRINT Output In SAS

You can print the PROC PRINT result to a RTF (Rich Text Format) file in SAS. ODS system options can be used in SAS to create this document. ODS statements help generate high-quality, detailed presentation output from SAS. 

You need to add an ODS RTF statement in the beginning of your code by specifying output file details and add an ODS RTF close statement at the end. 

We will use the earlier example and put the result into an RTF file using the ODS RTF statement.

				/* Create External RTF file using PROC PRINT In SAS */

options nodate nodetails;
ods rtf file='/home/u61950255/Files/proc_print_Result.RTF';
proc print data=my_cars noobs
   style(HEADER)={fontstyle=italic backgroundcolor=green foreground=white}
   style(DATA)={backgroundcolor=gray foreground=white};
   id make;
   var make model type origin;
   var msrp / style(column)={backgroundcolor=my_color.} ;
   var invoice enginesize / style(header)={backgroundcolor=blue foreground=white} 
   							style(column)={backgroundcolor=blue};
   title 'Formatted Report Created Using PROC PRINT Procedure';
run;
ods rtf close;
			
Create External RTF file using PROC PRINT In SAS

Example 13: Create PDF File With PROC PRINT Output In SAS

You can print the PROC PRINT result to an external PDF file in SAS. ODS system options can be used in SAS to create this document. ODS statements help generate high-quality, detailed presentation output from SAS.

				/* Create External PDF file using PROC PRINT In SAS */

options nodate nodetails;
ods pdf file='/home/u61950255/Files/proc_print_Result.PDF';
proc print data=my_cars noobs
   style(HEADER)={fontstyle=italic backgroundcolor=green foreground=white}
   style(DATA)={backgroundcolor=gray foreground=white};
   id make;
   var make model type origin;
   var msrp / style(column)={backgroundcolor=my_color.} ;
   var invoice enginesize / style(header)={backgroundcolor=blue foreground=white} 
   							style(column)={backgroundcolor=blue};
   title 'Formatted Report Created Using PROC PRINT Procedure';
run;
ods pdf close;
			

FAQ – PROC PRINT In SAS

What does PROC PRINT do?

PROC PRINT is a SAS procedure that prints the contents of a SAS data set to the output window or to an external file.

How do I use PROC PRINT in SAS?

To use PROC PRINT in SAS, you need to specify the data set to be printed and the variables to be included in the output. You can also customize the output by using various options such as the VAR statement, the BY statement, and the WHERE statement.

What are the advantages of using PROC PRINT?

PROC PRINT provides a quick and easy way to view the contents of a SAS data set. It also allows you to customize the output by using various options such as the VAR statement, the BY statement, and the WHERE statement.

How do I customize the output of PROC PRINT?

To customize the output of PROC PRINT, you can use various options such as the VAR statement, the BY statement, and the WHERE statement. You can also use STYLE=options to add different colors in the report.

 

These options allow you to control the appearance of the output and the variables that are displayed.