Hello
I'm currently in the process of learning SAS using SAS Enterprise Guide and I was wondering if there was a way to extract the last string after a semicolon and space.
For example: Stephanie; Ashley; David
I would like to be able to extract JUST David from this if possible. Please let me know if you need any additional details.
data _null_;
string = 'Stephanie; Ashley; David';
last_word = scan(string, countw(string));
put _all_;
run;
data _null_;
string = 'Stephanie; Ashley; David';
last_word = scan(string, countw(string));
put _all_;
run;
Thank you so much! 😊
The scan function has an option to let it start looking from end of the string onwards. In this case the solution provided by @SASKiwi seems to be easier to understand, but if you need the second last-word, i would use:
data _null_;
string = 'Stephanie; Ashley; David';
last_word = scan(string, 2, '; ', 'b');
put _all_;
run;
In fact, you don't even need the 'B' parameter for backward scanning. Any negative number in he second parameter tells sas to scan backwards:
data _null_;
string = 'Stephanie; Ashley; David';
last_word=scan(string,-1,';');
put (_all_) (=);
run;
It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.