Solved
New Contributor
Posts: 3

# ANYALPHA & ANYDIGIT VALIDATION

Hi Guys

So i have a dataset with a variable with ID´s. ID´s should consist of 2 ALPHA positions and 8 DIGITposition. If this requirement is not met i should write it in a separated dataset.

AB12345678

The first filter i wanna say its the length = 10.

Second one is that first 2 positions both should be ALPHA.

Third that every single position from the 3rd to the 10th should be DIGIT.

If any of this requirements is not met, immediatly without going any further should be sent to a dataset.

I´ve seen the following code.

```data _null_;
string='Next = _n_ + 12E3;';
j=0;
do until(j=0);
j=anyalpha(string,j+1);
if j=0 then put +3 "That's all";
else do;
c=substr(string,j,1);
put +3 j= c=;
end;
end;
run;```

But i wonder how could i integrate the ANYDIGIT part and the conditional for every position after has been validated wheater is an ALPHA or a DIGIT.

Accepted Solutions
Solution
‎03-07-2017 11:07 AM
Super User
Posts: 10,621

## Re: ANYALPHA & ANYDIGIT VALIDATION

``````data have;
input x \$20.;
cards;
AB12345678
A12345678
;
run;
data right error;
set have;
if prxmatch('/^[a-z]{2}\d{8}\$/i',strip(x)) then output right;
else output error;
run;``````

All Replies
Super User
Posts: 22,873

## Re: ANYALPHA & ANYDIGIT VALIDATION

Substr out the components and check them. Look at NOTALPHA/NOTDIGIT as well.

LENGTH() = 10

SUBSTR(string, 1, 2) - NOTALPHA

SUBSTR(string, 3, 8) - NOTDIGIT

Solution
‎03-07-2017 11:07 AM
Super User
Posts: 10,621

## Re: ANYALPHA & ANYDIGIT VALIDATION

``````data have;
input x \$20.;
cards;
AB12345678
A12345678
;
run;
data right error;
set have;
if prxmatch('/^[a-z]{2}\d{8}\$/i',strip(x)) then output right;
else output error;
run;``````
New Contributor
Posts: 3