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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.