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

New SAS user here!

 

In my dataset I am comparing three different diets (A,B, and C) in how they affect weight loss. Currently, my data has a column for the weightloss, followed by three columns A, B, and C. If A=1 it means the person was on diet A (columns B and C would have 0, the diets are mutually exclusive). I don't want three columns with 1s and 0s like this though. I just want one simple column called "Diet" That stores either A, B, or C. How do I do this?

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Use the transformation

 

diet = char("ABCX", whichn(1, A, B, C, 1));
PG

View solution in original post

4 REPLIES 4
Reeza
Super User
You can code them as A, B, C or 1, 2, 3.

name = whichn(1, A, B, C);

I think this would code it as 1, 2, 3. If that works for you.
r_behata
Barite | Level 11
data have;
input  a b c ;
cards;
1 0 0
0 1 0
0 0 1
0 1 0
run;

data want;
set have;
array nm a b c;
diet=' ';
do i =1 to dim(nm) while(diet eq ' ') ;
	if nm[i] > 0 then diet=vname(nm[i]);
end;

drop a b c i;
run;
PGStats
Opal | Level 21

Use the transformation

 

diet = char("ABCX", whichn(1, A, B, C, 1));
PG
DoctorShemp
Fluorite | Level 6

Thank you! this is the simplest solution that did exactly what I was trying to do.

 

Thank you to everyone else who responded as well, this community is crazy helpful

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1331 views
  • 5 likes
  • 4 in conversation