proc compare base=HT_ORIG_2017_subset compare=HT_NEW_2017_subset out=check outnoequal outdif outstats=diffstat noprint;
id CIHI_KEY;
var HOMETIME AWAYTIME ;
with HOMETIME AWAYTIME;
title 'Comparison of Variables in Different Data Sets';
run;
I get the check dataset with 34 obs – are these 34 obs the only obs that have a different value for the 2 var variables specified in the var statement in the 2 datasets?
And the diffstat has the stats produces for the 2 var variables for the 678 obs in the 2 datasets specified in main and compare
How would I instead produce the same stats for the 34 obs ?
data data1_dif;
merge check(where=(base) in=a) data1;
by keyvars;
if a;
run;
/*pick the different obs*/
data data2_dif;
merge check(where=(base) in=a) data2;
by keyvars;
if a;
run;
/*compare indirectly*/
proc compare b=data1_dif c=data2_dif OUT=OUTCOMP LISTALL OUTBASE OUTCOMP OUTDIFF OUTNOEQUAL OUTSTATS=diffstat;
run;
/*1. first compare directly*/
proc compare base=data1 compare=data2 OUT=OUTCOMP1 LISTALL OUTBASE OUTCOMP OUTDIFF OUTNOEQUAL OUTSTATS=diffstat;
id CIHI_KEY;
var HOMETIME AWAYTIME ;
with HOMETIME AWAYTIME; /*can drop this line since variable name is same as var statements*/
run;
/*pick the different obs*/
/*keyvars: CIHI_KEY, to distinguish each unique record in your datasets */
data data1_dif;
merge OUTCOMP1 (where=(base) in=a) data1;
by keyvars;
if a;
run;
/*pick the different obs*/
data data2_dif;
merge OUTCOMP1 (where=(base) in=a) data2;
by keyvars;
if a;
run;
/*2. second compare indirectly*/
proc compare b=data1_dif c=data2_dif OUT=OUTCOMP2 LISTALL OUTBASE OUTCOMP OUTDIFF OUTNOEQUAL OUTSTATS=diffstat;
id CIHI_KEY;
var HOMETIME AWAYTIME ;
run;
Yes, it is better if you can upload sample datasets to testthe code
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.