patient | race1 | race2 | race3 | race4 | race5 | PrimaryRace |
2005blwek | missing | a | a | a | a | asian |
2012rhiol | a | missing | b | missing | a | mixed |
1989lliok | missing | b | missing | missing | w | mixed |
2000ijlli | w | w | w | w | w | white |
abcdef | a | f | missing | missing | missing | filipino |
abcdefg | a | f | w | missing | missing | mixed |
The simpler the better. I think this is simpler...
data have;
length patient $10 race1-race5 $1;
input patient race1-race5;
datalines;
2005blwek missing a a a a
2012rhiol a missing b missing a
1989lliok missing b missing missing w
2000ijlli w w w w w
abcdef a f missing missing missing
abcdefg a f w missing missing
;
data want;
set have;
array r{*} race:;
length pr $1;
pr = r{1};
do i = 2 to dim(r);
if pr ne "x" and r{i} ne "m" then
if pr = "m" then pr = r{i};
else if pr = "a" and r{i} = "f" then pr = "f";
else if pr = "f" and r{i} = "a" then;
else if pr ne r{i} then pr = "x";
end;
drop i;
run;
proc print data=want noobs; run;
PG
If races are really coded with single letters, you could do :
data have;
length patient $10 race1-race5 $1;
input patient race1-race5;
datalines;
2005blwek missing a a a a
2012rhiol a missing b missing a
1989lliok missing b missing missing w
2000ijlli w w w w w
abcdef a f missing missing missing
abcdefg a f w missing missing
;
proc transpose data=have out=temp(where=(col1 ne "m")) name=race;
by patient notsorted;
var race:;
run;
proc sort data=temp nodupkey; by patient col1; run;
data pr;
length r $8 pr $1;
do until(last.patient);
set temp; by patient;
r = cats(r, col1);
end;
select;
when(length(r) < 2) pr = r;
when(r = "af") pr = "f";
otherwise pr = "x"; /* x stands for mixed */
end;
keep patient pr;
run;
proc print data=pr noobs; var patient pr; run;
PG
What is non-Asian ? Can you enumerate it ?
data have; length patient $10 race1-race5 $1; input patient race1-race5; datalines; 2005blwek . a a a a 2012rhiol a . b . a 1989lliok . b . . w 2000ijlli w w w w w abcdef a f . . . abcdefg a f w . . ; run; data want; set have; array x{*} $ race:; a=0;b=0;w=0;f=0; if 'a' in x then a=1; if 'b' in x then b=1; if 'w' in x then w=1; if 'f' in x then f=1; length PrimaryRace $ 10; select; when(a=1 and b=0 and w=0 and f=0) PrimaryRace='asian'; when(a=0 and b=1 and w=0 and f=0) PrimaryRace='b'; when(a=0 and b=0 and w=1 and f=0) PrimaryRace='white'; when(a=0 and b=0 and w=0 and f=1) PrimaryRace='filipino'; when(a=1 and b=0 and w=0 and f=1) PrimaryRace='filipino'; otherwise PrimaryRace='mixed'; end; drop a b w f; run;
Xia Keshan
The simpler the better. I think this is simpler...
data have;
length patient $10 race1-race5 $1;
input patient race1-race5;
datalines;
2005blwek missing a a a a
2012rhiol a missing b missing a
1989lliok missing b missing missing w
2000ijlli w w w w w
abcdef a f missing missing missing
abcdefg a f w missing missing
;
data want;
set have;
array r{*} race:;
length pr $1;
pr = r{1};
do i = 2 to dim(r);
if pr ne "x" and r{i} ne "m" then
if pr = "m" then pr = r{i};
else if pr = "a" and r{i} = "f" then pr = "f";
else if pr = "f" and r{i} = "a" then;
else if pr ne r{i} then pr = "x";
end;
drop i;
run;
proc print data=want noobs; run;
PG
It works perfectly!!!
Ohh Thank you Thank you Thank you!!!
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.