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

I want to search a variable in a very big data set for a list of specific substring.

I though index function would do the job, but index function can fit only one string to search for.

I var  on variable, and I want to search this variable for big number of small strings for example "DEX", HEX", "RED", "BET" and so on.

so I want any word in the search variable to be flagged if int has for example dexamethsone, hexagonal...etc

I was thinking this would work but it's not

if index(VAR, 'DEX', 'HEX', 'BET') >0 then flag=1

I don't  want to specify many if statements, because my st of search is big

Any ideas.

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

You might want to add a suffix to the pattern to improve efficiency and behaviour:

FLAG=(prxmatch('/DEX|HEX|RED|BET/io',VAR)>0);

The 'i' makes the match case insensitive and the 'o' instructs the function that the pattern should be compiled only once, otherwise, it is recompiled on every call.

PG

PG

View solution in original post

5 REPLIES 5
Astounding
PROC Star

There's no way I know of to search for all three at the same time.  To cut down the costs a bit, you can use:

if index(VAR, 'DEX') then flag=1;

else if index(VAR, 'HEX') then flag=1;

else if index(VAR, 'BET') then flag=1;

Good luck.

Haikuo
Onyx | Level 15

Maybe some PRX?

FLAG=(prxmatch('/[DEX|HEX|RED|BET]/',VAR)>0);

Haikuo

Altal
Calcite | Level 5

Thank you guys. That was really helpful.

Haikuo- thank you, I didn't think about the prxmatch function before. It worked; I minimally modified your code to work. Here is what worked for me...

FLAG=(prxmatch('/DEX|HEX|RED|BET/',VAR)>0);

You can add any substring and divide them by "|". This will save from putting multiple IF statements if looking for various substrings.

PGStats
Opal | Level 21

You might want to add a suffix to the pattern to improve efficiency and behaviour:

FLAG=(prxmatch('/DEX|HEX|RED|BET/io',VAR)>0);

The 'i' makes the match case insensitive and the 'o' instructs the function that the pattern should be compiled only once, otherwise, it is recompiled on every call.

PG

PG
Altal
Calcite | Level 5

Yep- worked like magic! Thanks PGStats

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 1582 views
  • 6 likes
  • 4 in conversation