Hello
I want to select rows with following criteria:
1- start with 'ttt'
2- name structure is : ttt+8 digits
3-Length is 11
What is the correct way to do it?
data a;
input tblName: $12.;
datalines;
ttt01032021
ttt08032021
ttt17022021
ttt21022021
tttabcdefgh
tttttttttttt
ttt1
tttAbc
ABCttt
t1
t2
t3
;
Run;
PROC SQL;
create table wanted as
select *
from a
where tblName like 'ttt%'
and prxmatch('/ttt\d{11}/i',tblName)
;
QUIT;
You can simply handle it with the following data steps, but do you want to use prxmatch?
data want;
set a;
if index(tblName,'ttt')=1 and
input(subpad(tblName,4,8),??best.)>0 and
length(tblName)=11;
run;
Thank you,
Using the following condition is giving a warning : input(subpad(tblName,4,8),best.)>0
I think that it happens because in some rows it try to covert to numeric a char.
What is the way to find a better code that have no warning?
PROC SQL;
create table wanted as
select tblName,subpad(tblName,4,8) as x
from a
where tblName like 'ttt%'
and length(tblName)=11
and input(subpad(tblName,4,8),best.)>0
;
QUIT;
To prevent the input function from throwing a warning, I used ??best. informat.
However, you can't use ?? in where clause.
@Ronein did you try my code?
@Ronein Try this
data a;
input tblName: $12.;
datalines;
ttt01032021
ttt08032021
ttt17022021
ttt21022021
tttabcdefgh
tttttttttttt
ttt1
tttAbc
ABCttt
t1
t2
t3
;
data b;
set a;
where prxmatch("/^ttt\d{8}\s*$/", tblName);
run;
data a; input tblName: $12.; datalines; ttt01032021 ttt08032021 ttt17022021 ttt21022021 tttabcdefgh tttttttttttt ttt1 tttAbc ABCttt t1 t2 t3 ; Run; PROC SQL; create table wanted as select * from a where tblName like 'ttt%' and prxmatch('/^ttt\d{8}$/i',strip(tblName)) ; QUIT;
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.