SAS : PROC PRINT (with Examples)

Deepanshu Bhalla Add Comment

This tutorial explains how to use the PROC PRINT procedure in SAS, along with examples.

PROC PRINT is a commonly used SAS procedure that displays the contents of a dataset.

Sample Dataset

Let's create a sample SAS dataset that will be used in the examples of this tutorial.

data mydata;
input name $ height weight sex $ sale;
datalines;
John 175 70 M 1000
Emily 160 55 F 800
Michael 180 80 M 1200
Sophia 165 60 F 950
David 170 75 M 1100
Emma 155 50 F 750
;
run;

Print Entire Dataset

The simplest usage of PROC PRINT is that it displays all variables in the dataset.

proc print data=mydata;
run;
SAS: Print Entire Dataset

Print First N Observations

The OBS= option in PROC PRINT is used to display first N observations in the dataset. In this example, it displays first 5 observations.

proc print data=mydata(obs=5);
run;
Print First N Observations

Print Specific Variables

You can specify which variables you want to display using the VAR statement.

proc print data=mydata;
var name height weight;
run;
Print Specific Variables

Exclude Observation Number in the Output

You can tell SAS not to display observation number in the output using the NOOBS option.

proc print data=mydata noobs;
var name height weight;
run;
Exclude Observation Number in the Output

Conditional Printing

You can use the WHERE statement to display only specific observations that meet a condition. The following code generates an output that displays only the observations where the 'sex' variable has a value of 'M'.

proc print data=mydata noobs;
where sex='M';
run;
Conditional Printing

How to Use Formats in PROC PRINT

You can apply formats to the variables using the format statement. In the code below, format sale dollar10.1; applies a dollar format to the "sale" variable.

proc print data=mydata;
format sale dollar10.1;
run;
Use Formats in PROC PRINT

How to Use Title and Footnote in PROC PRINT

You can use the TITLE and FOOTNOTE statements to print the dataset with a title and a footer.

proc print data=mydata;
title "Students Dataset";
footnote "Sample Dataset";
run;
How to Use Title and Footnote in PROC PRINT

Labeling Variables in PROC PRINT

You can use the label statement to specify labels for the variables.

proc print data=mydata label obs='Observation No.';
label name='Students Name' weight='Students Weight';
run;
Labeling Variables in PROC PRINT

Summing Variables in PROC PRINT

You can use the sum statement to display total at the bottom of the output.

proc print data=mydata;
sum sale;
run;

To sum all the numeric variables, you can use the SUM statement with keyword _numeric_. To label the grand total, use the grandtotal_label= option.

proc print data=mydata grandtotal_label='Grand Total';
sum _numeric_;
run;
Summing Variables in PROC PRINT

Print Data Grouped by Specific Variable

The following code displays the data in groups based on the "sex" variable. It is important to sort the dataset when you are using the BY statement in PROC SORT. If data is already sorted by the variable which is used in the BY statement, you don't need to sort the dataset prior to PROC PRINT procedure.

proc sort data=mydata;
by sex;
run;

proc print data=mydata noobs;
by sex;
run;
Print Data Grouped by Specific Variable

Summing Variables Grouped by Specific Variable

The following code generates a report that displays sales data by 'sex', including a total row, a count of observations, and a dynamic title for each group. The sales values will be formatted as dollars with one decimal place.

proc sort data=mydata;
by sex;
run;

options nobyline;
proc print data=mydata noobs sumlabel='Total'
           n='Number of observations for the sex type: '
           'Number of observations in the data set: ';
sum sale;
by sex;
title 'Sales for #byval(sex)';
format sale dollar7.1;
run;
options byline;

When you use PROC PRINT with the NOBYLINE option, each BY group starts on a new page. The TITLE statement adds a title in the output. The #BYVAL keyword tells SAS to include the current value of the BY variable "sex" in the title.

Summing Variables Grouped by Specific Variable

How to Style Table in PROC PRINT

You can use the style(location)= option to modify the appearance of the output. In the code below, the header cells of the output will appear with an italic font and a black background, while the data cells will have a red background with white text.

proc print data=mydata noobs
   style(HEADER)={fontstyle=italic backgroundcolor=black foreground=white}
   style(DATA)={backgroundcolor=red foreground=white};
run;
How to Style Table in PROC PRINT
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 0 Response to "SAS : PROC PRINT (with Examples)"
Next → ← Prev