Contributor
Posts: 25

# Creation of categorical variables

Is it possible to take three dummy variables and recode them into one categorical variable with 4 different categories?

Super User
Posts: 13,563

## Re: Creation of categorical variables

Yes.

Simple example is 3 variables coded 1 for yes (the dummy value of interest).

ina a data step

data want;

set have;

If var1=1 then Cat=1;

else if var2=1 then Cat=2;

else if var3=1 the cat=3;

else Cat=4;
run;

Posts: 1,147

## Re: Creation of categorical variables

From the above code, since we are using the same cat variable for categorizing the var1,var2,var3 data, the final output will show the cat=3. per the code everytime the if then else condition executes and if the var1,var2 and var3 values are same , the cat value will be replaced or overwritten with 1, 2 and 3. but the final output will show 3.

the above code will work only when the three variables will have different values per row.

Thanks,

jag

Thanks,
Jag
Contributor
Posts: 25

## Re: Creation of categorical variables

Is there any way to code it so the final output does not show all 3's? I would like each category to display a unique value.

Posts: 5,532

## Re: Creation of categorical variables

You could use

cat = whichn(1, var1, var2, var3);

PG

PG
Super User
Posts: 13,563