Hello,
I have a SAS chracter variable and I want to extract a specific 8 digits from it that could appear at any point in time. The string may also include dates in the format dd/mm/yyyy so the variable is not just simply 8 digits and loads of characters. Is there anyway of extracting 8 consecutive digits from this string?
Example of the variable string below.
aaaaaaaaaaaaaa 12/01/2016 bbbbbbbbbbb 12345678 nnnnnnn
23456789 aaaaaaaaaaaaaa bbbbbbbbbbbb
01/12/2016aaaaaaaaabbbbb12345678
Thanks in advance.
Are the digits restricted to being 12345678 and 23456789 or could it also be 54839572 eg?
It could be any 8 digit number.
Thanks,
Mark
Adapt next example code to your needs:
data _NULL_;
txt = 'xxxx 897453266 mmmmm 24/08/2015 bbb';
new = compress(txt, ,'A'); /* remove alphabetic characters */
put new=;
n=countw(new);
if n > 0 then do;
do i=1 to n;
word = scan(new,i);
len=lengthn(word);
if input(word,?? 8.) ne .
then put word= len=;
end; end;
run;
There are also pound values and colons for datetime.
Regards,
Mark
Try next code:
data test;
txt = 'xxxx 12345999 mm-mm/m 24/08/2015 08:30';
new = compress(txt, ,'A');
put new=;
n=countw(new);
if n > 0 then do;
do i=1 to n;
word = scan(new,i);
len=lengthn(word);
if indexc(word,'.,/:;$') =0 and /* add any other special character, like pound */
input(word,?? 8.) ne . and len=8
then output;
end; end;
run;
Use prxmatch + prxposn if there is just one number to extract.
data work.want;
set work.have;
length rx number 8;
retain rx;
drop rx;
if _n_= 1 then do;
rx = prxparse('/.*(\d{8,8}).*/');
end;
if prxmatch(rx, string) then do;
number = input(prxposn(rx, 1, string), 8.);
end;
run;
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.