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

I want to create a new variable( RaceEthnicity)  based on these two variables (Race and Ethnicity):

RaceEthnicity

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?

1 ACCEPTED SOLUTION

Accepted Solutions
mohamed_zaki
Barite | Level 11

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;

View solution in original post

4 REPLIES 4
mohamed_zaki
Barite | Level 11

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;

bikash
Calcite | Level 5

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

mohamed_zaki
Barite | Level 11

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;

bikash
Calcite | Level 5

This worked. Thanks a lot Mohamed.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 7690 views
  • 0 likes
  • 2 in conversation