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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.