BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SIgnificatif
Quartz | Level 8

Hi Dear All.

I have this working code code :

data have;
re="all";
ro="hello";
ra=catx('',ro,re);
texto='blablabla hello all, people';
if index(texto,(trim(ra)))>0 then do;
bi="wee";
end;
run;


I would like to transorm the re variable to a list , so index could find the string if one of the values of the list 're' is found, here is an example 

data have;
re="all|you";  /* will find if word 'all' or 'you' is found the text 'texto' below , text is delimited with | */
ro="hello";
ra=catx('',ro,re);
texto='blablabla hello all, people';   /* <= instead of word all , the text can contain the word you*/
if index(texto,(trim(ra)))>0 then do;
bi="wee";
end;
run;

Thank you for your response.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

Using a array.

 

data have;
re="all|you";  
ro="hello";
ra=catx('',ro,re);
texto='blablabla hello all, people';   /* <= instead of word all , the text can contain the word you*/


do i=1 to countw(re,'|');
temp=scan(re,i,'|');
if index(texto,(trim(temp)))>0 then do;
bi="wee";leave;
end;
end;

run;


proc print;run;

View solution in original post

7 REPLIES 7
gamotte
Rhodochrosite | Level 12

Hello,

 

data have;
re="\b(all|you)\b";  /* will find if word 'all' or 'you' is found the text 'texto' below , text is delimited with | */
ro="hello";
ra=catx('',ro,re);
texto='blablabla hello all, people';   /* <= instead of word all , the text can contain the word you*/
if prxmatch(cats("/",ra,"/"),texto) then do;
    bi="wee";
end;
run;
SIgnificatif
Quartz | Level 8

Thank you for the response, but I would like to find the answer using sas code and not re.
(* sorry, since i forgot to mention that 😞 )

gamotte
Rhodochrosite | Level 12

prxmatch is sas code but i understand you don't want to use regular expressions (why ?).

 

Here is a solution without regex. Note that it will match the string "hello young fellow" for instance.

 

data have;
re="all|you"; 
ro="hello";

texto='blablabla hello all, people';

do i=1 to countw(re,'|');
	re2=scan(re,i,'|');
	ra=catx('',ro,re2);

	if index(texto, trim(ra)) then do;
	    bi="wee";
	end;
end;
run;
PGStats
Opal | Level 21

Why not Regular Expressions? They are your best bet for this task.

PG
SIgnificatif
Quartz | Level 8

I think that they are more expensive in terms of cpu usage.... unfortunately I did not find any article that compares re to sas functions ...

Ksharp
Super User

Using a array.

 

data have;
re="all|you";  
ro="hello";
ra=catx('',ro,re);
texto='blablabla hello all, people';   /* <= instead of word all , the text can contain the word you*/


do i=1 to countw(re,'|');
temp=scan(re,i,'|');
if index(texto,(trim(temp)))>0 then do;
bi="wee";leave;
end;
end;

run;


proc print;run;
SIgnificatif
Quartz | Level 8

Thank you Dear 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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