## Selecting a 5-digit number which is randomly placed in a character string

Solved
Frequent Contributor
Posts: 100

# Selecting a 5-digit number which is randomly placed in a character string

Hello,

Consider these character strings (my input):

All the character strings include a 5-digit number, however they are written manually by the users, so there's no logic in how they're placed. They aren't always placed last in the string.

What I need to do is to write an expression which selects the 5-digit number from each character string. The output value for the bottom row, for example, should be "30653". Can someone please offer advice on how I might do this?

Accepted Solutions
Solution
‎03-13-2014 11:07 AM
SAS Super FREQ
Posts: 810

## Re: Selecting a 5-digit number which is randomly placed in a character string

This is a good case for the PRXMATCH function, see example below. You might also try the ANYDIGIT function

data have;
attrib x format=\$200.;

x='<skl>asffaafs 15178</skl>';

output;

x='<skl>30653</skl>';

output;

x='<skl>353</skl>';

output;

x='353';

output;
run;

data want;
set have;
d5match = prxmatch("/\d{5}/", x);

if d5match > 0 then do;
d5 = substr(x, d5match,
5);
end;
run;

All Replies
Super User
Posts: 9,397

## Re: Selecting a 5-digit number which is randomly placed in a character string

Try

substring 5 characters from the first instance of...

substr(string,indexc(string,'1','2','3','4','5','6','7','8','9','0'),5)

as in:

data temp;

attrib x format=\$200.;

x='<skl>asffaafs 15178</skl>'; output;

x='<skl>30653</skl>'; output;

run;

data temp2;

set temp;

y=substr(x,indexc(x,'1','2','3','4','5','6','7','8','9','0'),5);

run;

Super User
Posts: 6,626

## Re: Selecting a 5-digit number which is randomly placed in a character string

In your examples, each line contains only 5 digits.  If that's a realistic picture of what is in your data, the solution is easy:

length newvar \$ 5;

newvar = compress(oldvar,,'kd');

If you might encounter other digits along the way outside of those 5-in-a-row, the solution is still only mildly complex.  Is that a possibility?

Solution
‎03-13-2014 11:07 AM
SAS Super FREQ
Posts: 810

## Re: Selecting a 5-digit number which is randomly placed in a character string

This is a good case for the PRXMATCH function, see example below. You might also try the ANYDIGIT function

data have;
attrib x format=\$200.;

x='<skl>asffaafs 15178</skl>';

output;

x='<skl>30653</skl>';

output;

x='<skl>353</skl>';

output;

x='353';

output;
run;

data want;
set have;
d5match = prxmatch("/\d{5}/", x);

if d5match > 0 then do;
d5 = substr(x, d5match,
5);
end;
run;
Frequent Contributor
Posts: 100