BookmarkSubscribeRSS Feed
sandeep12
Fluorite | Level 6

 When looking for data contained in a character string of 150 bytes, which function is the best to locate that data: scan, index, or indexc?

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Depends largely on what you actually want to do. Consult the SAS Documentation for the Index, Indexc and Scan functions to see what fits your needs best.

 

Or be more specific about your actual problem. Post some example data and what your desired result looks like. Then we can give you a usable code answer.

 

Also, welcome to the SAS communities 🙂

Kurt_Bremser
Super User

The data you have and the end result you expect will define which tools to use when going from here to there.

So you need to show us your data, and what you want to extract or find.

Hints for posting existing data in a usable form can be found in my footnotes. The preferred way for posting data is a data step with datalines.

 

You should refrain from using the bold typeface for your whole post. Use bold only to emphasize certain parts; by using bold all the way through you lose that option.

 

And make it a habit to answer to posts given in response to your questions (https://communities.sas.com/t5/SAS-Programming/Difference-between-reading-data-from-an-external-file...), so we can know if our answers were helpful.

rinugour
Fluorite | Level 6

Index function – Searches a character expression for a string of characters 

SAS StatementsResults
a=’ABC.DEF (X=Y)’;
b=’X=Y’;
x=index(a,b);
put x;
10
ballardw
Super User

Don't forget INDEXW, FIND, FINDC and FINDW as well.

Also if your text is double-byte characters you would be looking at Kindex or other K function equivalents.

 

SCAN implies that you are providing a "word" position so would likely require a loop of some sort to find a specific word. If the string you want to find includes the delimiter that Scan is using then this function is likely not for you.

 

If the string you are searching for may be part of another word and you only want complete "words" then Index and Indexc are probably not what you want though Indexw might. If the entire string value you search for must be there then you do not want Indexc.

 

If you want to find the first position of any of a list of characters then INDEXC.

 

If you need or want to specify a start position then you likely want one of the FIND functions, or to search backwards from the end of a string.

 

Actual examples of what you want to search in, search for what to return, position or word number, will likely determine what is "best" or even may work at all.

 

Note that the length of the character string really doesn't matter though searching for a 6 character word in variable of length 5 is contraindicated.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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