Help using Base SAS procedures

Race Id

Accepted Solution Solved
Reply
Contributor
Posts: 57
Accepted Solution

Race Id

Sorry- starting new thread but want to reference the discussion with Race Loop(below).   I was searching this website in hopes there was an answer already to my question.  Ok---Trying to assign PrimaryRace based on race1-race5.  My struggle involves data in the last two rows.  I need the Primary Race to be Filipino if anything in columns Race1-Race5 includes Asian. To add complexity- the Primary Race needs to be Mixed if one of the race is Filipino and anything else in Race1-Race5 is non-Asian.   Appreciate the help!
patientrace1race2race3race4race5PrimaryRace
2005blwekmissingaaaaasian
2012rhiolamissingbmissingamixed
1989lliokmissingbmissingmissingwmixed
2000ijlliwwwwwwhite
abcdefafmissingmissingmissingfilipino
abcdefga fwmissingmissingmixed

Accepted Solutions
Solution
‎05-07-2015 10:59 PM
Respected Advisor
Posts: 4,646

Re: Race Id

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

PG

View solution in original post


All Replies
Respected Advisor
Posts: 4,646

Re: Race Id

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

PG
Super User
Posts: 9,676

Re: Race Id

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

Solution
‎05-07-2015 10:59 PM
Respected Advisor
Posts: 4,646

Re: Race Id

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

PG
Contributor
Posts: 57

Re: Race Id

It works perfectly!!!

Ohh Thank you Thank you Thank you!!!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 311 views
  • 0 likes
  • 3 in conversation