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

 

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

PersonIDA1A2B1B2
1TRUEFALSETRUEFALSE
2FALSETRUEFALSETRUE
3TRUEFALSETRUETRUE
4TRUEFALSEFALSEFALSE
5FALSETRUEFALSETRUE

 

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

PersonIDA1A2B1B2New_ANew_B
1TRUEFALSETRUEFALSEyesno
2FALSETRUEFALSETRUEnoyes
3TRUEFALSETRUETRUEyesyes
4TRUEFALSEFALSEFALSEyesno
5FALSETRUEFALSETRUEnoyes

 

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

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
/* 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;
--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26
/* 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;
--
Paige Miller
kempfz
Calcite | Level 5

Thanks! It works great, much appreciated!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1257 views
  • 0 likes
  • 2 in conversation