@CP2,
As long as you are forcing yourself to learn macro language, here are some tools to consider. I'll give you the short tour.
You are starting with these macro variables:
%let a = gender, age ;
%let b= age height weight gender ;
%let b2= "unk1" "unk2" "unk3" "unk4";
From that, you need to add calculations to the DATA step:
data want;
set have;
if age = '' then age="unk1" ;
if gender = "" then gender="unk4" ;
run;
The programming was complex, but ultimately you found a solution.
If you had started with a different set of macro variables, the task would be straightforward. Starting with:
%let a = gender age ;
%let age = "unk1";
%let height = "unk2";
%let weight = "unk3";
%let gender = "unk4";
From that starting point, the solution is easier:
data want;
set have;
%do i=1 %to %sysfunc(countw(&a));
%let varname = &scan(&a, &i);
if &varname = ' ' then &varname = &&&varname;
%end;
run;
If the programming would be that easy, it must be worthwhile to investigate ... how do you take your original set of macro variables and create the second set from them?
In most cases, getting commas out of macro variables can only help. The simplest way to do that is in a DATA step:
data _null_;
call symputx('a', translate("&a", ' ', ','));
run;
The other transformations can be handled by macro language:
%do i=1 %to %sysfunc(countw(&b));
%let &scan(&b, &i) = %scan(&b2, &i);
%end;
Done, and ready to use in the relatively simple solution.
That's the quick tour (omitting %local statements).