Hello SAS Users,
I'm relatively new to SAS so I'm looking for some advice on how to compare values between certain variables. I have a sample dataset (named Test) that looks like this:
Test
PersonID | A1 | A2 | B1 | B2 |
1 | TRUE | FALSE | TRUE | FALSE |
2 | FALSE | TRUE | FALSE | TRUE |
3 | TRUE | FALSE | TRUE | TRUE |
4 | TRUE | FALSE | FALSE | FALSE |
5 | FALSE | TRUE | FALSE | TRUE |
I am needing to compare A1 to B1, followed by A2 to B2. What I want is to look and see if there is a least one "TRUE" between them and then create new variables for each comparison ("New_A", "New_B") that indicates at least one of the variables contains "TRUE". The new dataset (named Test2) I want to create will look something like this:
Test2
PersonID | A1 | A2 | B1 | B2 | New_A | New_B |
1 | TRUE | FALSE | TRUE | FALSE | yes | no |
2 | FALSE | TRUE | FALSE | TRUE | no | yes |
3 | TRUE | FALSE | TRUE | TRUE | yes | yes |
4 | TRUE | FALSE | FALSE | FALSE | yes | no |
5 | FALSE | TRUE | FALSE | TRUE | no | yes |
I already have written code that does this (the example below just shows for New_A but it works for New_B also):
data test2;
set test;
if A1 = "TRUE" and B1 = "TRUE"
then New_A = "yes";
else if A1 = "TRUE" or B1 = "TRUE"
then New_A = "yes";
else New_A = "no";
run;
The problem I'm running into is that in the actual dataset that I will eventually have to use, there is about 150 comparisons that I will have to do and I'm not really wanting to type them all out with "if-then-else" statements. I'm not too familiar with arrays or macros yet but I was wondering if they might make this process more efficient for me. Would anyone know of a faster way that I could do these comparisons? Any help/advice would be great!
P.S. I am using SAS EG 7.1
/* UNTESTED CODE */
data want;
set test;
array a a1-a150;
array b b1-b150;
array new $ 3 new1-new150;
do i=1 to dim(a);
if a(i)="TRUE" or b(i)="TRUE" then new(i)='yes';
else new(i)='no';
end;
drop i;
run;
/* UNTESTED CODE */
data want;
set test;
array a a1-a150;
array b b1-b150;
array new $ 3 new1-new150;
do i=1 to dim(a);
if a(i)="TRUE" or b(i)="TRUE" then new(i)='yes';
else new(i)='no';
end;
drop i;
run;
Thanks! It works great, much appreciated!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.