BookmarkSubscribeRSS Feed
abc1324
Calcite | Level 5

I'm trying to write a regular expression to test serial numbers to see if they are in the correct format and numeric range.

 

The serial numbers should be formatted as follows AB123X670301 to AB123X675301 where the last six digits represents a range from 670301 to 675301.

 

I've tried the following code but it doesn't accomodate the range properly. Can somebody with more experience provide me with some tips?

 

data want

   set have

      where not prxmatch("/AB123X[670301 - 675301]/", SERIAL_NBR)

4 REPLIES 4
Astounding
PROC Star

You'll probably get a simple answer from someone familiar with prxmatch.  Just in case, it's easy to do without:

 

where SERIAL_NBR =: 'AB123X67' and ('0301' <= substr(SERIAL_NBR, 9, 4) <= '5301');

 

PGStats
Opal | Level 21

If the prefix is constant then you can simply do:

 

where not ("AB123X670301" <= SERIAL_NBR <= "AB123X675301");

 

if it isn't, then we need more information describing the legal values.

PG
Jagadishkatam
Amethyst | Level 16

Alternatively, you could try to suppress the portion of the string which is not the actual range by prxchange as below and use the remaining portion for comparison

 

The input(prxchange('s/^\w{2}\d{3}\w{1}//',-1,char),best.) will return the numeric portion i.e. the last 6 digits which is converted to numeric value by input function

 

 

 

SERIAL_NBR =: 'AB123X67' and not (input(prxchange('s/^\w{2}\d{3}\w{1}//',-1,char),best.) <= SERIAL_NBR <= input(prxchange('s/^\w{2}\d{3}\w{1}//',-1,char),best.) )

 

 

 

Thanks,
Jag
abc1324
Calcite | Level 5
Thanks for the ideas, everyone.
The prefix should be consistent, however, I've found that sometimes it's included, other times it's partially included, and sometimes it's excluded altogether.
I'm thinking that I should 1) test to see if the prefix exists and flag those where it doesn't. 2) suppress the prefix if it does exist and then test the numeric portion to make sure that it's within the acceptable range.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 4 replies
  • 1336 views
  • 4 likes
  • 4 in conversation