You'll have to excuse me if this is a really basic question, but I'm trying to make a new variable composed of several other dummy coded variables. The original variables are numeric, coded 1 for yes and 0 for no. I'm trying to convert them to a new text variable that indicates their meaning so I can report descriptive statistics.
DATA eggs;
set eggs;
length EggColor $50 EggRed 5.0 EggOrange 5.0 EggYellow 5.0 EggGrn 5.0 EggBlu 5.0 EggPur 5.0
StripedEgg 5.0 EggDK 5.0 EggO 5.0;
IF EggRed=1 THEN EggColor='Red';
ELSE IF EggRed=0 OR EggRed=99 THEN EggColor='N/A';
IF EggOrange=1 THEN EggColor='Orange';
ELSE EggColor='N/A';
IF EggYellow=1 THEN EggColor='Yellow';
ELSE EggColor='N/A';
IF EggGrn=1 THEN EggColor='Green';
ELSE EggColor='N/A';
IF EggBlu=1 THEN EggColor='Blue';
ELSE EggColor='N/A';
IF EggPur=88 THEN EggColor='Other Color';
ELSE EggColor='N/A';
IF Striped=1 THEN EggColor='Striped';
ELSE EggColor='N/A';
IF EggDK=1 THEN EggColor='Do not know color';
ELSE EggColor='N/A';
The problem is when I recode to EggsColor, all of the observations are in N/A (or '.' when I use that for my else statements) I feel like I'm missing something really basic. Is it obvious to anyone else?
All your IF statements execute, but they overwrite each other because of the way you've structured it.
Since your last line is ELSE if it doesn't match it will default everything to N/A.
IF EggDK=1 THEN EggColor='Do not know color';
ELSE EggColor='N/A'; -> this is the last line and only one that works;
You can do the following instead and it should be slightly faster since you're no longer evaluating every IF condition.
IF EggRed=1 THEN EggColor='Red';
ELSE IF EggRed=0 OR EggRed=99 THEN EggColor='N/A';
ELSE IF EggOrange=1 THEN EggColor='Orange';
ELSE IF EggYellow=1 THEN EggColor='Yellow';
.... rest of your code.....
ELSE IF EggDK=1 THEN EggColor='Do not know color';
ELSE EggColor='N/A';
You could also try something different, whereby you find the variable with a 1 and get the colour from the variable name.
array _colours(*) egg: ; *list of all egg colour variables;
index_found = whichc(1, of _colours(*));
name = vname(_colours(index_found));
*then extract the colour from the name variable;
I pesume only one of the dummy vars is 1. Let's say eggred=1 and all the others are zero. Then your first IF test is true, setting eggcolor to "RED". But your subsequent IF tests are still being executed, thereby overriding the EGGCOLOR value as 'N/A';
Consider this structure
if dummy1=1 then outcome='pink';
else if dummy2=1 then outcome='black';
else if ...
...
else outcome='N/A';
All your IF statements execute, but they overwrite each other because of the way you've structured it.
Since your last line is ELSE if it doesn't match it will default everything to N/A.
IF EggDK=1 THEN EggColor='Do not know color';
ELSE EggColor='N/A'; -> this is the last line and only one that works;
You can do the following instead and it should be slightly faster since you're no longer evaluating every IF condition.
IF EggRed=1 THEN EggColor='Red';
ELSE IF EggRed=0 OR EggRed=99 THEN EggColor='N/A';
ELSE IF EggOrange=1 THEN EggColor='Orange';
ELSE IF EggYellow=1 THEN EggColor='Yellow';
.... rest of your code.....
ELSE IF EggDK=1 THEN EggColor='Do not know color';
ELSE EggColor='N/A';
You could also try something different, whereby you find the variable with a 1 and get the colour from the variable name.
array _colours(*) egg: ; *list of all egg colour variables;
index_found = whichc(1, of _colours(*));
name = vname(_colours(index_found));
*then extract the colour from the name variable;
Ahhh, okay. I didn't realize I was overwriting my code by structuring it like that. I restructured based on that and it worked. Thank you!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.