Help using Base SAS procedures

Check a character variable against a predefined pattern of letters and numbers

Reply
Contributor
Posts: 68

Check a character variable against a predefined pattern of letters and numbers

Dear all,

I would very much appreciate your help.

I have a variable that should contain letters and numbers in a predefined combination i.e.   XYZ1234  (3letters - any letters and 4 numbers - any numbers)

The problem is that some values they have:

1.- 2 letters (false) and 3 numbers   (false)

2. 3 letters (correct) and 3 numbers (false)

3.- sometimes within the 3 letters there is one or more numbers while they shouldn't have been

4.  the same applies to letters section ( there are letters instead of numbers

5.-- No letters only numbers --more than  4

etc..

I would like to create a variable that will flag each and every discrepancy from the required as above (3 letters+ 4 numbers)

Thanking you in advance.

Best

Nikos

Respected Advisor
Posts: 3,156

Re: Check a character variable against a predefined pattern of letters and numbers

If you don't mind using Perl Regular expression, then it can be as simple as:

DATA HAVE;

     INPUT VAR $;

     FLAG=PRXMATCH('/^[A-Z]{3}\d{4}$/',strip(VAR))>0;

     CARDS;

XYZ1234

XZ124

XY1234

12345

X1Y1Z1234

XYZ1234

;


Regards,

Haikuo

Ask a Question
Discussion stats
  • 1 reply
  • 134 views
  • 0 likes
  • 2 in conversation