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

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1587 views
  • 0 likes
  • 4 in conversation