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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1831 views
  • 0 likes
  • 4 in conversation