BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
singhsahab
Lapis Lazuli | Level 10

Hi, 

 

i want to extract all last numeric value from a given string. Datasets are given below with input and output.

 

data have;
input string $20.;
cards;
P@123kjh2345
r@ty56kli122
ghjgdj788jkjlk23456
;
run;

data want;
input string $20.;
cards;
2345
122
23456
;
run;

Thank you All 🙂 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi @singhsahab,

 

You can also use the SCAN function to extract the last "word" (second argument -1) of the string, treating all non-digit characters as delimiters (fourth argument 'kd', third argument empty).

data want;
set have;
string=scan(string,-1,,'kd');
run;

View solution in original post

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

How about

 

data have;
input string $20.;
cards;
P@123kjh2345
r@ty56kli122
ghjgdj788jkjlk23456
;
run;

data want(keep = string lastnum);
   set have;
   pid = prxparse('/(\d+)(?!.*\d)/'); 
   if prxmatch(pid, string) then 
      call prxposn(pid, 1, p, l);
   lastnum = substr(string, p, l);
run;

 

Result:

 

string               lastnum 
P@123kjh2345         2345 
r@ty56kli122         122 
ghjgdj788jkjlk23456  23456 
FreelanceReinh
Jade | Level 19

Hi @singhsahab,

 

You can also use the SCAN function to extract the last "word" (second argument -1) of the string, treating all non-digit characters as delimiters (fourth argument 'kd', third argument empty).

data want;
set have;
string=scan(string,-1,,'kd');
run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 3559 views
  • 10 likes
  • 3 in conversation