DATA Step, Macro, Functions and more

Trouble with creating new variables SAS 9.4

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Trouble with creating new variables SAS 9.4

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?


Accepted Solutions
Solution
‎10-16-2017 11:24 AM
Super User
Posts: 23,305

Re: Trouble with creating new variables SAS 9.4

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


All Replies
Trusted Advisor
Posts: 1,312

Re: Trouble with creating new variables SAS 9.4

 

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

  

Solution
‎10-16-2017 11:24 AM
Super User
Posts: 23,305

Re: Trouble with creating new variables SAS 9.4

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;

Occasional Contributor
Posts: 6

Re: Trouble with creating new variables SAS 9.4

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!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 148 views
  • 0 likes
  • 3 in conversation