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

Hi all,

 

How can I convert a alphanumeric format (character) : 5 x 10 ^ 6 to numeric 5000000.

 

Thanks.

 

Luis.

1 ACCEPTED SOLUTION

Accepted Solutions
hashman
Ammonite | Level 13

@Luis3:

data _null_ ;                                                                                                                           
  c = "5 x 10 ^ 6" ;                                                                                                                    
  n = input (tranwrd (compress (upcase (c)), "X10^", "E"), E32.) ;                                                                      
  put n= ;                                                                                                                              
run ;

Kind regards

Paul D. 

View solution in original post

4 REPLIES 4
hashman
Ammonite | Level 13

@Luis3:

data _null_ ;                                                                                                                           
  c = "5 x 10 ^ 6" ;                                                                                                                    
  n = input (tranwrd (compress (upcase (c)), "X10^", "E"), E32.) ;                                                                      
  put n= ;                                                                                                                              
run ;

Kind regards

Paul D. 

SASKiwi
PROC Star

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;
Tom
Super User Tom
Super User

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. 

hashman
Ammonite | Level 13

@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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 1647 views
  • 3 likes
  • 4 in conversation