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;
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.
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.
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;
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 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.