Take advantage of using the IN operator with an array name to simplify the direct approach.
data have;
input (Subject old_var1-old_var4 new_var1-new_var4) ($);
cards;
1 A C B . C D . .
2 X Z . . . . . .
3 C D X . X C D .
;
data want;
set have;
array old old_var: new_var: ;
array combine[8] $8 ;
do i=1 to dim(old);
if not missing(old[i]) then do;
if old[i] not in combine then do;
j=sum(j,1);
combine[j]=old[i];
end;
end;
end;
drop i j;
run;
Result
... View more