BookmarkSubscribeRSS Feed
mk131190
Obsidian | Level 7

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.

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

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

mk131190
Obsidian | Level 7

It could be any 8 digit number.

 

Thanks,

 

Mark

Shmuel
Garnet | Level 18

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;
Shmuel
Garnet | Level 18
you can use also index(word,'/') = 0 to eliminate dates, but is there other special characters ?
like dots, comma, etc.
mk131190
Obsidian | Level 7

There are also pound values and colons for datetime.

 

Regards,

 

Mark

Shmuel
Garnet | Level 18

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;

andreas_lds
Jade | Level 19

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 13371 views
  • 0 likes
  • 4 in conversation