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

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    
1 ACCEPTED SOLUTION

Accepted Solutions
LinusH
Tourmaline | Level 20
If find(cat(of active1-active4),'YES') then active = 'YES';
Else active = 'NO';
Data never sleeps

View solution in original post

4 REPLIES 4
Astounding
PROC Star

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;

novinosrin
Tourmaline | Level 20
 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;
 
LinusH
Tourmaline | Level 20
If find(cat(of active1-active4),'YES') then active = 'YES';
Else active = 'NO';
Data never sleeps
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 4 replies
  • 2199 views
  • 6 likes
  • 5 in conversation