BookmarkSubscribeRSS Feed
CaesarS
Calcite | Level 5
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';
3 REPLIES 3
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
deleted_user
Not applicable
Maybe indexc is more efficient in this context.

if indexc(abc,'STRING1',...,'STRING6') > 0 then NEW_FIELD = 'RESULT1';
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1220 views
  • 0 likes
  • 3 in conversation