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?
Use the transformation
diet = char("ABCX", whichn(1, A, B, C, 1));
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;
Use the transformation
diet = char("ABCX", whichn(1, A, B, C, 1));
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
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.