Kul
Kul

Reputation: 73

Using If-then statements in SAS, multiple variables

I am using the SAS-University edition. I need to create a new ethnicity variable based on existing sub ethnicity. Is there a way to select 'WHITE all' instead of specifying 'WHITE-RUSSIAN' or 'WHITE-EUROPEAN' and create the new variable.

Here is my code.

data agg1;
set agg;
if ethnicity = 'WHITE' then ethnicity1= 'white' ;
if ethnicity = 'WHITE-RUSSIAN' then ethnicity1= 'white' ;
if ethnicity = 'WHITE-EUROPEAN' then ethnicity1= 'white';
.
.
run ;

Upvotes: 1

Views: 454

Answers (2)

user2877959
user2877959

Reputation: 1792

Assuming the values of the ethnicity variable are always in the format '<main>-<sub>', you can reduce this to one line and zero if statements:

data agg1;
set add;
ethnicity1=scan(ethnicity,1,'-');
run;

Upvotes: 3

user667489
user667489

Reputation: 9569

Use a : modifier:

if ethnicity eq: 'WHITE' then ethnicity1= 'white' ;

Upvotes: 5

Related Questions