Hi all,
I have a quite long string, with somewhere inside the string: a number, composed by a various number of digits.
I need to extract the complete number.
I am sure that on the right there is at least one character, on the left too.
All characters apart from the number are not digits.
Do you know how I can do it?
Thanks
Hi
Many different methods. I find prxchange most useful for that:
data _null_;
length num $12;
str = 'ssfdfsf56789qeqeqd';
num = prxchange('s/(\D*)(\d*)(\D*)/$2/',-1,str);
put num=;
run;
Can you show us your data or if not replicate a dummy of it ?
Hi
Many different methods. I find prxchange most useful for that:
data _null_;
length num $12;
str = 'ssfdfsf56789qeqeqd';
num = prxchange('s/(\D*)(\d*)(\D*)/$2/',-1,str);
put num=;
run;
setting assurances for matching the whole string would be better, on the off change more that one string of numbers happens to occur in a row of the data.
num = prxchange('s/^\D+(\d+)\D+$/$1',1,str);
also, since OP states there is definitely at least one char to left and right, can use + instead of * to further validate expected input.
Does your data have date values along with the number string you are searching for?
How about identifiers that may include digits such as Highway 66, addresses, telephone numbers or similar?
Since you simply want all the digits, a suitably simple approach would be:
num = compress(long_string, , 'kd');
k = keep
d = digits
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.