SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

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

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 89
Accepted Solution

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

Hello,

Consider these character strings (my input):

Skjermbilde.PNG

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?

Thanks for your time.


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

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;

View solution in original post


All Replies
Super User
Super User
Posts: 7,426

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: 5,096

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: 685

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: 89

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

Thanks for the feedback everyone. PRXMATCH was ideal, thanks Bruno. Smiley Happy

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 427 views
  • 6 likes
  • 4 in conversation