Help using Base SAS procedures

How to find a list of specific strings amid a character variable?

Accepted Solution Solved
Reply
Contributor
Posts: 62
Accepted Solution

How to find a list of specific strings amid a character variable?

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


Accepted Solutions
Solution
‎09-07-2013 11:52 PM
Respected Advisor
Posts: 4,930

Re: How to find a list of specific strings amid a character variable?

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


All Replies
Super User
Posts: 5,516

Re: How to find a list of specific strings amid a character variable?

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.

Respected Advisor
Posts: 3,156

Re: How to find a list of specific strings amid a character variable?

Maybe some PRX?

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

Haikuo

Contributor
Posts: 62

Re: How to find a list of specific strings amid a character variable?

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.

Solution
‎09-07-2013 11:52 PM
Respected Advisor
Posts: 4,930

Re: How to find a list of specific strings amid a character variable?

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
Contributor
Posts: 62

Re: How to find a list of specific strings amid a character variable?

Yep- worked like magic! Thanks PGStats

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 232 views
  • 6 likes
  • 4 in conversation