BookmarkSubscribeRSS Feed
Mdormond
Calcite | Level 5

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

5 REPLIES 5
ballardw
Super User

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;

Jagadishkatam
Amethyst | Level 16

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
Mdormond
Calcite | Level 5

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.

PGStats
Opal | Level 21

You could use

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

PG

PG
ballardw
Super User

Since you didn't provide any example of your dummy variables or data layout I had to guess. If multiples of your dummy variables are 'Yes' then you need to describe the rules for which result you want. If the dummy variables are coded 0/1 and ONLY one is set to yest the code will work as described. Incomplete descriptions=incomplete answers.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 534 views
  • 0 likes
  • 4 in conversation