SAS Studio Workshop – Proc Print

PROC PRINT procedure :

It is used to print observations in a SAS dataset using some or all of the variables .

The syntax of PRINT procedure is :

PROC PRINT DATA = SAS-data-set ;

BY variable_list ;

ID variable(s);

VAR variable(s) ;

run;

The various attributes of PRINT procedure are :

DATA – SAS-data-set  specifies  the SAS data set to print .

BY – Produce a separate section of the report for each group

ID – Identify observations by the formatted values of the variables instead of by observation number .

VAR – Select variables that appears in the report and determine their order.

 

We want to see the observations in sales data set . The sales dataset stored in learn library . So, we use following code to print observations of sales dataset .

proc print data=learn.sales;

run;

OUTPUT:

It shows observation number (obs)  and variables . It shows purchase , age , gender and income variables from sales dataset .

 

We used var statement to show observations of EmpID , Customer and TotalSales variables only.

proc print data=learn.sales;

var EmpID Customer TotalSales;

run;

OUTPUT:

 

 

We used id statement to replace obs column with EmpID variable.

proc print data=learn.sales;

id EmpID;

var Customer TotalSales;

run;

OUTPUT:

 

We used title statement to print line in output . It shows “Listing of SALES” in output window and then it shows the observations of sales dataset .

title “Listing of SALES”;

proc print data=learn.sales;

id EmpID;

var Customer Quantity TotalSales;

run;

OUTPUT:

It shows the observations of sales data set .It includes EmpID , Customer , Quantity and TotalSales . We used format statement to show TotalSales in dollar formatted values.

proc print data=learn.sales;

id EmpID;

var Customer Quantity TotalSales;

format TotalSales dollar10.2;

run;

OUTPUT:

It shows sales data with TotalSales in dollar formatted value and Quantity in comma formatted value .

proc print data=learn.sales;

id EmpID;

var Customer Quantity TotalSales;

format TotalSales dollar10.2 Quantity comma7.;

run;

OUTPUT:

We can use where statement to output only Quantity values greater than 400.

proc print data=learn.sales;

where Quantity gt 400;

id EmpID;

var Customer Quantity TotalSales;

run;

 

OUTPUT:

 

It shows observations related to  EmpID values 1843 and 0177 . We used following statement as:

 

proc print data=learn.sales;

where EmpID in (‘1843’ ‘0177’);

id EmpID;

var Customer Quantity TotalSales;

format TotalSales dollar10.2 Quantity comma7.;

run;

OUTPUT:

We can use where statement  data set option as:

proc print data=learn.sales(where=( EmpID in (‘1843’ ‘0177’)));

id EmpID;

var Customer Quantity TotalSales;

format TotalSales dollar10.2 Quantity comma7.;

run;

OUTPUT:

We can use ten titles to produce in an output .

title1 “The XYZ Company”;

title2 “Sales Figures for Fiscal 2006”;

title3 “Prepared by Roger Rabbit”;

title4 “—————————–“;

footnote “All sales figures are confidential”;

proc print data=learn.sales;

id EmpID;

We sused

var Customer Quantity TotalSales;

format TotalSales dollar10.2 Quantity comma7.;

run;

 

OUTPUT:

 

We have to close title statement otherwise it will always show us titles in our output .

We can close title statements by using following code :

title1;

title2;

title3;

 

We can see temporary labels by label option in print procedure .

proc print data=learn.sales label;

id EmpID;

var TotalSales Quantity;

label EmpID = “Employee ID”

TotalSales = “Total Sales”

Quantity = “Number Sold”;

format TotalSales dollar10.2 Quantity comma7.;

run;

OUTPUT:

We can use by statement to show output group-wise . We need to sort the by variable to show in grouping . We used sort procedure to sort the variable .

The syntax of SORT procedure is :

PROC SORT DATA = SAS-data-set OUT= SAS-data-set ;

BY variable(s);

RUN;

BY  – the variables are sorted in ascending order by default . If we want to sort variables in descending order , we use DESCENDING  before variable name .

We sort sales data set in learn library to save the sorted data set as sales data set in work library . We sort sales data set by region variable .

proc sort data=learn.sales out=sales;

by Region;

run;

We print the sales data set from work library and also show labels associated with variables .We used by statement to group output by region variable . We used id statement to replace obs column with EmpID variable . We used var statement to show TotalSales and Quantity variables in output . we used label statement to assign labels to EmpID , TotalSales and Quantity variables . We used format statement to show TotalSales in dollar formatted values and Quantity in comma formatted values .

proc print data=sales label;

by Region;

id EmpID;

var TotalSales Quantity;

label EmpID = “Employee ID”

TotalSales = “Total Sales”

Quantity = “Number Sold”;

format TotalSales dollar10.2 Quantity comma7.;

run;

OUTPUT:

 

We create a new data set sales to sort region variable and store in sales data set .

title “Adding Totals and Subtotals to Your Listing”;

proc sort data=learn.sales out=sales;

by Region;

run;

We can print sales data with sum statement to find sum of variables .

proc print data= sales label;

by Region;

id EmpID;

var TotalSales Quantity;

sum Quantity TotalSales;

label EmpID = “Employee ID”

TotalSales = “Total Sales”

Quantity = “Number Sold”;

format TotalSales dollar10.2 Quantity comma7.;

run;

OUTPUT:

 

We sort sales data set by EmpID in ascending order .

title “Using the Same Variable in an ID and BY Statement”;

 proc sort data=learn.sales out=sales;

by EmpID;

run;

We print sales data set with EmpID , Customer , TotalSales and Quantity .

proc print data= sales label;

by EmpID;

id EmpID;

var Customer TotalSales Quantity;

label EmpID = “Employee ID”

TotalSales = “Total Sales”

Quantity = “Number Sold”;

format TotalSales dollar10.2 Quantity comma7.;

run;

OUTPUT:

 

We used sales data set with n = option to see total number of observations .

 title “Demonstrating the N option of PROC PRINT”;

proc print data=sales n=”Total number of Observations:”;

id EmpID;

var TotalSales Quantity;

label EmpID = “Employee ID”

TotalSales = “Total Sales”

Quantity = “Number Sold”;

format TotalSales dollar10.2 Quantity comma7.;

run;

OUTPUT:

We used double option to double space between rows of the report

title “Double-Spacing Your Listing”;

proc print data=sales double;

id EmpID;

var TotalSales Quantity;

label EmpID = “Employee ID”

TotalSales = “Total Sales”

Quantity = “Number Sold”;

format TotalSales dollar10.2 Quantity comma7.;

run;

OUTPUT:

 

It shows starting 5 observation of sales .

title “First Five Observations from SALES”;

proc print data=learn.sales(obs=5);

run;

OUTPUT:

 

It shows starting 5 observations with all headings in vertical direction .

title “First Five Observations from SALES”;

proc print data=learn.sales(obs=5) heading=vertical;

run;

OUTPUT:

 

We used sales data set to include observation starting from 5 onwards.

proc print data=learn.sales(firstobs=5) ;

run;

OUTPUT:

We can show observations from 5 to 9 by using following code :

proc print data=learn.sales(firstobs=5 obs =9) noobs;

run;

 

We can split the variables labels by using split option .It split in two parts of variable names as:

proc sort data=learn.sales out=sales;

by EmpID;

run;

proc print data=sales  split=”*”;

id Empid ;

by Empid;

var Customer Quantity ;

label Empid=”Employee *Id”

Customer = “Customer *Name”

Quantity = “Quantity *Sold”;

run;

OUTPUT:

 

We can display output in style format . It uses following statement :

style(header)={frontstyle = italic color =green} used to give font style as italic and color as green in header  of data set.

proc print data=learn.sales

  style(header)={fontstyle=italic color= green}

  style(obs)={backgroundcolor=#a8a44ff8a color=blue};

    var EmpID Customer Item;

run;

OUTPUT:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top