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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;

View solution in original post

3 REPLIES 3
mkeintz
PROC Star

 

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';

  

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Reeza
Super User

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;

TMSmith
Calcite | Level 5

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!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 826 views
  • 0 likes
  • 3 in conversation