Solved
Senior User
Posts: 1

# 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?

-Jim

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

## 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;``````

All Replies
Super User
Posts: 13,583

## 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: 4,247

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