Desktop productivity for business analysts and programmers

IF Statement with multiple variables

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 129
Accepted Solution

IF Statement with multiple variables

Hi All,

 

I have data that I need to create a new variable based on the 4 variables Active1 - Active4).

 

The observations in the variables can be

  1. Blank
  2. YES
  3. NO

I need to be able to create a new variable called 'Active' that says YES if any one of the variables has YES and NO if they are a mixture of BLANK or NO. 

 Apologies I can't get the data into a test

AgentEMPLOYEE_NAMEActive1Active2Active3Active4Active
AAAAYESYESYesYESYES
BBBBYESNONONOYES
CCCAYesYesNONOYES
DDDBYesNO   YES
EEEAYESYES  YES
FFFBYESNO YESYES
GGGA    NO
HHHB YES  YES
IIIANO NO NO

This is some code I have used when only two variables exist but hoping there is a better way to code this.

 

IF Active1 = " " and Active2= " " Then
		SPTN = 'NO';
	ELSE IF  Active1 = "NO" and Active2= "NO" Then
		SPTN = 'NO';
	ELSE IF  Active1 = "NO" and Active2= "" Then
		SPTN = 'NO';
	ELSE IF  Active1 = " " and Active2= "NO" Then
		SPTN = 'NO';
	ELSE IF  Active1 = "YES" and Active2= " " Then
		SPTN = 'YES';
	ELSE IF  Active1 = " " and Active2= "YES" Then
		SPTN = 'YES';
	ELSE IF  Active1 = "YES" and Active2= "NO" Then
		SPTN = 'YES';
	ELSE IF  Active1 = "YES" and Active2= "YES" Then
		SPTN = 'YES';

Any help appreciated

 

Cheers

 

Dean

 

AgentEMPLOYEE_NAMEActive1Active2Active3 
AAAAYESYESYesYES
BBBBYESNONONO
CCCAYesYesNONO
DDDBYesNO   
EEEAYESYES  
FFFBYESNO YES
GGGA    
HHHB YES  
IIIA    

Accepted Solutions
Solution
‎05-16-2018 01:59 AM
Super User
Posts: 5,914

Re: IF Statement with multiple variables

If find(cat(of active1-active4),'YES') then active = 'YES';
Else active = 'NO';
Data never sleeps

View solution in original post


All Replies
Super User
Posts: 6,903

Re: IF Statement with multiple variables

It's sounding like this does what you want:

 

data want;

set have;

array act {4} active1-active4;

length active $ 3;

active = 'NO';

do k=1 to 4;

   if upcase(act{k}) = 'YES' then active='YES';

end;

drop k;

run;

Super User
Posts: 2,046

Re: IF Statement with multiple variables

[ Edited ]
 data want;
 set have;
 array t(*) active1-active4;
 if cmiss(of t(*))=dim(t) then active='NO';
 else if 'YES' in t or 'Yes' in t then active='YES';
 else if  'YES' not in t and 'Yes' not in t and 'NO' in t then active='NO';
 run;
 
Solution
‎05-16-2018 01:59 AM
Super User
Posts: 5,914

Re: IF Statement with multiple variables

If find(cat(of active1-active4),'YES') then active = 'YES';
Else active = 'NO';
Data never sleeps
Super User
Super User
Posts: 9,799

Re: IF Statement with multiple variables

Just 2 changes to @LinusH:

active=ifc(whichc("YES",of active1-active4),"YES","NO");

Binary choices can be simpified with ifc() or ifn(), and whichc will return a numeric at the variable where the string is found.  I have grown to love ifc/ifn Smiley Happy

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 367 views
  • 6 likes
  • 5 in conversation