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
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.