Search for strings with apostrophes in SAS PROC SQL

1.4k views Asked by At

Is there a way to avoid the warning for the following code? I'm not so much interested in a system option to turn off warnings, rather, I am curious as to whether or not there is a better way to write this query.

data quotey; 
input string $9.;
cards;
shouldn't
wouldn't
couldn't
dont
won't
;
run;
proc print ; run;

ods html close; ods html ;


title 'Hmmm...';
proc sql ; 
select * from quotey 
where string like "%dn't%"
;quit;
2

There are 2 answers

2
Joe On BEST ANSWER

You have a few different ways to handle this, but the simplest is to use contains instead of like. Of course that wouldn't work in all cases, but it would in yours.

proc sql ; 
select * from quotey 
where string contains "dn't"
;
quit;

There are a lot of messy ways to handle it otherwise. The simplest is to use single quotes (to prevent macro resolution), but then you need to double your single quote/apostrophe.

proc sql ; 
select * from quotey 
where string like '%dn''t%'
;
quit;

You could also use %nrstr in a few ways (it prevents characters from being used to resolve a macro). Here's one.

proc sql ; 
select * from quotey 
where string like "%nrstr(%%)dn't%nrstr(%%)"
;quit;

Here you have to double the % character (As it's used as an 'escape' sort of).

1
Shenglin Chen On

Also try to use regular expression:

title 'Hmmm...';
proc sql ; 
select * from quotey 
where prxmatch("/dn't/",string)>1;
;quit;