BookmarkSubscribeRSS Feed
BrahmanandaRao
Lapis Lazuli | Level 10

Hi Guys,

Good Moring 

data nth_occurance;
c='who is this'  ;
run;

in the above string how to find  'is'      2nd occurrence position with out using any regex 

6 REPLIES 6
s_lassen
Meteorite | Level 14

Beats me why you do not want to use REGEX. But here is a solution using the FIND function:

data want;
  set nth_occurance;
  pos=-1; /* 1-length(search string) */
  do _N_=1 to 2; /* N=2, here */
    pos=find(c,'is',pos+2); /* add length(search string) to starting position */
    if pos=0 then leave;
    end;
run;

The program returns a POS value of 0 if the nth occurrence is not in string.

Amir
PROC Star

Hi,

 

The find() function can accept an argument for the starting position for the search, so you can search for the first occurrence and then look for the 2nd occurrence after that. See code below for an example.

 

data nth_occurance;
  c = 'who is this';
  pos = find(c,'is',find(c,'is') + 1);
run;

 

 

Thanks & kind regards,

Amir.

Kurt_Bremser
Super User

BTW your question is very unclear. What do you want to find?

  • the 2nd word?
  • the number/position of the word "is", if contained?
  • the 2nd appearance of the word "is"?
BrahmanandaRao
Lapis Lazuli | Level 10

Hi Kurt

 I want to find 2nd occurrence position ' is'  in that text

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 999 views
  • 0 likes
  • 4 in conversation