How to Use PROC COMPARE in SAS (with Examples)

Deepanshu Bhalla Add Comment

In this tutorial, we will cover how to use PROC COMPARE in SAS, along with examples.

Introduction : PROC COMPARE

PROC COMPARE in SAS is used to compare the contents and structure of two datasets. It returns a summary of both the similarities and differences found between two datasets.



The syntax of PROC COMPARE is as follows:

proc compare
 base = data1
 compare = data2;

This compares the datasets data1 and data2 and displays the differences between them. By default, PROC COMPARE compares all the variables in the datasets.

Let's compare two built-in SAS datasets: sashelp.class and sashelp.classfit.

proc compare
 base = sashelp.class
 compare = sashelp.classfit;
Dataset Summary

In the dataset summary section, it shows the comparison of the structure of both the datasets and returns the following analysis.

  • Dataset Creation Dates
  • Dataset Modification Dates
  • Number of Variables
  • Number of Observations
  • Labels
Variable Summary

In the Variable Summary section, it shows how many variables which are common in both the datasets and how many variables are in one dataset but not in the other dataset.

Observation Summary

In the Observation Summary section, it displays how many observations are in both the datasets and how many of them have equal or unequal values in some or all of the variables.

PROC COMPARE Observation Summary
Values Comparison Summary

In the "Values Comparison summary" section, it displays summary about the variables that either have all values exactly equal or contain some unequal values.

PROC COMPARE: Values Comparison Summary
PROC COMPARE: Values Comparison Details

How to Compare Specific Variables

You can use the VAR statement in PROC COMPARE to compare specific variables of both the datasets. Please note that the initial summary about dataset and variables remain unchanged. You should focus on the Value Comparison Results. In the code below, we are comparing "name" variable in both the datasets.

proc compare
 base = sashelp.class
 compare = sashelp.classfit;
 var name;

How to Compare Only Structure of Datasets

By using NOVALUES option in PROC COMPARE, we can tell SAS not to compare values between the two datasets. In short it returns only the similarities and difference in the variables, not values. The LISTVAR option is used to list the variables which are in one dataset but not in the other dataset.

proc compare
 base = sashelp.class
 compare = sashelp.classfit
 novalues listvar;
Related Posts
Spread the Word!
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 "How to Use PROC COMPARE in SAS (with Examples)"
Next → ← Prev