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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
BrunoMueller
SAS Super FREQ

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

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

Astounding
PROC Star

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?

BrunoMueller
SAS Super FREQ

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;
EinarRoed
Pyrite | Level 9

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

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!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

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