Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: significant digits format


From   Ben Jann <[email protected]>
To   [email protected]
Subject   Re: st: significant digits format
Date   Tue, 12 Jan 2010 00:41:06 +0100

Here's s a -sigdig- command based on code from estout.ado (which is
used by esttab you mentioned; the code was originally inspired by
similar functionality of -outreg2- by Roy Wada). Simply save the code
as sigdig.ado in your working directory or somewhere along the adopath
and then use it as illustrated below.

program sigdig, rclass
    args value fmt
    local d "`fmt'"
    if `"`d'"'=="" local d 3
    capt confirm integer number `d'
    if _rc {
        di as err `"`fmt': invalid format"'
        exit 198
    }
// missing: format does not matter
    if `value'>=. local fmt "%9.0g"
// integer: print no decimal places
    else if (`value'-int(`value'))==0 {
        local fmt "%12.0f"
    }
// value in (-1,1): display up to 9 decimal places with d significant
// digits, then switch to e-format with d-1 decimal places
    else if abs(`value')<1 {
        local right = -int(log10(abs(`value'-int(`value')))) // zeros after dp
        local dec = max(1,`d' + `right')
        if `dec'<=9 {
            local fmt "%12.`dec'f"
        }
        else {
            local fmt "%12.`=min(9,`d'-1)'e"
        }
    }
// |values|>=1: display d+1 significant digits or more with at least one
// decimal place and up to nine digits before the decimal point, then
// switch to e-format
    else {
        local left = int(log10(abs(`value'))+1) // digits before dp
        if `left'<=9 {
            local fmt "%12.`=max(1,`d' - `left' + 1)'f"
        }
        else {
            local fmt "%12.0e" // alternatively: "%12.`=min(9,`d'-1)'e"
        }
    }
    local value: di `fmt' `value'
    local value: list retok value
    di as res "`value'"
    ret local value "`value'"
end

Example:

. sigdig _pi 3
3.142

. sigdig _pi/100 3
0.0314

. return list

macros:
              r(value) : "0.0314"


Best,
ben




On Mon, Jan 11, 2010 at 9:03 PM, Jacob Wegelin <[email protected]> wrote:
> The -string- command can be combined with a format to render a number as a
> string with a specified number of decimal places. For instance:
>
> . display string(_pi, "%9.2f")
> 3.14
>
> . display string(_pi / 100, "%9.2f")
> 0.03
>
> . display string(_pi / 100, "%9.4f")
> 0.0314
>
> . display string(_pi * 100, "%9.4f")
> 314.1593
>
> Am I correct in believing that no comparable method exists for generating a
> string that shows a specified number of significant digits?
>
> For example, suppose the function was called -sigdig-. Then we would get the
> following results:
>
> display sigdig( _pi, 3 )
>
> 3.14
>
> display sigdig( _pi / 100, 3 )
>
> .0314
>
> display sigdig( _pi * 100, 3 )
>
> 314
>
> A google search brings up -esttab-, which evidently creates nice tables of
> output from regression models. I had in mind however a more general tool.
>
> Thanks for any pointers
>
> Jacob A. Wegelin
> Assistant Professor
> Department of Biostatistics
> Virginia Commonwealth University
> 730 East Broad Street Room 3006
> P. O. Box 980032
> Richmond VA 23298-0032
> U.S.A. URL: http://www.people.vcu.edu/~jwegelin
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index