BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
scha1996
Calcite | Level 5

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.

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star
data _null_;
  string = 'Stephanie; Ashley; David';
  last_word = scan(string, countw(string));
  put _all_;
run;

View solution in original post

4 REPLIES 4
SASKiwi
PROC Star
data _null_;
  string = 'Stephanie; Ashley; David';
  last_word = scan(string, countw(string));
  put _all_;
run;
scha1996
Calcite | Level 5

Thank you so much! 😊

andreas_lds
Jade | Level 19

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;
mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 652 views
  • 4 likes
  • 4 in conversation