Help using Base SAS procedures

Create a new variable based on two variables

Accepted Solution Solved
Reply
Contributor
Posts: 21
Accepted Solution

Create a new variable based on two variables

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?


Accepted Solutions
Solution
‎01-08-2015 02:29 PM
Super Contributor
Posts: 490

Re: Create a new variable based on two variables

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


All Replies
Super Contributor
Posts: 490

Re: Create a new variable based on two variables

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;

Contributor
Posts: 21

Re: Create a new variable based on two variables

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

Solution
‎01-08-2015 02:29 PM
Super Contributor
Posts: 490

Re: Create a new variable based on two variables

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;

Contributor
Posts: 21

Re: Create a new variable based on two variables

This worked. Thanks a lot Mohamed.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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