I want to create a new variable( RaceEthnicity) based on these two variables (Race and Ethnicity):
Race | Ethnicity | |
---|---|---|
American Indian or Alaska Native Asian/Native Hawaiian or Other Pacific Islander Bi-racial Black or African American Other Unknown White | '1'='HISPANIC' '2'='NOT HISPANIC ' '9'='UNKNOWN '7'=UNKNOWN | I |
I want new variable ( RaceEthnicity) to have 5 categories based on these conditions.
If Ethnicity= '1' and Race =Any then RaceAndEthnicity = Hispanic- Any Race
If Ethnicity= '2' and Race =White then RaceAndEthnicity = White NH
If Ethnicity= '2' and Race =Black or African American then RaceAndEthnicity = Black NH
If Ethnicity= '2' and Race = Asian/Native Hawaiian or Other Pacific Islander then RaceAndEthnicity = Asian
Any other combination= Others/Unknown.
What will be best way to do it (preferably in single step)? Any suggestion will be appreciated?
Sorry my fault. It should work just check the value of Race in the condition
data want;
set have;
format RaceAndEthnicity $ 20.;
If Ethnicity= "1" then RaceAndEthnicity = "Hispanic - Any Race";
Else If Ethnicity= "2" and Race = "White" then RaceAndEthnicity = "White NH";
Else If Ethnicity= "2" and Race = "Black or African American" then RaceAndEthnicity = "Black NH";
Else If Ethnicity= "2" and Race = "Asian/Native Hawaiian or Other Pacific Islander" then RaceAndEthnicity = "Asian";
Else RaceAndEthnicity = "Others/Unknown";
run;
data want;
set have;
format RaceAndEthnicity $ 20.;
If Ethnicity= 1 then RaceAndEthnicity = "Hispanic - Any Race";
Else If Ethnicity= 2 and Race = "White" then RaceAndEthnicity = "White NH";
Else If Ethnicity= 2 and Race in ("Black", "African American") then RaceAndEthnicity = "Black NH";
Else If Ethnicity= 2 and Race = "Asian/Native Hawaiian or Other Pacific Islander" then RaceAndEthnicity = "Asian";
Else RaceAndEthnicity = "Others/Unknown";
run;
Hi Mohamed,
Code ran but I can only see 'Hispanic - Any Race' and 'Others/Unknown' in my result. Looks like only first and last line were recognized by SAS.
See log below:
NOTE: Variable Ehtnic is uninitialized.
NOTE: Variable Race is uninitialized.
NOTE: There were 64430 observations read from the data set IP.IP13.
NOTE: The data set IP.IP13TRY has 64430 observations and 177 variables.
NOTE: DATA statement used (Total process time):
real time 17.34 seconds
cpu time 1.46 seconds
Sorry my fault. It should work just check the value of Race in the condition
data want;
set have;
format RaceAndEthnicity $ 20.;
If Ethnicity= "1" then RaceAndEthnicity = "Hispanic - Any Race";
Else If Ethnicity= "2" and Race = "White" then RaceAndEthnicity = "White NH";
Else If Ethnicity= "2" and Race = "Black or African American" then RaceAndEthnicity = "Black NH";
Else If Ethnicity= "2" and Race = "Asian/Native Hawaiian or Other Pacific Islander" then RaceAndEthnicity = "Asian";
Else RaceAndEthnicity = "Others/Unknown";
run;
This worked. Thanks a lot Mohamed.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.