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)
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');
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.
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.) )
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!
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.
Ready to level-up your skills? Choose your own adventure.