assuming n is unique in the 2nd table
- data a;
- do id=1 to 10000;
- array n[6];
- do _n_=1 to dim(n);
- n[_n_] = ceil(20000*ranuni(12345));
- end;
- output;
- end;
- stop;
- data b;
- do _n_=1 to 10000;
- n = ceil(20000*ranuni(54321));
- output;
- end;
- stop;
- proc sort data=b nodupkey;
- by n;
- proc sql;
- create table c as
- select a.*, b.n
- from a left join b
- on (b.n-a.n1)*(b.n-a.n2)*(b.n-a.n3)*(b.n-a.n4)*(b.n-a.n5)*(b.n-a.n6)=0;
- quit;
- data d;
- set a;
- if _n_=1 then do;
- if 0 then set b;
- declare hash _ht(dataset:'b', hashexp:16);
- _ht.definekey('n');
- _ht.definedata('n');
- _ht.definedone();
- end;
- array _x[*] n1-n6;
- array _y[6] _n1-_n6;
- found = 0;
- do i=1 to dim(_x);
- if _ht.find(key: _x[i])=0 then do;
- found=found+1;
- _y[found] = n;
- end;
- end;
- if found=0 then do;
- call missing(n);
- output;
- end;
- else do;
- n = _y[1];
- output;
- do i=2 to found;
- if index(peekclong(addrlong(_y[1]), 8*(i-1)), put(_y[i], rb8.)) = 0 then do;
- n = _y[i];
- output;
- end;
- end;
- end;
- drop i found _n1-_n6;
- run;
- proc sort data=c;
- by id n;
- proc sort data=d;
- by id n;
- proc compare base=c(keep=id n) compare=d(keep=id n);
- run;
复制代码The hash object in the big data step can be replaced with a format.
Alternatively, not quite as efficient but acceptable
- proc sql;
- create table e as
- select a.*, b1.n as _n1, b2.n as _n2, b3.n as _n3, b4.n as _n4, b5.n as _n5, b6.n as _n6
- from a left join b as b1 on a.n1=b1.n
- left join b as b2 on a.n2=b2.n
- left join b as b3 on a.n3=b3.n
- left join b as b4 on a.n4=b4.n
- left join b as b5 on a.n5=b5.n
- left join b as b6 on a.n6=b6.n
- ;
- quit;
- data e;
- set e;
- array _y[*] _n1-_n6;
- if n(of _y[*])=0 then output;
- else do _n_=1 to dim(_y);
- if _y[_n_] ne . then do;
- n = _y[_n_];
- output;
- end;
- end;
- drop _n1-_n6;
- proc sort data=e nodupkey;
- by id n;
- proc compare base=c compare=e;
- run;
复制代码Tweak the code above if n is not unique