DATA Step, Macro, Functions and more

SUBSTRING check

Reply
Frequent Contributor
Frequent Contributor
Posts: 76

SUBSTRING check

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 !

 

 

 

 

Super User
Posts: 17,837

Re: SUBSTRING check

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?

Frequent Contributor
Frequent Contributor
Posts: 76

Re: SUBSTRING check

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 !
Super User
Posts: 17,837

Re: SUBSTRING check

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.

Trusted Advisor
Posts: 1,204

Re: SUBSTRING check

[ Edited ]

 

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;
Respected Advisor
Posts: 4,650

Re: SUBSTRING check

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
Ask a Question
Discussion stats
  • 5 replies
  • 287 views
  • 0 likes
  • 4 in conversation