- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi all,
How can I convert a alphanumeric format (character) : 5 x 10 ^ 6 to numeric 5000000.
Thanks.
Luis.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data _null_ ;
c = "5 x 10 ^ 6" ;
n = input (tranwrd (compress (upcase (c)), "X10^", "E"), E32.) ;
put n= ;
run ;
Kind regards
Paul D.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data _null_ ;
c = "5 x 10 ^ 6" ;
n = input (tranwrd (compress (upcase (c)), "X10^", "E"), E32.) ;
put n= ;
run ;
Kind regards
Paul D.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Alternatively:
data _null_;
char_num = '5 x 10 ^ 6';
num = input(scan(char_num, 1, ' '), best12.) * (input(scan(char_num, 3, ' '), best12.) ** input(scan(char_num, 5, ' '), best12.));
put _all_;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Both answers above look good.
But note there is no need to use E informat instead of the normal numeric informat. In fact E, D, F and BEST are just alias for that informat.
Also the concept of "best" informat doesn't even make sense. I suspect that people get confused because there is a FORMAT named BEST. You use that when you want SAS to make a decision about how many digits to show before/after the decimal place and whether to use exponential notation to best display the number in a specific number of characters. But when reading text into a number there is no decision to be made. There is no "best" way to store a number. A number is just a number.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@Tom:
>But when reading text into a number there is no decision to be made.<
Yeah, there is, to wit: How to best interpret the text according to the rules sewn into the informat in order to arrive at a number. That involves a lot of logic; and the latter is the vehicle through which decisions are made. In this sense, "best" is semantically quite proper, as textual notations denoting quantities come in different forms, and a numeric informat is trying to make the best decision at how to interpret a given form. So, there's nothing wrong, IMO, in calling the standard numeric informat BESTw.d.
>There is no "best" way to store a number.<
Yeah, there is, and it depends on a cornucopia of factors, related to (a) the physical properties of the computing platform and (b) how the number is going to be used. The latter is the reason why different numeric data types exist (and SAS is no exception if we don't limit it to Base where the only type is RB8). Aside from it, a numeric informat makes no decisions on how numbers are stored. Its role is to decide how to interpret a bunch of characters as a number and, if the attempt is successful, pass it to the SAS facility that physically stores it according to the data type of the variable to which it is assigned.
Kind regards
Paul D.