turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Finding substring position in a source string

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-16-2008 05:17 PM

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

thanks,

Sarunas

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

thanks,

Sarunas

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-16-2008 06:29 PM

Hi:

Does it have to be a macro solution? Have you looked at the INDEX, INDEXC and FIND and/or SCAN functions???

cynthia

Does it have to be a macro solution? Have you looked at the INDEX, INDEXC and FIND and/or SCAN functions???

cynthia

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-17-2008 03:10 AM

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.

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-17-2008 07:49 AM

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.

A lot of possible logical designs can be thought of to achieve what you are after. Following is a logic using INDEXW and SCAN.

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-17-2008 03:37 PM

Thanks a lot, tangentray for a comprehensive answer

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-16-2008 09:40 PM

I would try handle with %SCAN function.