Dear SAS experts,
Could you please help me to find out if there are macro or sas/base functions that would return the nuber of substring position in a source string. Similar to %INDEX function which returns the position of first character in a source string.
%let myString = First second third;
If I use: %let charPosition = %index(&myString,"second"); I'd get &charPosition is 7.
I need a function the would return me 2. I.e. the positions of substring "second" in a source string.
I am not aware of any function which can directly meet your requirements. You probably will need to come up with a logic to do so. The functions that you can use to come up with your logic have already been listed by fellow users above. Another function that you might find useful maybe INDEXW, it finds the index of the first alphabet of the string being searched in the given mother-string.
A lot of possible logical designs can be thought of to achieve what you are after. Following is a logic using INDEXW and SCAN.
mystring='First Second Third';
n= 2 ** (indexw(mystring,'Second') - 3);
Do i=1 to n;
if scan(mystring,i) = 'Second' then myindex=i;
The logic is simple, I find the starting point of the word in question, in this case it is 7. So there are 6 positions before it and I try to find out the number of possible words that can fit there. The 6th position should be blank and the 1st position should not be blank, remaining all positions may either be blank or be filled with alphabets, so the number of possible words = 2^(7-3), and I scan all words in this range.
A better logic might be achieved with the use of INDEXW, COUNTC, SUBSTR and SCAN. Assumption input string would not start with a space.