This will output all observations in B that match any observation in A, which satisfies your criterion as long as neither dataset has duplicates, and A and B have the same variables.
data a b;
set sashelp.class;
if mod(_n_,3)=0 then output a b;
else if mod(_n_,3)=1 then output a;
else output b;
run;
data both;
set b;
if _n_=1 then do;
declare hash ha (dataset:'a');
ha.definekey(all:'Y');
ha.definedone();
end;
if ha.find()=0;
run;