BookmarkSubscribeRSS Feed
animesh123
Obsidian | Level 7

Extract numeric value from a string

String=a1b2c3df456

 

15 REPLIES 15
PaigeMiller
Diamond | Level 26

What numeric value do you want to extract? What are the rules to do this extraction?


Can you give us a few more examples of strings where you want to extract the numeric values?

--
Paige Miller
animesh123
Obsidian | Level 7
Hey @PaigeMiller the output I am expecting here is - 123456,
I think we can do it using compress function.
but could you please tell me if its even possible to try the same using _numeric_?
PaigeMiller
Diamond | Level 26

So what are the rules for extracting? You didn't say

 

I think we can do it using compress function.
but could you please tell me if its even possible to try the same using _numeric_?


I don't understand the second part of the question.

--
Paige Miller
animesh123
Obsidian | Level 7
String=a1b2c3df456
I need the output as - 123456
Also , I was just wondering if we can pull the numeric variable from string using _NUMERIC_(I am not sure if this is possible or not using _numeric_)
PaigeMiller
Diamond | Level 26

You can use COMPRESS

 

numbers = compress(string,,'dk');

 

I'm still not sure what you mean when you refer to _NUMERIC_, you can't use _NUMERIC_ in the COMPRESS function, and this variable in SAS would not be a numeric variable anyway, it has to be a character variable. Do you mean that you have a lot of variables and you want to do this for all of them?

--
Paige Miller
animesh123
Obsidian | Level 7
Thanks @PaigeMiller .For the _NUMERIC_ function I was hoping we could use only this function (_NUMERIC_ ) to extract numeric variable from string.
PaigeMiller
Diamond | Level 26

But these are character variables, not numeric.

 

I am going to give you some advice, since you have not explained what you want to do here, but you keep mentioning _NUMERIC_ as something to do. ADVICE: don't tell use what object of SAS code you want to use (like _NUMERIC_) and then stop and give no further explanation why you want it. Tell us what you want to do, rather than the SAS code object that you want to use. You have not told us what you want to do. And without that, we can't advise, we can't move forward, we can't get you closer to achieving the goal.

--
Paige Miller
animesh123
Obsidian | Level 7
Hey @Paige Miller Appreciate your advice , but I am just learning I was wondering if we could do the extract using _numeric_ but ideally we can't as explained below.
But yeah Thank you very much for helping out really appreciate
PaigeMiller
Diamond | Level 26

@animesh123 wrote:
Hey @Paige Miller Appreciate your advice , but I am just learning I was wondering if we could do the extract using _numeric_ but ideally we can't as explained below.
But yeah Thank you very much for helping out really appreciate

I keep asking you to explain what you are trying to do ... but you don't explain, you just keep saying you want to use _NUMERIC_. There may be other ways to do what you want to do that don't use _NUMERIC_, but without an explanation of what you are trying to do ... 

--
Paige Miller
animesh123
Obsidian | Level 7
Got it , If there are any other ways we can do the same , could you please let me know .How we can do the same?
FreelanceReinh
Jade | Level 19

@PaigeMiller: I think the reason for the focus on alternative approaches (like using _NUMERIC_) was that the actual problem had already been solved in a parallel thread with almost the same subject line. Just the follow-up question about _NUMERIC_ was (and is) still open there.

 

@animesh123: Please don't double-post. As you see, it leads to unnecessary confusion and wastes resources.

Tom
Super User Tom
Super User

@animesh123 wrote:
Thanks @PaigeMiller .For the _NUMERIC_ function I was hoping we could use only this function (_NUMERIC_ ) to extract numeric variable from string.

There is no _NUMERIC_ function.

 

The keyword _NUMERIC_ can be used where you could enter a list of variable names.  For example in an ARRAY definition. Or a VAR or other statement that needs a list of variable names.  Or it can be used with the OF keyword in function call to a function that can accept a variable number of arguments.

array nums _numeric_;
var _numeric_;
nmiss = nmiss(of _numeric_);
ballardw
Super User

@animesh123 wrote:
Hey @PaigeMiller the output I am expecting here is - 123456,
I think we can do it using compress function.
but could you please tell me if its even possible to try the same using _numeric_?

 

_Numeric_ refers to the list of variables that are of numeric type in a data set. Since your value starts with non-digit characters it must be character an in no way would respond to the places where _numeric_ may be used.

animesh123
Obsidian | Level 7
Thanks @ballardw for explaining, Just a request could you show me some example where we could use _numeric_ function

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 15 replies
  • 3062 views
  • 3 likes
  • 5 in conversation