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?
Thanks for your time.
This is a good case for the PRXMATCH function, see example below. You might also try the ANYDIGIT function
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;
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?
This is a good case for the PRXMATCH function, see example below. You might also try the ANYDIGIT function
Thanks for the feedback everyone. PRXMATCH was ideal, thanks Bruno.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.