BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
J_J_J
Obsidian | Level 7

Hi!

I'm trying to find only those variables, where first 2 symbols are letters and next 8 are numbers and length of variable is 10 digits. I used code: (anyalpha(substr(CODE, 1, 2), 2) and anydigit(substr(CODE, 3, 10), 😎 and length(CODE)=10). But it doesn't work. Please help to do it.

Have:

AB12345678

ABCD201642
20AB3413089000788913
20AB00000247287021

CD12345678

Want:

AB12345678

CD12345678

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

HI @J_J_J  Please try-


data have;
input var $20.;
cards;
AB12345678
ABCD201642
20AB3413089000788913
20AB00000247287021
CD12345678
;

data want;
 set have;
 where lengthn(var)=10;
 if prxmatch('/^[a-z]{2}\d{8}/i', var);
run;

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20

HI @J_J_J  Please try-


data have;
input var $20.;
cards;
AB12345678
ABCD201642
20AB3413089000788913
20AB00000247287021
CD12345678
;

data want;
 set have;
 where lengthn(var)=10;
 if prxmatch('/^[a-z]{2}\d{8}/i', var);
run;
J_J_J
Obsidian | Level 7
Thanks a lot, novinosrin!
Tom
Super User Tom
Super User

Let's try using the ANYDIGIT and ANYALPHA functions to get the information needed for your test.

First let's make some data.

data have ;
  input code $30. ;
cards;
AB12345678
ABCD201642
20AB3413089000788913
20AB00000247287021
CD12345678
XX1234 678
;

Now let's make some tests and then check that they follow all of the rules. Let's use NOTDIGIT to make sure the last 8 characters are all digits.

data want;
  set have ;
  len = lengthn(code);
  first_digit= anydigit(code);
  last_digit= anydigit(code,-len);
  last_nondigit=notdigit(code,-len);
  first_alpha=anyalpha(code);
  last_alpha=anyalpha(code,-len);
  want=len=10 and first_digit=3 and last_digit=10 and last_nondigit=2 and first_alpha=1 and last_alpha=2;
run;

Results:

                                      first_    last_      last_     first_    last_
Obs    code                    len     digit    digit    nondigit     alpha    alpha    want

 1     AB12345678               10       3        10         2          1        2        1
 2     ABCD201642               10       5        10         4          1        4        0
 3     20AB3413089000788913     20       1        20         4          3        4        0
 4     20AB00000247287021       18       1        18         4          3        4        0
 5     CD12345678               10       3        10         2          1        2        1
 6     XX1234 678               10       3        10         7          1        2        0
J_J_J
Obsidian | Level 7
Thanks, Tom. Your solution seems good too.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 1142 views
  • 2 likes
  • 3 in conversation