Help using Base SAS procedures

Inserting a single quote via PRXCHANGE?

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Inserting a single quote via PRXCHANGE?

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?


Accepted Solutions
Solution
‎03-17-2017 01:38 PM
Trusted Advisor
Posts: 1,300

Re: Inserting a single quote via PRXCHANGE?

[ Edited ]

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

View solution in original post


All Replies
Respected Advisor
Posts: 4,654

Re: Inserting a single quote via PRXCHANGE?

[ Edited ]

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);

PG
Solution
‎03-17-2017 01:38 PM
Trusted Advisor
Posts: 1,300

Re: Inserting a single quote via PRXCHANGE?

[ Edited ]

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
New Contributor
Posts: 3

Re: Inserting a single quote via PRXCHANGE?

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!

New Contributor
Posts: 3

Re: Inserting a single quote via PRXCHANGE?

Well I meant to mark this as the solution. Now I can't undo it. I am a dork.
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 214 views
  • 2 likes
  • 3 in conversation