OK, a brand new program and a brand new approach:
proc sort data=dataset1;
by gender ageclass;
run;
proc sort data=dataset2;
by gender ageclass;
run;
data dataset1_revised;
set dataset1;
drop subset;
rename code = assigned_code;
output;
output;
run;
data want;
in1=0;
in2=0;
merge dataset1_revised (in=in1) dataset2 (in=in2);
by gender ageclass;
if in2;
if in1=0 then assigned_code = ' ';
run;
Doing a many-to-many merge on purpose, after expanding DATASET1 so it has double the number of observations.