DATA Step, Macro, Functions and more

Finding substring position in a source string

Reply
Contributor
Posts: 33

Finding substring position in a source string

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.

An example:

%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.

Hope this makes a sense Smiley Happy

thanks,
Sarunas
SAS Super FREQ
Posts: 8,869

Re: Finding substring position in a source string

Hi:
Does it have to be a macro solution? Have you looked at the INDEX, INDEXC and FIND and/or SCAN functions???
cynthia
Contributor
Posts: 33

Re: Finding substring position in a source string

Posted in reply to Cynthia_sas
Well yes, it has to be a macro solution, but I could use and SAS BASE functions of course.

I will use the %SCAN function afterward. That is a point that I need a position of substring in order to obtain the substring itself with the help of %SCAN function.

Thanks for hints.
N/A
Posts: 0

Re: Finding substring position 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;
end;


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.


mystring=COMPBL(mystring);
mystring1=SUBSTR(mystring,1,INDEXW(mystring,'Second'));
myindex=COUNTC(mystring1,' ') + 1;


Counting the number of spaces to find out the number of words.

Loads of other methods possible, if this suites you or you get a better mthod, please let us know.
Contributor
Posts: 33

Re: Finding substring position in a source string

Posted in reply to deleted_user
Thanks a lot, tangentray for a comprehensive answer Smiley Happy
N/A
Posts: 0

Re: Finding substring position in a source string

I would try handle with %SCAN function.
Ask a Question
Discussion stats
  • 5 replies
  • 280 views
  • 0 likes
  • 3 in conversation