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
There are many examples in the search results and I have 2 results that will assist you.
https://communities.sas.com/t5/General-SAS-Programming/Extracting-word-from-string/m-p/320572#M42293
https://communities.sas.com/t5/New-SAS-User/how-to-extract-key-words-from-variables/m-p/527321#M5262
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 that is great you learn and acknowledge that please mark the solution that assist you thank you.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.