## specific characters from a string

Solved
Occasional Contributor
Posts: 14

# specific characters from a string

Hello,

I need to find the ID where the first 3 characters of the field are between (‘A’) and (‘Z’)...So if I have this list:

```5021060
A310603
D1A5421
5022000
5001050
5002000
5010020
5010045
5010045
50A5421
5012060
5013100

```

then the SAS program would output only these:

A310603

D1A5421

50A5421

Thank you!

Accepted Solutions
Solution
‎03-07-2018 10:43 AM
PROC Star
Posts: 514

## Re: specific characters from a string

something like this

``````data have;
input string \$;
datalines;
5021060
A310603
D1A5421
5022000
5001050
5002000
5010020
5010045
5010045
50A5421
5012060
5013100
501310D
;

data want;
set have;
if anyalpha(substr(string,1,3)) gt 0;
run;``````

All Replies
Solution
‎03-07-2018 10:43 AM
PROC Star
Posts: 514

## Re: specific characters from a string

something like this

``````data have;
input string \$;
datalines;
5021060
A310603
D1A5421
5022000
5001050
5002000
5010020
5010045
5010045
50A5421
5012060
5013100
501310D
;

data want;
set have;
if anyalpha(substr(string,1,3)) gt 0;
run;``````
Super User
Posts: 9,599

## Re: specific characters from a string

Post test data in the form of a datastep!

Something like (and not tested as not typing all that in):

```data want;
set have;
if lengthn(compress(substr(yourvar,1,3),"","ka")) > 0 then output;
run;```

So take the first 3 chars, compress down to only a-z and if length > 0 at least one is present

☑ This topic is solved.