Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- SAS Procedures
- /
- Find numeric values in character

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 06-29-2020 10:45 AM
(749 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;
```

4 REPLIES 4

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thanks a lot, novinosrin!

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thanks, Tom. Your solution seems good too.

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

**If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. **

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.