PROC PRINT in SAS for Displaying Datasets

PROC PRINT in SAS for Displaying Datasets, PROC PRINT in SAS is a straightforward procedure used to display the rows of a dataset.

PROC PRINT in SAS for Displaying Datasets

Below, we outline various approaches to utilize PROC PRINT effectively.

Method 1: Print the Entire Dataset

To print all rows in a dataset, you can use the following syntax:

/* Print entire dataset */ 
proc print data=my_data;
run;

Method 2: Print the First N Observations

If you want to display only the first few rows of the dataset, you can specify the OBS= option:

/* Print only the first five rows */ 
proc print data=my_data(obs=5);
run;

Method 3: Print Specific Variables

To print only selected variables from the dataset, use the VAR statement:

/* Print rows for the team and points variables only */ 
proc print data=my_data;
    var team points;
run;

Method 4: Print Dataset Grouped by a Specific Variable

You can group the dataset by a specific variable, such as team, by first sorting the data:

/* Sort rows of dataset by values in the team column */ 
proc sort data=my_data; 
    by team; 
run;

/* Print entire dataset grouped by values in the team column */ 
proc print data=my_data; 
    by team; 
run;

Method 5: Print Dataset with Title and Footer

You can enhance the printed output by adding a title and footer. Here’s how you can set that up:

/* Print dataset with title and footer */ 
title "First Five Rows of Basketball Dataset";
footnote "2015 Data Source";
proc print data=my_data;
run;

Example Dataset

To better illustrate the application of PROC PRINT, consider this dataset containing basketball player statistics:

/* Create dataset */
data my_data;
    input team $ position $ points assists;
    datalines;
A Guard 14 4
A Guard 22 6
A Guard 24 9
A Forward 13 8
A Forward 13 9
A Guard 10 5
B Guard 24 4
B Guard 22 6
B Forward 34 2
B Forward 15 5
B Forward 23 5
B Guard 10 4
;
run;

Practical Examples

Example 1: Print Entire Dataset

Use the following code to print every row from the dataset:

/* Print entire dataset */
proc print data=my_data;
run;

This command outputs all rows from my_data.

Example 2: Print First N Observations

To display only the first five observations, implement:

/* Print first five rows of dataset */
proc print data=my_data(obs=5);
run;

You will see output limited to five rows.

Example 3: Print Specific Variables

Focus on particular variables with:

/* Print rows for team and points variables only */ 
proc print data=my_data;
    var team points;
run;

This outputs only the team and points columns.

Example 4: Print Dataset Grouped by Team

First, sort the data by team:

/* Sort rows of dataset by values in team column */ 
proc sort data=my_data; 
    by team; 
run;

/* Print entire dataset grouped by values in team column */
proc print data=my_data; 
    by team; 
run;

The output is organized first by team A and then by team B.

Example 5: Print with Title and Footer

To include a title and footer:

/* Print dataset with title and footer */ 
title "First Five Rows of Basketball Dataset";
footnote "2015 Data Source";
proc print data=my_data;
run;

The title appears above and the footer below the printed dataset.

Conclusion

PROC PRINT is a flexible and powerful tool for displaying datasets in SAS.

Whether you wish to print the entire dataset, a subset, specific variables, or present the data with added context through titles and footers, PROC PRINT helps streamline your data presentation effortlessly.

SAS Archives » FINNSTATS

You may also like...

Leave a Reply

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

three × 1 =