DATA Step, Macro, Functions and more

More efficient way to search for strings in a character field?

Reply
N/A
Posts: 1

More efficient way to search for strings in a character field?

I'm currently using the code below to search field ABC for one of several text strings; if any of them appear, I set NEW_FIELD to a common result. Is there a better way to do this? In plain english, I'm looking for a statement that says, if ABC contains any of the following: 'STRING1', . . . 'STRING6' then NEW_FIELD = 'RESULT1'

IF INDEX(ABC, 'STRING1') > 0 THEN NEW_FIELD = 'RESULT1';
ELSE IF INDEX(ABC, 'STRING2') > 0 THEN NEW_FIELD = 'RESULT1';
ELSE IF INDEX(ABC, 'STRING3') > 0 THEN NEW_FIELD = 'RESULT1';
ELSE IF INDEX(ABC, 'STRING4') > 0 THEN NEW_FIELD = 'RESULT1';
ELSE IF INDEX(ABC, 'STRING5') > 0 THEN NEW_FIELD = 'RESULT1';
ELSE IF INDEX(ABC, 'STRING6') > 0 THEN NEW_FIELD = 'RESULT1';
Super Contributor
Super Contributor
Posts: 3,174

Re: More efficient way to search for strings in a character field?

The INDEX function appears to be the most efficient, as compared to the recently added FIND function.

Also, using your coding example, I also tested using OR condition rather than IF THEN ELSE logic and again INDEX was more efficient, say with a 1 million DO loop iteration with an OUTPUT.

Scott Barry
SBBWorks, Inc.
N/A
Posts: 0

Re: More efficient way to search for strings in a character field?

Maybe indexc is more efficient in this context.

if indexc(abc,'STRING1',...,'STRING6') > 0 then NEW_FIELD = 'RESULT1';
Super Contributor
Super Contributor
Posts: 3,174

Re: More efficient way to search for strings in a character field?

Posted in reply to deleted_user
The function INDEXC applies to a character (one or more specified as argument #2) search within a string (argument #1), not a string of characters to search for.

Scott Barry
SBBWorks, Inc.
Ask a Question
Discussion stats
  • 3 replies
  • 181 views
  • 0 likes
  • 3 in conversation