BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SannaSanna
Quartz | Level 8
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
1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

4 REPLIES 4
PGStats
Opal | Level 21

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
Ksharp
Super User

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

PGStats
Opal | Level 21

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
SannaSanna
Quartz | Level 8

It works perfectly!!!

Ohh Thank you Thank you Thank you!!!

sas-innovate-2024.png

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.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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