Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Formatting numbers saved in a local and using these in a text output

    Hello community!

    I am having troubles understanding the formatting of numbers, or more likely understanding the syntax.
    I am trying to format some locally saved variables (that's fine so far) and then to output these in a text string.

    Here's what I played around with. The lines in italics behind each ">" show what Stata output I receive.

    local ProbF = 1.5497823654
    local ProbF %9.3f `ProbF'
    local F_stat = 5.6973237484
    local F_stat %9.3f `F_stat'

    di `ProbF' `F_stat'
    > 1.550 5.697

    di "Prob F > P = " `ProbF '" & F-statistic = " `F_stat'
    > Prob F > P = 5497823654" invalid name

    local output "Prob F > P = " %9.3f `ProbF' "& F-statistic = "%9.3f `F_stat'
    di "`output' "
    > invalid syntax


    Can someone please tell me how I achieve the following desired output?

    "Prob F > P = 1.549 & F-statistic = 5.697"

  • #2
    This is close to what you want:
    Code:
    . local ProbF = 1.5497823654
    
    . local F_stat = 5.6973237484
    
    . 
    . display "Prob F > P = " %4.3f `ProbF' " & F-statistic = " %4.3f `F_stat'
    Prob F > P = 1.550 & F-statistic = 5.697
    It differs in outputting 1.550 instead of 1.549. Given that your number is 1.5497..., 1.550 is the correct rounding. If your goal is to actually truncate at three decimal places instead of rounding, that can be done but is a bit more complicated. And it doesn't seem like a reasonable thing to do with these statistics. But post back if that's really what you want.

    In general when using %f formats, if you are not throwing in commas or zero-padding, or deliberately blank-padding, it is best to do them as %a.bf where a = b+1. If more space is needed than that format designates, Stata will override a in order to provide enough room to display the number to b decimal places. The advantage of using this minimalist value of a is that you don't end up with unwanted left blank-padding.

    Comment


    • #3
      To what Clyde writes, let me also note that you can produce local macros with the formatted numbers as the following example shows. For details on : display see the output of help extended_fcn.
      Code:
      . local ProbF = 1.5497823654
      
      . local F_stat = 5.6973237484
      
      . local fmtProbF : display %4.3f `ProbF'
      
      . local fmtF_stat : display %4.3f `F_stat'
      
      . macro list _fmtProbF _fmtF_stat
      _fmtProbF:      1.550
      _fmtF_stat:     5.697
      
      . display "Prob F > P = `fmtProbF' & F-statistic = `fmtF_stat'"
      Prob F > P = 1.550 & F-statistic = 5.697

      Comment


      • #4
        Unbelievable! I was so close!

        Clyde, thanks so much. You solved what I asked for. It doesn't matter to me whether the result is 1.550 or 1.549.
        William, great addition to Clyde's response. Your suggestion reduces my code a little more, which I like, and most importantly adds the tiny thing that with your solution I can actually use the output in the notes of an outreg2 or coefplot. I don't see how this could've worked so easily with the unformatted local variables.

        Many thanks to both of you again. My question is solved.

        Comment


        • #5
          See also https://www.stata.com/statalist/arch.../msg01258.html on terminology.

          Comment


          • #6
            I have a quick follow up about this:

            If I want to assign a string (e.g. "-0.400") to a local macro that is formatted in a particular way, is there a way to preserve the formatting I've just created? Here's what I tried:
            local tester = string(-.4)
            local tester : display %04.3f `tester'
            di "`tester'"
            local tester = string(`tester')
            di "`tester'"
            The first time I display the macro (-0.400) differs from the second time (-.4). Like Thomas, I am trying to format some locally saved variables so that I can write them to a LaTeX file.

            Thank you!

            Comment


            • #7
              You redefined the local macro (not local variable, strictly: see the reference in =5), so it is different. It was the string -0.400, and then second the result of string(-.4), which is the string -.4

              Another way to think about this is to note that


              Code:
              . di string(-0.4, "%04.3f")
              -0.400
              insists on the display format given as second argument, whereas not specifying a format implies use of the default format. It's simple at one level: if you want a particular display format, you may need to specify it explicitly.

              Comment


              • #8
                Thank you, Nick. Redefining my local macro (thanks for the terminology correction) as
                Code:
                local tester = string(`tester', "%04.3f")
                worked perfectly.

                Comment


                • #9
                  Just have to say I came across this after banging my head against a wall for the last two hours: why, oh WHY am I getting a left-padded space in my local macros with decimal places specified. Nick's suggestion of using the rule a=b+1 fixed it beautifully!

                  Comment

                  Working...
                  X