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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.