Hi all - I would like to use a single quote in the PRXCHANGE function as follows:
new_text_field = PRXCHANGE('s/([A-Z]\w+)(?=s)/$1\'/',-1,text_field);
But the above returns an error. It doesn't like that single quote in the pink section. I know these are a pain to read but this would ideally scan text for words that end in 's' and replace them so that they end in 's basically making them possessive. This works if I use a dash instead of a single quote.
Any idea how to make it accept the single quote?
You are going to get unwanted results unless you modify the regular expression to look for a word boundary too
data have;
input text_field $10.;
cards;
Expression
Tools
;
run;
data want;
set have;
new_text_field=prxchange('s/([A-Z]\w+)(?=s)/$1''/',-1,text_field);
foo_text_field=prxchange('s/([A-Z]\w+)(?=s\b)/$1''/',-1,text_field);
put (text_field new_text_field foo_text_field) (/=);
run;
text_field=Expression new_text_field=Expres'sion foo_text_field=Expression text_field=Tools new_text_field=Tool's foo_text_field=Tool's
Use double quotes to surround a string with a single quote inside
new_text_field = PRXCHANGE("s/([A-Z]\w+)(?=s)/$1'/", -1, text_field);
You are going to get unwanted results unless you modify the regular expression to look for a word boundary too
data have;
input text_field $10.;
cards;
Expression
Tools
;
run;
data want;
set have;
new_text_field=prxchange('s/([A-Z]\w+)(?=s)/$1''/',-1,text_field);
foo_text_field=prxchange('s/([A-Z]\w+)(?=s\b)/$1''/',-1,text_field);
put (text_field new_text_field foo_text_field) (/=);
run;
text_field=Expression new_text_field=Expres'sion foo_text_field=Expression text_field=Tools new_text_field=Tool's foo_text_field=Tool's
Thank you to both!
And yes I have not yet tackled the word boundary issue but I am aware of it. Good advice.
Thank you!
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.