BookmarkSubscribeRSS Feed
Jackie_Stanbank
Calcite | Level 5

hi

I have a variable with values containing numeric as well as alphabetic characters.(eg. 1675ag157). I want to put the observations that contains alphas in a seperate dataset. Is there a formula that i can use to identify these records other than a lengthy if statement?

8 REPLIES 8
andreas_lds
Jade | Level 19

You can use the anyalpha-function:

...

if anyalpha(variable) then output alphas;

else output numerics;

...

sas_Forum
Calcite | Level 5


data l;
input x $;
cards;
rt56y
polio
run;

data l2(drop=c) l3(drop=c);
set l;
c=anydigit(x);
if c=0 then output l2;
else output l3;
run;
proc print data=l2;
run;

GreggB
Pyrite | Level 9

did any of these replies answer your question?

FriedEgg
SAS Employee

*solution using regular expression;

data digits alphas;

input key $;

if prxmatch('/[a-zA-Z]+/',key) then output alphas; else output digits;

cards;

1675ag157

ag09912

simpton

123476

32246

12345h

;

run;

art297
Opal | Level 21

Matt, Just FWIW, Andreas' proposed anyalpha solution ran twice as fast as using a regular expression.  The code I ran, on 100,000 replications of your example data, was:

data digits alphas;

  set have;

  if prxmatch('/[a-zA-Z]+/',key) then output alphas;

   else output digits;

run;

data digits alphas;

  set have;

  if anyalpha(key) then output alphas;

  else output digits;

run;

FriedEgg
SAS Employee

Yes, in my opinion SAS does not implement regular expressions efficiently, in most cases a native function will outperform.  The benefit generally comes from more specific strings and cases that fall outside what the index and find functions can do (and the anyalpha/similar functions).

Ksharp
Super User

FriedEgg

You can use 'o' modifier to prevent SAS parse Perl Regular Expression at every data step loop.

That will be fast a lot.

if prxmatch('/[a-zA-Z]+/o',key) then output alphas; else output digits;

Ksharp

FriedEgg
SAS Employee

Thanks Ksharp, I did not know that hint.  Another option is to use prxparse first and then retain the id.  This does save a substantial amount of time when processing, however I still find it typically to be less efficient.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 8 replies
  • 1809 views
  • 1 like
  • 7 in conversation