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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1260 views
  • 0 likes
  • 4 in conversation