PROC PRINT in SAS - What Does the PRINT Procedure Do

🔍 What is PROC PRINT in SAS?

PROC PRINT is a SAS procedure that prints the observations in a SAS dataset to the output window. It provides a tabular view of your data and is commonly used for checking data during the data cleaning and analysis phase.


🧱 Basic Syntax of PROC PRINT

PROC PRINT DATA=dataset-name;
RUN;

Explanation:

  • PROC PRINT tells SAS to initiate the print procedure.
  • DATA=dataset-name specifies the dataset to print.
  • RUN; executes the procedure.


📝 Example: Basic PROC PRINT

DATA employees;
INPUT ID Name $ Department $ Salary; DATALINES; 1 John HR 50000 2 Alice IT 70000 3 Bob Finance 60000 ; RUN; PROC PRINT DATA=employees; RUN;

This code will output all rows and columns of the employees dataset.


🎯 Using PROC PRINT with SELECTED VARIABLES

You can use the VAR statement to print only specific columns from your dataset.

PROC PRINT DATA=employees;
VAR Name Department; RUN;

This will display only the Name and Department columns.


🔢 Adding Row Numbers with ID Statement

The ID statement lets you replace the default observation number with a variable value.

PROC PRINT DATA=employees;
ID ID; VAR Name Department Salary; RUN;

Now, instead of row numbers (1, 2, 3), SAS will display the values from the ID column.


🧾 Using Labels in Output

To display more meaningful column headers, you can use labels.

DATA employees;
SET employees; LABEL Salary = 'Annual Salary'; RUN; PROC PRINT DATA=employees LABEL; VAR Name Salary; RUN;

Now the column header will display as Annual Salary.


🎯 Filtering Rows with WHERE Statement

The WHERE clause lets you filter records before printing.

PROC PRINT DATA=employees;
WHERE Department = 'IT'; RUN;

This will display only the rows where the department is IT.


📌 More Useful Options

  • OBS=number: Limits number of observations printed.
  • NOOBS: Suppresses the default row number column.
  • LABEL: Displays labels instead of variable names.
  • TITLE: Adds a title to the output.

PROC PRINT DATA=employees (OBS=2) NOOBS LABEL;
TITLE "Employee Report"; RUN;

💡 Best Practices for PROC PRINT

  • Use VAR and ID to improve readability.
  • Always apply WHERE clauses for large datasets.
  • Use LABEL for user-friendly column headings in reports.


➕Here are some More Examples for Proc print -

1. First Proc print to get the output for sasuser.admit dataset in output window

Code -

proc print data=sasuser.admit;
run;
Proc Print Output

2. To get the sum total for a specified column, you can use the sum keyword

Code -
proc print data=sasuser.admit;
sum fee;
run;
Proc Print Sum


3. To get the sum total for more than one specified columns

Code -
proc print data=sasuser.admit;
sum fee height;
run;
Proc print sum of two variables


4. Selecting variables for printing, the use of the var keyword

Code -
proc print data=sasuser.admit;
var age sex height fee;
sum fee ;
run;
Var keyword in proc


5. Selecting variables for printing  without default obs column

Code -
proc print data=sasuser.admit noobs;
var age sex height fee;
sum fee ;
run;
Noobs in Proc Print


6. Use of no obs - It gives the total no of observations and shows the obs column too

Code -

proc print data=sasuser.admit no obs;
var age sex height fee;
sum fee ;
run;
/* same can be achieved by N; 
proc print data=sasuser.admit N;
var age sex height fee;
sum fee ;
run;
use of no obs or N in proc print


7. Use of ID statement - The id statement basically replaces the obs column with the specified variable against the id keyword

Code -
proc print data=admit;
var age sex height fee;
id name;
sum fee ;
run;
use of ID statement in proc print


8. More than one variable in ID statement  - If you specify two variables in the id statement then those two vars replace the obs columns and proc print of data is also displayed.

Code - 
proc print data=sasuser.admit;
id name age height;
run;
more than one variable in ID


9. The use of where statement is to filter the observations (Note - we can’t use IF statement in proc print to filter data)

Code -
proc print data=admit;
var age sex height fee;
where age gt 30;
sum fee ;
run;
where condition in proc print


10. The use of '?' operator along with where condition for filtering the data

Code - 
proc print data=sasuser.admit;
var age sex height fee name;
where name ? 'er';
sum fee ;
run;
use of ? operator in Where statement


11. The use of double spacing in the listing output between the rows

Code - 
proc print data=sasuser.admit double;
var age sex height fee name;
where name ? 'er';
sum fee ;
run;
Double spacing in proc print


12. Titles in Proc print -  The use of title in the listing output, title or title1 is the same

Code - 
title1 'Proc Print Examples';
title3 'Please subscribe datahark on Facebook, youtube and visit our website';
title4 'website - https://www.datahark.in/'
title5 'facebook - https://www.facebook.com/datahark/'
title6 'youtube - https://www.youtube.com/channel/UCZAPTZ0YvrTiKdh2WgnxEBA or Search for Datahark'
proc print data=sasuser.admit;
var age sex height fee name;
where name ? 'er';
sum fee ;
run;
Titles in proc print










📌 Conclusion

PROC PRINT is a foundational tool in the SAS programmer’s toolkit. Whether you’re debugging a data step, presenting a report, or just exploring your data, it offers a quick and flexible way to visualize your dataset.

Mastering PROC PRINT will make your data review and reporting tasks much easier. Try the examples above with your own datasets and see the results instantly!

Post a Comment

0 Comments