Calculate Order of Magnitude of a Numeric Variable

Accepted Solution Solved
Reply
Senior User
Posts: 1
Accepted Solution

Calculate Order of Magnitude of a Numeric Variable

Hello,

 

Is there a way to compute the order of magnitude of a numeric variable? For example, I'm looking for a function that I can call like

y = 1234.56;
x = order_mag(y);

 

In this case, x would be assigned 3 since y = 1.23456 * 10^3 in scientific notation. Alternatively, a function that would assign 1000 to x would also be helpful.

 

Does anyone know how to go about doing this?

 

Thanks in advance!

 

-Jim


Accepted Solutions
Solution
‎09-06-2016 08:47 AM
SAS Super FREQ
Posts: 3,390

Re: Calculate Order of Magnitude of a Numeric Variable

The number of integer digits in a number y is given by 

k = ceil(log10(y+1));

 

See the article "Compute the number of digits in an integer."

 

For the magnitude, subtract 1:

power = ceil(log10(n+1)) - 1;

 

 

data A;
y = 1234.56;
power = ceil(log10(y+1)) - 1;
magnitude = 10**power;
run;

proc print; run;

View solution in original post


All Replies
Grand Advisor
Posts: 10,196

Re: Calculate Order of Magnitude of a Numeric Variable

[ Edited ]

Does this work for you (2 ways)

 

data junk;
   input x;
   order_mag= input(scan( put(x,e.),2,'+'),best.);
   y= floor(log10(x));
datalines;
1
20
300
4000
50000
600000
;
run;

 

 

Solution
‎09-06-2016 08:47 AM
SAS Super FREQ
Posts: 3,390

Re: Calculate Order of Magnitude of a Numeric Variable

The number of integer digits in a number y is given by 

k = ceil(log10(y+1));

 

See the article "Compute the number of digits in an integer."

 

For the magnitude, subtract 1:

power = ceil(log10(n+1)) - 1;

 

 

data A;
y = 1234.56;
power = ceil(log10(y+1)) - 1;
magnitude = 10**power;
run;

proc print; run;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 256 views
  • 0 likes
  • 3 in conversation