BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
fabdu92
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
ErikLund_Jensen
Rhodochrosite | Level 12

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;

View solution in original post

5 REPLIES 5
anoopmohandas7
Quartz | Level 8

Can you show us your data or if not replicate a dummy of it ?

ErikLund_Jensen
Rhodochrosite | Level 12

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;

FriedEgg
SAS Employee

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.

ballardw
Super User

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?

Astounding
PROC Star

Since you simply want all the digits, a suitably simple approach would be:

 

num = compress(long_string, , 'kd');

 

k = keep

d = digits

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
  • 5 replies
  • 19213 views
  • 3 likes
  • 6 in conversation