All About PROC PRINT

In this blog, we will discuss some of the commonly used options statements of PROC PRINT in SAS.

Below are some of the common tasks which needs to be done and  how these can be achieved using PROC PRINT.

  • Print a SAS dataset
  • Print only a few variables of a SAS dataset (VAR statement)
  • Print Variable Labels (use Label Option and Label Statement)
  • Suppress observation column in the output (NOOBS option)
  • Control Observations to be printed (WHERE statement)
  • Print Total Values of Numeric Variable (SUM statement)
  • Format the numeric values printed (FORMAT statement)

Now let's look at the SAS code used for each of the above tasks.

Read Data Into SAS

%* Read data;
FILENAME rtfile '/folders/myshortcuts/myfolders1/data/RETIAL_TRANSACTIONS1.csv';

PROC IMPORT DATAFILE=rtfile
	DBMS=CSV
	OUT=retail_transactions;
	GETNAMES=YES;
RUN;

Filename statement defines a reference to a physical file in this case a CSV file (RETIAL_TRANSACTIONS1.csv) and PROC IMPORT  is used for reading a data file and creating a SAS dataset - retail_transactions. The dataset is created in work library as this is only single level name.

Print a SAS dataset

For print a SAS dataset you could use below code.

PROC PRINT DATA = retail_transactions;
RUN;

If you do not give DATA = retail_transactions, SAS will print the latest SAS dataset created in the session. You will have to be careful if SAS dataset is big (have large number of obvervasions).

Control Variables to be printed

%* Print Selected;
PROC PRINT DATA = retail_transactions;
  VAR CATEGORY_DESCRIPTION
   HOUSEHOLD_ID
   PRODUCT_ID
   TRANSACTION_DATE
;
RUN;

VAR statement is used for printing selected variables of the input SAS dataset.

Segregate Output by a Variable Values

%* Print Records by Category;
proc sort data=retail_transactions;
  by CATEGORY_DESCRIPTION;
run;
PROC PRINT DATA = retail_transactions;
  BY CATEGORY_DESCRIPTION;
  VAR 
         PRODUCT_ID
         SUB_CATEGORY_DESCRIPTION
     TRANSACTION_DATE
;
RUN;

BY statement is used to print observations by values of BY variable (e.g. CATEGORY_DESCRIPTION). Whenever you use BY statement , you need to have a dataset sorted with same BY variables. Details on Sorting of SAS dataset.

proc print BY statement

Improve Layout of PROC PRINT output

We can use NOOBS and LABEL option along with LABEL statement to improve output. Label statement helps in assigning labels to variables. LABEL instructs to use label instead of variable name and NOOBS suppresses obs column. TITLE statement defines title of the output.

%* Improve Output - Labels and Title;
PROC PRINT DATA = retail_transactions Label Noobs;
  BY CATEGORY_DESCRIPTION;
  VAR 
     PRODUCT_ID   SUB_CATEGORY_DESCRIPTION
     TRANSACTION_DATE ;
    Label PRODUCT_ID ="Product ID"
      SUB_CATEGORY_DESCRIPTION ="Sub Category"
      TRANSACTION_DATE = "Transaction Date" 
      CATEGORY_DESCRIPTION=" Category Description"	  ;

 title "Product by Category"; 
RUN;

PROC PRINT OUTPUT

Control Observations

You can use obs= option to print specified number of observations. In the below example, we wanted to print 10 obs.

PROC PRINT DATA = retail_transactions (obs=10)  Label Noobs ;
  VAR    PRODUCT_ID  SUB_CATEGORY_DESCRIPTION
     TRANSACTION_DATE ;
Label PRODUCT_ID ="Product ID"
      SUB_CATEGORY_DESCRIPTION ="Sub Category"
      TRANSACTION_DATE = "Transaction Date"
	  ;
 title "Products Return for Category - PC Hardware"; 
RUN;

You can also use WHERE statement to control observations to be printed.

%* Control Observations;
PROC PRINT DATA = retail_transactions Label Noobs;

  WHERE RETURN_IND="Y" and 
                  CATEGORY_DESCRIPTION = 'PC HDWR';

  VAR    PRODUCT_ID  SUB_CATEGORY_DESCRIPTION
     TRANSACTION_DATE ;
Label PRODUCT_ID ="Product ID"
      SUB_CATEGORY_DESCRIPTION ="Sub Category"
      TRANSACTION_DATE = "Transaction Date"
	  ;
 title "Products Return for Category - PC Hardware"; 
RUN;

 

Print Total Value of Numeric Variable

We want to sprint total values at the end for numeric column and here is how you can do using SUM statement.

PROC PRINT DATA = retail_transactions Label Noobs;
  WHERE TRANSACTION_TYPE=2; 
  VAR 
     PRODUCT_ID
	 CATEGORY_DESCRIPTION
	 TRANSACTION_DATE
	 UNIT__PRICE
;
SUM UNIT__PRICE;
Label PRODUCT_ID ="Product ID"
      CATEGORY_DESCRIPTION ="Category"
      TRANSACTION_DATE = "Transaction Date"
	  UNIT__PRICE = " Unit Price"
	  ;
 title "Unit Price and Total Value of Products Returned"; 
RUN;

Format statement to print the currency value as formatted values.

PROC PRINT DATA = retail_transactions Label Noobs;
  WHERE TRANSACTION_TYPE=2; 
  VAR 
     PRODUCT_ID
	 CATEGORY_DESCRIPTION
	 TRANSACTION_DATE
	 UNIT__PRICE
;
SUM UNIT__PRICE;
Label PRODUCT_ID ="Product ID"
      CATEGORY_DESCRIPTION ="Category"
      TRANSACTION_DATE = "Transaction Date"
	  UNIT__PRICE = " Unit Price"
	  ;
 title "Unit Price and Total Value of Products Returned"; 
 format UNIT__PRICE dollar10.2;
RUN;

proc print formatted values

 

 

 

 

2 thoughts on “All About PROC PRINT”

Leave a Comment