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?
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 🙂
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.
Index function – Searches a character expression for a string of characters
SAS Statements | Results |
a=’ABC.DEF (X=Y)’; b=’X=Y’; x=index(a,b); put x; | 10 |
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.