Hi Guys
Here I am trying to compare dataset are identical or not in a library using %sysinfo option but below code not give correct result
data first;
set sashelp.class;
run;
data second;
set sashelp.cars;
run;
data check_dsn;
if &sysinfo.=1 then put "The two data sets are not identical";
else put "The two data sets are identical ";
run;
From the documentation of SYSINFO:
SYSINFO Automatic Macro Variable
Contains return codes provided by some SAS procedures.
Type: Automatic macro variable (read only) Details
Values of SYSINFO are described with the procedures that use it. You can use the value of SYSINFO as a condition for determining further action to take or parts of a SAS program to execute.
For example, PROC COMPARE, which compares two data sets, uses SYSINFO to store a value that provides information about the result of the comparison.
(emphasis added)
Two separate data steps do not compare anything.
If you're comparing two different data sets, you have to make sure they share something in common like an ID.
data first;
set sashelp.class;
run;
data second;
set sashelp.cars;
run;
proc compare
data = first
compare = second;
run;
Look into PROC COMPARE -- it's a SAS procedure that does exactly what you want without having to code it. Note that I just did a raw compare of both files -- it found one variable in common (weight), but it has no substantive significance (comparing car weights to school childrens' weights).
There are many statements in PROC COMPARE that allow you to select variables, identify observations, etc. You can create datasets from the PROC COMPARE procedure too.
There are many ways to do this, but just go with what SAS has already done for you.
To make use of SYSINFO you first need to run PROC COMPARE then immediately following you can check it:
proc compare base = base
comp = compare
;
run;
%let rc = &sysinfo;
data _null_;
%* Test for data set label ;
if &rc = '1'b then
put "<<<< &table.: Data sets have different labels";
%* Test for data set types ;
if &rc = '1.'b then
put "<<<< &table.: Data set types differ";
%* Test for label ;
if &rc = '1.....'b then
put "<<<< &table.: Variable has different label";
%* Test for base observation ;
if &rc = '1......'b then
put "<<<< &table.: Base data set has observation not in comparison data set";
%* Test for length ;
if &rc = '1....'b then
put "<<<< &table.: Variable has different lengths between the base data set and the comparison data set";
%* Variable in base data set not in compare data set;
if &rc ='1..........'b then
put "<<<< &table.: Variable in base data set not found in comparison data set";
%* Comparison data set has variable not in base data set ;
if &rc = '1...........'b then
put "<<<< &table.: Comparison data set has variable not contained in the base data set";
%* Test for values ;
if &rc = '1............'b then
put "<<<< &table.: A value comparison was unequal";
%* Conflicting variable types;
if &rc ='1.............'b then
put "<<<< &table.: Conflicting variable types between the two data sets being compared";
run;
Identical structure? Identical contents? Identical contents including current record order? or some other "identical"?
Proc Compare will report on number, names and values of variables, but is sort order dependent on reporting differences in values.
You can run this code to see the possible effect of sort order:
proc sort data=sashelp.class out=work.class1; by name ; run; proc sort data=sashelp.class out=work.class2; by descending name ; run; proc compare base=work.class1 compare=work.class2; run;
You should have the SASHELP.Class data set available as part of your install. Notice that since the data set has 19 records (an odd number) the middle of the sort on a single variable with unique values like Name in this set, makes the middle record, the 10th observation the same. So you see 18 of 19 records different.
You can look at the documentation for the procedure to see what additional options may be of interest.
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/proc/n1nwxbchh5hpu1n1h28kmici2awd.htm
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.