BookmarkSubscribeRSS Feed
Ivy
Quartz | Level 8 Ivy
Quartz | Level 8

Hi,

 

I have two variable a  & b, I would like to check whether b string is in a string .  

 

index(a, b )  could not pick up some values, for examples:

 

data _null_ ; 

a = "Bevacizumab,Cisplatin,Pemetrexed Disodium" ;

b = "Bevacizumab,Pemetrexed Disodium" ;

c = index( a, b ) ;

put c  ;

run ;

 

Should I need to seperate the B string and then use index function or whether there is another function to check this ?

 

Thank you !

 

 

 

 

5 REPLIES 5
Reeza
Super User

You'll have to separate the strings. 

 

The INDEX() function looks for an exact match, not for each of the words. 

 

You can try PRX functions or put the words into a temporary array and use that. 

It depends on how generalized your problem is, is the B changing per row, or fixed across the data set?

Ivy
Quartz | Level 8 Ivy
Quartz | Level 8
Thank you, Reeza .

B is changing per row, some B string has one part, some has two or three parts. Maybe I need to separate them. Thank you again !
Reeza
Super User

Yes, you need to separate them. Use the scan function.

If you make the data long, you can change it to a SQL query which should be easier to code as well.

stat_sas
Ammonite | Level 13

 

data want(drop=search_string i flag);
set have;
flag=0;
length search_string $30;

do i=1 to countw(b,',');
    do search_string = scan(b,i,','); 
       if index(trim(a), trim(search_string)) then flag+1;
    end;
end;

if flag=countw(b,',') then found='b in a';
else found='b not in a';

run;
PGStats
Opal | Level 21

You couild always use regular expressions

 

data _null_; 
length pat $60;
a = "Bevacizumab,Cisplatin,Pemetrexed Disodium" ;
x = "Bevacizumab,Cisplatin,Pemetrexed Dissodium" ;
b = "Bevacizumab,Pemetrexed Disodium";
pat = prxchange("s/,/.*/", -1, b);
pat = cats("/", pat, "/i");
c = prxmatch(pat, a) > 0;
put c=;
d = prxmatch(pat, x) > 0;
put d=;
run ;

Will match if the substrings are in the same order.

PG

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 5 replies
  • 1562 views
  • 0 likes
  • 4 in conversation