DATA Step, Macro, Functions and more

Looking for consecutive numeric characters in a string

Reply
N/A
Posts: 0

Looking for consecutive numeric characters in a string

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
Valued Guide
Posts: 2,175

Re: Looking for consecutive numeric characters in a string

ideal challenge for perl regular expression.
N/A
Posts: 0

Re: Looking for consecutive numeric characters in a string

thanks!!, I'll post updates once i got the code going.
Contributor
Posts: 31

Re: Looking for consecutive numeric characters in a string

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.. Smiley Happy


Thanks!
PROC Star
Posts: 1,570

Re: Looking for consecutive numeric characters in a string

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;
Contributor
Posts: 31

Re: Looking for consecutive numeric characters in a string

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 Smiley Sad

Thanks for the updated regex!
PROC Star
Posts: 1,570

Re: Looking for consecutive numeric characters in a string

No worries, many ways to skin a cat. Smiley Happy
I was refering to the if _N_=1 then test.
Calling prxparse repeatedly is not advised.
Contributor
Posts: 31

Re: Looking for consecutive numeric characters in a string

Smiley Happy 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 Smiley Happy ) 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" Smiley Happy ..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 Smiley Happy
PROC Star
Posts: 1,570

Re: Looking for consecutive numeric characters in a string

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.
N/A
Posts: 0

Re: Looking for consecutive numeric characters in a string

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!
Ask a Question
Discussion stats
  • 9 replies
  • 236 views
  • 0 likes
  • 4 in conversation