在SAS中搜索具有字符和数字的模式



我正在检查数据质量,并试图查看正确填充了多少行。该字段应包含一个字符串,其中一个字符后跟九个数字,并且类型为"字符",长度为10。

Ex。

  • A123456789
  • B123531490
  • C319861045

我尝试过使用PRXMATCH函数,但我不确定是否使用了正确的语法。我还尝试过将PROC SQL与"Where not like"[A-Z][0-9][0-9]等一起使用。我的感觉是,这应该不难执行,有人有解决方案吗?

向致以最良好的问候

您可以构造一个REGEX来进行测试。或者只是使用正常的SAS功能构建测试。

data want ;
set have ;
flag1 = prxmatch('/^[A-Z][0-9]{9}$/',trim(name));
test1 = 'A' <= name <= 'Z' ;
test2 = not notdigit(trim(substr(name,2))) ;
test3 = length(name)=10;
flag2 = test1 and test2 and test3 ;
run;

结果:

Obs    name             flag1    test1    test2    test3    flag2
1     A123456789590      0        1        1        0        0
2     B123531490ABC      0        1        0        0        0
3     C3198610           0        1        1        0        0
4     A123456789         1        1        1        1        1
5     B123531490         1        1        1        1        1
6     C319861045         1        1        1        1        1

您可以使用:

^[a-zA-z][0-9]{9}$

内置SAS函数NOTALPHANOTDIGIT可以执行验证测试。

invalid_flag = notalpha(substr(s,1,1)) || notdigit(s,2) ;

您可以使用where语句或选项直接选择无效记录

data invalid;
set raw;
where notalpha(substr(s,1,1)) || notdigit(s,2) ;  * statement;
run;
data invalid;
set raw (where=(notalpha(substr(s,1,1)) || notdigit(s,2)));  * data set option;
run;

NOT*ANY*族中有几个函数,它们可以比PRX*族中的通用正则表达式函数提供更快的性能。

您可以使用prxparse和prxmatch,如下所示。

data have;
input name $20.;
datalines;
A123456789590
B123531490ABC
C3198610
A123456789
B123531490
C319861045
;

data want;
set have;
if _n_=1 then do; 
retain re; 
re = prxparse('/^[a-zA-z][0-9]{9}$/'); 
end;
if prxmatch(re,trim(name))  gt 0 then Flag ='Y';
else Flag ='N';
drop re;
run;

如果您只想要符合条件的记录,则使用

data want;
set have;
if _n_=1 then do; 
retain re; 
re = prxparse('/^[a-zA-z][0-9]{9}$/'); 
end;
if prxmatch(re,trim(name));
drop re;
run;

最新更新