Search code examples
saslogarithm

Calculate lower and upper log10 value of numbers


I would like to calculate the log10 lower and upper values of a number. E.g. for val=4 it should return 1 as lower and 10 as upper log10 value.

data a;
  infile datalines;
  input val;
  datalines;
4
27
38765
983434
;
run;

This should be the result:

expected result

How can I achieve this and which functions are needed? The function log10 calculates just the value of val to the base of 10.


Solution

  • I'm not a math Ph.D., but log10 does what log10 says it does, namely, calculate the log-base-10 of a number. I've never heard of "lower log10" or "upper log10", nor has Google as far as I can tell.

    What it sounds like you want is the number which would have an integer logarithm (base 10) which is less than or equal to the number given, and the number which would have an integer logarithm (base 10) which is greater than or equal to the number given. (What if the number given is exactly 10, you don't say what to do).

    Easily enough done by re-exponentiating after floor/ceil:

    data _null_;
      input x;
      l_x = 10**(floor(log10(x)));
      u_x = 10**(ceil(log10(x)));
      put x= l_x= u_x=;
    datalines;
    4
    27
    38765
    983434
    ;;;;
    run;