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

Hi,

 

I have a variable (order_message) whereby some outputs are personalised to a an individual (example below)

 

Congratulations on your order Mr Ben Griggs

Congratulations on your order Mr Trevor Terry

Congratulations on your order Miss Kate Jones

Congratulations on your order Dr Jon Berry

 

How do I write some code to say if at the end of an order_message we have a title that consists of either of the following Mr, Miss, Mrs, Dr, Sir, Ms, Prof. then delete so all we see is 'Congratulations on your order'? 

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20
data have;
input order_message $ 1-46;
datalines;
Congratulations on your order Mr Ben Griggs
Congratulations on your order Mr Trevor Terry
Congratulations on your order Miss Kate Jones
Congratulations on your order Dr Jon Berry
;

data want;
    set have;
    newVar=prxchange('s/(Mr|Miss|Mrs|Dr|Sir|Ms|Prof).*//', -1, order_message);
run;

 

Result:

 

order_message                                  NewVar
Congratulations on your order Mr Ben Griggs    Congratulations on your order
Congratulations on your order Mr Trevor Terry  Congratulations on your order
Congratulations on your order Miss Kate Jones  Congratulations on your order
Congratulations on your order Dr Jon Berry     Congratulations on your order

View solution in original post

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20
data have;
input order_message $ 1-46;
datalines;
Congratulations on your order Mr Ben Griggs
Congratulations on your order Mr Trevor Terry
Congratulations on your order Miss Kate Jones
Congratulations on your order Dr Jon Berry
;

data want;
    set have;
    newVar=prxchange('s/(Mr|Miss|Mrs|Dr|Sir|Ms|Prof).*//', -1, order_message);
run;

 

Result:

 

order_message                                  NewVar
Congratulations on your order Mr Ben Griggs    Congratulations on your order
Congratulations on your order Mr Trevor Terry  Congratulations on your order
Congratulations on your order Miss Kate Jones  Congratulations on your order
Congratulations on your order Dr Jon Berry     Congratulations on your order
Ksharp
Super User

I think you should add \b.


data want;
    set have;
    newVar=prxchange('s/\b(Mr|Miss|Mrs|Dr|Sir|Ms|Prof)\b.*//i', -1, order_message);
run;

Otherwise ,something like 

"Congratulations on your ordMr Mr Ben Griggs"

would get you wrong result .

SAS Innovate 2025: Call for Content

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!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 2 replies
  • 419 views
  • 1 like
  • 3 in conversation