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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 442 views
  • 1 like
  • 3 in conversation