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 !
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?
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.
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;
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.