BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi, I would just like to ask if there's a function in SAS that can look for 10 consecutive numeric characters in a string?

Example:

I have a table with 2 columns..Column A contains the following data:

01234567890ABC
01234ABC567890
01ABC23456789001

I wanted to populate Column B with a value that depends on whether there are 10 consecutive numeric characters in a string in Column A. I'm looking for a SAS function that may be able to help me with this (something like findc), as im avoiding using arrays or checking each character as it might be process intensive specially when alot of observations are already involved.

Thanks in advance.

Edit: I'm using SAS 9.1 and looking to do this in a data step if possible. Message was edited by: jpmoreno
9 REPLIES 9
Peter_C
Rhodochrosite | Level 12
ideal challenge for perl regular expression.
deleted_user
Not applicable
thanks!!, I'll post updates once i got the code going.
SushilNayak
Obsidian | Level 7
Hey Peter/jpmoreno,
the code for this problem in regular expression is very very simple. 10 time \d would get things going.

data d1;
input id $40.;
datalines;
01234567890ABC
01234ABC567890
01ABC23456789001
;
run;
data dx;
set d1;
re=prxparse('/\d\d\d\d\d\d\d\d\d\d/');
if prxmatch(re,id) then output;
run;
proc print;run;

Maybe you guys already got the solution but couldn't stop myself writting a perl regex..i rarely get to write regex these day....it's so tempting and cool.. 🙂


Thanks!
ChrisNZ
Tourmaline | Level 20
It is better to call prxparse once only.

data dx1;
retain re;
if _N_=1 then re=prxparse('/\d{10}/');
set d1;
if prxmatch(re,id) then output;
run;
SushilNayak
Obsidian | Level 7
Hey Chris,
you're right...totally forgot how to lessen the \d ...as i said, don't get to write perl regex these days...totally forgot about this 😞

Thanks for the updated regex!
ChrisNZ
Tourmaline | Level 20
No worries, many ways to skin a cat. 🙂
I was refering to the if _N_=1 then test.
Calling prxparse repeatedly is not advised.
SushilNayak
Obsidian | Level 7
🙂 ah that one ....the use of IF _N_ = 1 then do; or W/O it doesn't makes any difference( thank god i remember this 🙂 ) coz for constant regular expression the compilation happens only once and the value to the left side variable would be same for next successive observations as was for 1st observation. So successive calls to PRXPARSE will not cause a recompile, but will return the same value to the left side variable for the regular expression that was already compiled.
Again, as you said - "many ways to skin a cat" 🙂 ..w/ or w/o doesn't makes any difference in case of constant regular expression like the one above.

Thanks Chris, for pointing that out 🙂
ChrisNZ
Tourmaline | Level 20
Mmm, I didn't know that. It's a nice improvement on rxparse.
rxparse would eat up all the memory and abort the data step.
Cheers for the heads up.
deleted_user
Not applicable
yes we got something like this, thanks!. I really didn't know about these kind of stuffs(regular expression) before I posted, luckily Peter gave me an idea where to look/research. Thanks everyone!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 1835 views
  • 0 likes
  • 4 in conversation