BookmarkSubscribeRSS Feed
sarunas
Calcite | Level 5
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
5 REPLIES 5
Cynthia_sas
SAS Super FREQ
Hi:
Does it have to be a macro solution? Have you looked at the INDEX, INDEXC and FIND and/or SCAN functions???
cynthia
sarunas
Calcite | Level 5
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.
deleted_user
Not applicable
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.
sarunas
Calcite | Level 5
Thanks a lot, tangentray for a comprehensive answer 🙂
deleted_user
Not applicable
I would try handle with %SCAN function.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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