This can be done in a single data step that first reads DATASET1, stores it in a hash object (lookup table) with a lookup key created by modifying X (remove leading non-digits, shorten it to the last 5 digits if necessary, and remove leading zeroes. This is followed by reading DATASET2, where X is similarly modified, and a lookup is performed to see whether it is in the hash object, from which the PRODUCT value is retrieved:
data dataset1 (label='x with 3 or 5 digits');
infile datalines missover;
input product $4. x :$20. ;
datalines;
via1
via2 003
via3 014
via4 GA4
via5 GA015
via6 319
via7 23456
via8 10101010198765
run;
data dataset2;
infile datalines ;
input name $1. x :$20. ;
datalines;
a 2
b 3
c 14
d 4
e 15
f GF319
g 23456
h 98765
run;
data want (drop=rc);;
set dataset1 (in=in1) dataset2 (in=in2);
where x^='';
if _n_=1 then do;
declare hash d1 ();
d1.definekey('x');
d1.definedata('product');
d1.definedone();
end;
x=substr(x,anydigit(x)); /*Remove leading non-digits*/
if length(x)>5 then x=substr(x,length(x)-4); /*If too long, take last 5 digits*/
do while (x=:'0'); /* Strip leading zeroes*/
x=substr(x,2);
end;
if in1=1 then d1.add();
if in2;
rc=d1.find();
run;
... View more