Written and maintained by CASRAI Editorial Board
Last updated
PROC COMPARE is the SAS procedure that compares two data sets, or two sets of variables inside one data set, and reports every difference it finds. The short version: name the two data sets with BASE= and COMPARE=, add an ID statement so observations are matched by key, and read the summary reports and the SYSINFO return code. This guide covers the sequence from first run to an automated check, and what the output means. For the wider analysis context, see the guide to ANOVA in SAS and the comparison of SAS and SPSS for research analysis.
What PROC COMPARE is used for in research
PROC COMPARE answers one question: are these two data sets the same, and if not, where do they differ? In research practice that covers checking that a re-run of a cleaning program reproduces the earlier data set, confirming that two versions of an analysis file differ only where you expect, and validating a derived data set against an independently programmed one. If your data sets follow a standard such as CDISC clinical data standards, the same comparison applies to the standardized files.
According to the SAS 9.4 documentation, PROC COMPARE sends its results to the SAS log, the SYSINFO macro variable, procedure output, and output data sets. That means one run can serve a person reading a report and a program deciding whether to continue.
Install or access (official source only)
PROC COMPARE runs inside a SAS session, so it is available wherever you already run SAS code through an installation or environment licensed to you or your institution. The reference page for the procedure is part of SAS’s own documentation on support.sas.com (listed under Sources below). Check which SAS release your site runs, because option behavior is documented per release.
Step-by-step: compare two data sets
- Name the data sets.
BASE=is the base data set andCOMPARE=is the comparison data set. SAS documents the basic call asproc compare base=SAS-data-set compare=SAS-data-set; run;. - Run the minimal step. The documentation’s own example is
proc compare base=proclib.one compare=proclib.two; run;. - Match observations by key with ID. Values of the
IDvariables label each observation in the output. Duplicate ID values produce notes or warnings, so use a key that is unique within each data set. - Restrict or pair variables. The
VARstatement limits which variables are compared. TheWITHstatement names the variables in the comparison data set to pair against theVARvariables, andVARis required alongsideWITH. If you omitCOMPARE=, the procedure compares variables within the one data set and you must useWITH(documented in the SAS 9.1.3-era overview). - Write differences to the log or a data set.
WARNING,PRINTALLandERRORwrite a description of the differences to the log.OUT=creates an output data set, by default one observation for each pair of matching observations;OUTSTATS=writes summary statistics for each numeric variable pair. - Control the report.
NOVALUESsuppresses the value comparison,NOSUMMARYsuppresses the summary reports, andTRANSPOSEprints the report by observation instead of by variable.
proc compare base=work.analysis_v1
compare=work.analysis_v2;
id subject_id;
run;
Output and how to read it
The default summary reports
By default PROC COMPARE prints four summary reports: a data set summary, a variables summary, an observation summary, and a values comparison summary (SAS 9.1.3-era overview). Read them in that order. The first two tell you whether the structure matches (which variables exist in only one data set, and whether types, lengths, labels and formats differ). The last two tell you whether the same observations are present and whether the values agree. The BRIEFSUMMARY option suppresses the first four reports.
The OUT= data set
An OUT= data set carries the ID and BY variables and the compared variables, and each row has a _TYPE_ value: BASE, COMP, DIF or PERCENT (SAS 9.1.3-era overview). Options such as OUTBASE, OUTCOMP, OUTDIF and OUTPERCENT add the corresponding observation types. Filtering on _TYPE_='DIF' gives a compact list of the values that differ.
Which numeric variables get statistics
STATS prints statistics only for the numeric variables that are unequal; ALLSTATS and PRINTALL print them for all numeric variables.
The SYSINFO return code
PROC COMPARE stores a return code in the automatic macro variable SYSINFO. The value is the sum of the codes for each condition that is true, so one number can encode several differences. The documented codes are:
| Condition | Code | Meaning |
|---|---|---|
| DSLABEL | 1 | Data set labels differ |
| DSTYPE | 2 | Data set types differ |
| INFORMAT | 4 | Variable has different informat |
| FORMAT | 8 | Variable has different format |
| LENGTH | 16 | Variable has different length |
| LABEL | 32 | Variable has different label |
| BASEOBS | 64 | Base data set has observation not in comparison |
| COMPOBS | 128 | Comparison data set has observation not in base |
| BASEBY | 256 | Base data set has BY group not in comparison |
| COMPBY | 512 | Comparison data set has BY group not in base |
| BASEVAR | 1024 | Base data set has variable not in comparison |
| COMPVAR | 2048 | Comparison data set has variable not in base |
| VALUE | 4096 | A value comparison was unequal |
| TYPE | 8192 | Conflicting variable types |
| BYVAR | 16384 | BY variables do not match |
| ERROR | 32768 | Fatal error: comparison not done |
For an automated check, SAS shows testing the value right after the step, for example %if &sysinfo >= 64 %then %do; handle error; %end;. Because the codes are sums of powers of two, a result of 4096 means only values differ, while 4104 means values differ and a format differs (4096 + 8). A result of 0 means no differences were found in the conditions listed above.
Troubleshooting
- SYSINFO reads zero or an unexpected value later in the program. PROC COMPARE must run before you check
SYSINFO, and every SAS step resets it. Copy it into your own macro variable (%let rc=&sysinfo;) immediately after the step. - Notes or warnings about duplicate ID values. The
IDvariables label observations, and duplicates in them are flagged. Choose a key that is unique in both data sets. - Numeric values flagged that look identical. In the SAS 9.1.3-era overview the default
CRITERION=is 0.00001 and the defaultMETHOD=isEXACT(RELATIVEwhenCRITERION=is used). Check your release’s reference page before relying on these defaults in a validation plan. - Missing values. By default a missing value is equal only to a missing value of the same kind.
NOMISSBASE,NOMISSCOMPandNOMISSINGchange this. - Variables or observations that exist in only one data set.
LISTVARandLISTOBSlist them.
Free alternatives
If you do not have SAS, the same check can be done in free tools, with different matching rules. The two below are documented by their own projects.
| Tool | Function | What the official documentation says |
|---|---|---|
| Python (pandas) | DataFrame.compare |
Compares two DataFrames element-wise and returns a DataFrame highlighting the differences. It can only compare identically-labeled DataFrames (same shape, identical row and column labels), and matching NaNs do not appear as a difference. |
| R (base) | all.equal |
Tests near equality of two R objects, with a default numeric tolerance of sqrt(.Machine$double.eps), close to 1.5e-8. It returns TRUE or a character description of the differences, so wrap it in isTRUE() inside if conditions. |
FAQ
What does PROC COMPARE do in SAS?
It compares a base data set with a comparison data set and reports differences in structure, observations and values, writing results to the log, the SYSINFO macro variable, printed output and optional output data sets.
How do I compare two datasets in SAS with PROC COMPARE?
Use proc compare base=dataset1 compare=dataset2; run;, adding an ID statement for the key variable. The output is described above.
How do I compare only some variables?
List them in a VAR statement, and add WITH to pair them with differently named variables in the comparison data set.
How do I know programmatically whether two data sets matched?
Check &sysinfo immediately after the step; the value is the sum of the condition codes in the table above.
Sources (official documentation)
- SAS Institute, SAS 9.4 COMPARE procedure, Results (accessed 2026-10-29): support.sas.com, COMPARE Procedure, Results
- SAS Institute, COMPARE procedure overview (SAS 9.1.3-era documentation, accessed 2026-10-29): support.sas.com, COMPARE Procedure overview
- pandas documentation, pandas.DataFrame.compare (accessed 2026-10-29)
- R Core documentation, all.equal (accessed 2026-10-29)
Related reading: SPSS statistical analysis software and JASP statistical software for other ways to work with analysis data.








