DATA Step, Macro, Functions and more

Extract numbers from a string

Reply
Occasional Contributor
Posts: 19

Extract numbers from a string

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.

PROC Star
Posts: 746

Re: Extract numbers from a string

Are the digits restricted to being 12345678 and 23456789 or could it also be 54839572 eg?

Occasional Contributor
Posts: 19

Re: Extract numbers from a string

It could be any 8 digit number.

 

Thanks,

 

Mark

Trusted Advisor
Posts: 1,566

Re: Extract numbers from a string

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;
Trusted Advisor
Posts: 1,566

Re: Extract numbers from a string

you can use also index(word,'/') = 0 to eliminate dates, but is there other special characters ?
like dots, comma, etc.
Occasional Contributor
Posts: 19

Re: Extract numbers from a string

There are also pound values and colons for datetime.

 

Regards,

 

Mark

Trusted Advisor
Posts: 1,566

Re: Extract numbers from a string

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;

Super Contributor
Posts: 345

Betreff: Extract numbers from a string

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;
Ask a Question
Discussion stats
  • 7 replies
  • 807 views
  • 0 likes
  • 4 in conversation