BookmarkSubscribeRSS Feed
ChrisNZ
Tourmaline | Level 20

Hi all,

I am validating values, looking for quoted single characters, except quoted quotes.

'a'  yes

'"'  no

'A   no

Abc  no

This works fine, but I had to fight the code to make it work.

Here b works, c doesn't.

%let a="'";

%put b=%sysfunc(prxmatch(/^(%str(%'|%"))[^%str(%"%')]\1$/,%superq(a)));

%put c=%sysfunc(prxmatch(/^(%str(%'|%"))[^%str(%'%")]\1$/,%superq(a)));

The only change is the order of the quotes in the regexp list expression. SAS wants them "closed" in the right order or gets confused.

This order should not matter (it is just a list), and does not matter in SAS code or when using prxparse:

%put d=%sysfunc(prxmatch(%sysfunc(prxparse(/^(%str(%'|%"))[^%str(%"%')]\1$/)),%superq(a)));

%put e=%sysfunc(prxmatch(%sysfunc(prxparse(/^(%str(%'|%"))[^%str(%'%")]\1$/)),%superq(a)));

I sent this to tech support, but was wondering whether anyone had encountered this / any comments / similar odd regexp cases.

3 REPLIES 3
Tom
Super User Tom
Super User

You are working too hard to create the regex expression. Let's break it down.  To match any character that is not a double quote you need [^"]. To match that surrounded by quotes you just need to add the quotes. To have that match the full string then you need at have ^ before and $ after.  Plus you need the beginning and ending slashes. Since your single quotes are balanced and your unbalanced double quote is inside of single quotes then no macro quoting of the regex is required.

 

 

%let pos=%sysfunc(prxmatch(/^'[^"]'$/,%superq(a)));

 

ChrisNZ
Tourmaline | Level 20

Tom i think you missed the fact that the string can be singled quoted *or doubled quoted*. The double quote option was not obvious from my text though it was used in the second example.

ChrisNZ
Tourmaline | Level 20
Tech support as lodged a defect.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 2449 views
  • 0 likes
  • 2 in conversation