BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
nitink26
Obsidian | Level 7


data test;
a1= "aa";
a2= "bb";
a3= "cc";

b1= "bb";
b2= "bb";
b3= "cc";

/*After comparison*/
comp1= "No";
comp2= "Yes";
comp3= "Yes";
run;

What I am looking for  -

if a1 in (b1,b2,b3) the comp1 = 'Yes';
else comp1 = 'No';
as we don't find the match for a1 in any of (b1,b2,b3) then comp1= "No" and our next scenario will be

if a2 in (b1,b2,b3) the comp1 = 'Yes';
else comp2 = 'No';

now we found a match for a2 in b1 then comp2= "Yes" and will not include that column where we found the matching value in next scenario and our next scenario will be

if a3 in (b2,b3) then comp3 = 'Yes';
else comp3 = 'No';

now we found a match for a3 in b2 then comp3= "Yes" and our next scenerio will be if there are anything else

if a4 in (b3) then comp3 = 'Yes';
else comp3 = 'No';

Thanks!

 

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

If the values of the a-variables are that simple, this should work:

data want;
   set test;
   
   length compare1-compare3 $ 3;
   
   array a_vars[3] a1-a3;
   array cmp[3] compare1-compare3;
   
   cmp_string = catx('|', of b1-b3);
   
   do i = 1 to dim(a_vars);
      if findw(cmp_string, a_vars[i], '|', 't') > 0 then do;
         cmp_string = tranwrd(cmp_string, a_vars[i], ' ');
         cmp[i] = 'yes';
      end;
      else do;
         cmp[i] = 'no';
      end;
   end;
   
   drop cmp_string i;
run;

View solution in original post

1 REPLY 1
andreas_lds
Jade | Level 19

If the values of the a-variables are that simple, this should work:

data want;
   set test;
   
   length compare1-compare3 $ 3;
   
   array a_vars[3] a1-a3;
   array cmp[3] compare1-compare3;
   
   cmp_string = catx('|', of b1-b3);
   
   do i = 1 to dim(a_vars);
      if findw(cmp_string, a_vars[i], '|', 't') > 0 then do;
         cmp_string = tranwrd(cmp_string, a_vars[i], ' ');
         cmp[i] = 'yes';
      end;
      else do;
         cmp[i] = 'no';
      end;
   end;
   
   drop cmp_string i;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1 reply
  • 606 views
  • 1 like
  • 2 in conversation