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;
Same principle. See the code below. The i means that the text search is case insensitive
data temp;
set sashelp.cars;
if prxmatch('/audi|acura|volvo/i', make) then dum=1;
run;
Try PRXMATCH 🙂
data _null_;
string='an apple';
if prxmatch('/apple|orange/', string) then fruit=1;
put fruit=;
run;
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;
Same principle. See the code below. The i means that the text search is case insensitive
data temp;
set sashelp.cars;
if prxmatch('/audi|acura|volvo/i', make) then dum=1;
run;
Better add '\b'.
if prxmatch('/\b(apple|orange|banana)\b/', string) then fruit=1;
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;
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.