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
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
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
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.