BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

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;

 

 

6 REPLIES 6
japelin
Rhodochrosite | Level 12

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;
Ronein
Onyx | Level 15

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;
japelin
Rhodochrosite | Level 12

To prevent the input function from throwing a warning, I used ??best. informat.

However, you can't use ?? in where clause.

PeterClemmensen
Tourmaline | Level 20

@Ronein did you try my code?

PeterClemmensen
Tourmaline | Level 20

@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;
Ksharp
Super User
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;
How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1083 views
  • 3 likes
  • 4 in conversation