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

I would like to make a code to check if a string variable includes any of multiple words (e.g., any of "apple" or "orange"). Can I simplify the below code because my list of target words is more than ten (apple, orange, blueberry, etc.)? The variable, NAME, can include some strings including "orange". An example is "two apples."  In this case, I would like to give 1 to the fruit variable.

 

 

if 	index(lowcase(name), "apple") or index(lowcase(name), "orange") then fruit= 1; 
1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Same principle. See the code below. The means that the text search is case insensitive

 

data temp;
    set sashelp.cars;
    if prxmatch('/audi|acura|volvo/i', make) then dum=1;
run;

View solution in original post

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

Try PRXMATCH 🙂

 

data _null_;
    string='an apple';
    if prxmatch('/apple|orange/', string) then fruit=1;
    put fruit=;
run;
braam
Quartz | Level 8

Sorry for the confusion. In the piece of my code, "name" was the column in my dataset. To be clearer, the below is the code I can make. But I would like to get it simplified further (perhaps a code including "audi", "acura", "volvo"?).

 

 

 


data temp; set sashelp.cars;
	if index(lowcase(make), "audi")>0 or 	
		index(lowcase(make), "acura")>0 or 
		index(lowcase(make), "volvo")>0  
	then dum= 1; run;
PeterClemmensen
Tourmaline | Level 20

Same principle. See the code below. The means that the text search is case insensitive

 

data temp;
    set sashelp.cars;
    if prxmatch('/audi|acura|volvo/i', make) then dum=1;
run;
Ksharp
Super User

Better add '\b'.

 

 if prxmatch('/\b(apple|orange|banana)\b/', string) then fruit=1;
average_joe
Obsidian | Level 7
What about "apples, oranges and bananas"?
Ksharp
Super User
data _null_;
string="apples, oranges and bananas";
if prxmatch('/\b(apple|orange|banana)s?\b/', string) then fruit=1;
put string= fruit=;

call missing(of _all_);

string="apple, orange and banana";
if prxmatch('/\b(apple|orange|banana)s?\b/', string) then fruit=1;
put string= fruit=;
run;
dknochen
Fluorite | Level 6
That was perfect, thank you!

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
  • 17551 views
  • 6 likes
  • 5 in conversation