BookmarkSubscribeRSS Feed
thanikondharish
Calcite | Level 5

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
Calcite | Level 5
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.

 

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