BookmarkSubscribeRSS Feed
thanikondharish
Fluorite | Level 6

data ex1;
name='aaa234bbb$&#^#*abrrtbbrtbbb';
output;
name='harbbdgetbb' ;
output;
run;

 

 

I have one dataset like see above dataset so how to extract 'bb' wherever the string has only 'bb' word(onlu two letters) pick that position

like see below dataset

 

name                                                   position

aaa234bbb$&#^#*abrrtbbrtbbb             21

harbbdgetbb                                         4

 

4 REPLIES 4
Tom
Super User Tom
Super User

You could use CALL SCAN() in a loop.   Treat any character other than 'b' as a delimiter. Look at each "word" and stop when you find one that is length of 2.

data have ;
  input name $50. ;
cards;
aaa234bbb$&#^#*abrrtbbrtbbb
harbbdgetbb
xxxbbyyy
bbzzz
none
;

data want ;
  set have;
  do i=1 by 1 until(len in (0,2)) ;
    pos=sum(pos,len,0);
    call scan(name,i,pos,len,compress(name,'b'));
  end;
  put (i pos len name) (=);
run;

Result

i=3 pos=21 len=2 name=aaa234bbb$&#^#*abrrtbbrtbbb
i=1 pos=4 len=2 name=harbbdgetbb
i=1 pos=4 len=2 name=xxxbbyyy
i=1 pos=1 len=2 name=bbzzz
i=1 pos=0 len=0 name=none
thanikondharish
Fluorite | Level 6
Thanq now I learnt one new thing
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

@thanikondharish that is great you learn and acknowledge that please mark the solution that assist you thank you.

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 4 replies
  • 1177 views
  • 4 likes
  • 3 in conversation