BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jenim514
Pyrite | Level 9

Hi!

 

I have a race text variable that can have multiple values for race:

 

RACE

Asian, White

White

Black, White

Asian, Black

 

How can I use a find all occurrences of say, Asian, and assign a numeric value?  I know it is not using 'contains' but this is an example of what I'm trying to do.  Maybe index?

 

data want;

set have;

if c then do;
ord=1;
if race contains ('White') then sord=6;
else if race contains ('Black') then sord=4;
else if race contains ('Asian') then sord=3;

end;

run;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Yes, INDEX is a good tool for the job:

 

if index(race, 'White') then ...

 

However, note that you can only have one value for SORD per observation.  So your logic is selecting which RACE value takes priority.

View solution in original post

2 REPLIES 2
Astounding
PROC Star

Yes, INDEX is a good tool for the job:

 

if index(race, 'White') then ...

 

However, note that you can only have one value for SORD per observation.  So your logic is selecting which RACE value takes priority.

ballardw
Super User

And perhaps anything with a comma could/should be treated as "more than one race"?

 

For one project where I have stuff like this I actually create a series of dichotomous variables such as

 

rw = index(Race,'White')>0;

rb = index(Race,'Black')>0;

ra = index(Race,'Asian')>0;

/* and for those who think Hispanic is a race*/

rh = index(Race,'Hispanic)>0;

 

Because I have to report on multiracial, those that are only one race or that report combinations.

sums or max of multiple variables are then easy ways to find the specific examples.

HB = sum(rb,rh)=2; for instance creates a dichotomous variable indicating Black Hispanics. Which is much easier once you get used to it than: IF rb and rh then HB=1; else HB=0;

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 2 replies
  • 2218 views
  • 0 likes
  • 3 in conversation