SAS (Base SAS)+
data SEX;
infile datalines dlm='|' dsd missover;
input Name : $8. Sex : $1.;
label ;
format ;
datalines4;
Alfred|M
Henry|M
Mary|F
;;;;
run;
data AHW;
infile datalines dlm='|' dsd missover;
input Name : $8. Age : best32. Height : best32. Weight : best32.;
label ;
format ;
datalines4;
Alfred|14|69|112.5
Henry|14|63.5|102.5
James|12|57.3|83
;;;;
run;
proc sort data=sex;
by name;
run;
proc sort data=ahw;
by name;
run;
data unmatched;
merge sex(in=a) ahw(in=b keep=name);
by name;
if a and not b;
run;
data unmatched;
merge sex(in=a keep=name) ahw(in=b);
by name;
if b and not a;
run;
infile datalines dlm='|' dsd missover;
input Name : $8. Sex : $1.;
label ;
format ;
datalines4;
Alfred|M
Henry|M
Mary|F
;;;;
run;
data AHW;
infile datalines dlm='|' dsd missover;
input Name : $8. Age : best32. Height : best32. Weight : best32.;
label ;
format ;
datalines4;
Alfred|14|69|112.5
Henry|14|63.5|102.5
James|12|57.3|83
;;;;
run;
proc sort data=sex;
by name;
run;
proc sort data=ahw;
by name;
run;
data unmatched;
merge sex(in=a) ahw(in=b keep=name);
by name;
if a and not b;
run;
data unmatched;
merge sex(in=a keep=name) ahw(in=b);
by name;
if b and not a;
run;
- To find unmatched records in SAS, we combine a MERGE with the
in=dataset option to track which inputs contributed each row. merge sex(in=a) ahw(in=b); by name;creates boolean flagsaandbthat are 1 when the current BY group came from that dataset.- Adding
if a and not b;keeps only rows present insexbut absent fromahw. Reversing the flags finds the opposite mismatch. - As with any BY merge, both inputs must be sorted on the key first.
- The result is a dataset containing only the unmatched rows from the requested side.