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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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