Announcement

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

  • Putpdf text printing unrounded numbers from a rounded variable

    Hello,

    I am using FRED data to create a pdf report of unemployment information. The goal is to have the pdf state "The current rate is X, which is more/less/the same as last year's average rate of Y." However, Y is printed with multiple decimal points even when I try using the round() function.


    To create the rounded variable:

    gen year = substr(date, 1, 4)
    gen ryear = real(year)
    egen yrave = sum((UNRATE)/12), by(ryear)
    gen yrave_r = round(yrave, 0.1)

    After doing that, when asking Stata to list yrave_r or using codebook and describe, it would seem that the values of yrave_r are rounded to the .1 decimal place. However, then I try printing the number in a pdf:

    putpdf begin
    putpdf paragraph
    putpdf text (yrave_r[1])

    and instead of producing the nice rounded number reported with list, the pdf will include many more decimal points beyond the tenths place (for example 3.9000001 instead of just 3.9). Can someone explain to me why this is and how I can fix it?

  • #2
    Hi,
    I am also seeing the same thing with -putexcel-, and have seen it when using a local macro to print text onto graph (such as adding a P-value to a survival curve) with the options to add text to a graph in the graph-making command.
    So, I don't think it is something specific to -putpdf- .
    I think it has to do with the precision that round() returns -- it cannot actually return exactly 3.9 as a float.
    If possible, try using an option to send the value to -putpdf- in a specified format using nformat() option instead of rounding.

    Scott

    Comment


    • #3
      Hi Jessica,
      After responding last night to your post, I took another look at solving this problem in my own code.
      I think a solution might be to use the function -strofreal()- which allows you to specify the Stata format, and then write the string.
      For example, I sometimes (and unpredictably) would find that results from -summarize- would be written with all the extra digits, even after rounding:

      Code:
                            
      sum hivrna if hiv==1, det
                  if `r(N)'>0 {
                      local p50=round(`r(p50)'/1e3,0.1)
                      local p25=round(`r(p25)'/1e3,0.1)           
                      local p75=round(`r(p75)'/1e3,0.1)
                      putexcel `col'86 =`p50' `col2'86= " (`p25' - `p75')"    //median and interval from p25 to p75
                  }
      However, for the moment this seems to fix the issue:
      Code:
               
        sum hivrna if hiv==1, det
                  if `r(N)'>0 {
                      local p50=round(`r(p50)'/1e3,0.1)
                      local p25=round(`r(p25)'/1e3,0.1)              
                      local p75=round(`r(p75)'/1e3,0.1)
                      putexcel `col'86 =`p50' `col2'86=("(" + strofreal(`p25',"%9.1f") + " - " +strofreal(`p75',"%9.1f") + ")")     ,nformat(0)
                  }
      Probably the round() is not necessary here. So far, no long decimals have shown up.

      Hope that helps,
      Scott

      Comment


      • #4
        The -strofreal()- function worked perfectly. Thanks Scott!

        Comment


        • #5
          As strofreal() applies a display format to get what you want, the previous rounding is pointless.

          Comment

          Working...
          X