BookmarkSubscribeRSS Feed
wubsy
Calcite | Level 5

Hello, I am looking for some help trying to match text between variables in the same row. Essentially, I am looking to see if a text string in one variable matches a string in several other variables. An example of my data is shown below. I want to see if the string in Var1 matches any of the strings in iVar1, iVar2, iVar3 with a flag variable by row. Is there a way to do this? It seems like it should be easy but I can't figure it out. Any help would be much appreciated, thank you for your time!

 

 ID     Var1       Var2     Var3       iVar1      iVar2     iVar3        var1_flag

1      Charlie    Bravo    Alpha     Bravo     Alpha    Charlie          Y 

2      Alpha                   Charlie   Alpha     Charlie                        Y

3      Bravo                                 Charlie                                      N

4     Charlie     Bravo                  Bravo     Alpha    Charlie          Y

5      Alpha                                                                              N

2 REPLIES 2
Reeza
Super User

You need to use two arrays here with the WHICHC function.

 

Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/

 

data want;
set have;

var1_flag = whichc(var1, of iVar1-iVar3);

array _words(*) var1-var3;
arrays _search(*) ivar1-ivar3;
arrays _flags(*) var_flag1-var_flag3;

do i=1 to dim(_words);
_flags(i) = whichc(_words(i), of _search(*));
end;

run;

@wubsy wrote:

Hello, I am looking for some help trying to match text between variables in the same row. Essentially, I am looking to see if a text string in one variable matches a string in several other variables. An example of my data is shown below. I want to see if the string in Var1 matches any of the strings in iVar1, iVar2, iVar3 with a flag variable by row. Is there a way to do this? It seems like it should be easy but I can't figure it out. Any help would be much appreciated, thank you for your time!

 

 ID     Var1       Var2     Var3       iVar1      iVar2     iVar3        var1_flag

1      Charlie    Bravo    Alpha     Bravo     Alpha    Charlie          Y 

2      Alpha                   Charlie   Alpha     Charlie                        Y

3      Bravo                                 Charlie                                      N

4     Charlie     Bravo                  Bravo     Alpha    Charlie          Y

5      Alpha                                                                              N


 

Ksharp
Super User

Using 'IN' operator of array.

 

data have;
input  ID    ( Var1       Var2     Var3       iVar1      iVar2     iVar3 ) ($);   
cards;
1      Charlie    Bravo    Alpha     Bravo     Alpha    Charlie  
;

data want;
 set have;
 array x{*} $ ivar1-ivar3;
 if var1 in x then var1_flag='Y';
  else var1_flag='N';
run;