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
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
Agent | EMPLOYEE_NAME | Active1 | Active2 | Active3 | Active4 | Active |
AAA | A | YES | YES | Yes | YES | YES |
BBB | B | YES | NO | NO | NO | YES |
CCC | A | Yes | Yes | NO | NO | YES |
DDD | B | Yes | NO | YES | ||
EEE | A | YES | YES | YES | ||
FFF | B | YES | NO | YES | YES | |
GGG | A | NO | ||||
HHH | B | YES | YES | |||
III | A | NO | 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
Agent | EMPLOYEE_NAME | Active1 | Active2 | Active3 | |
AAA | A | YES | YES | Yes | YES |
BBB | B | YES | NO | NO | NO |
CCC | A | Yes | Yes | NO | NO |
DDD | B | Yes | NO | ||
EEE | A | YES | YES | ||
FFF | B | YES | NO | YES | |
GGG | A | ||||
HHH | B | YES | |||
III | A |
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;
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;
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
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.
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.