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;
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!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.