Announcement

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

  • How to format number variables?

    I am trying to format a number variable *b* if it is less than 1, so Stata will ad a "0" to before the decimal point. Would also like 3 places behind the decimal. Does not seem to be working. Still outputs a variable *b* that has 9 numbers after the decimal. Cross posted this to reddit (https://www.reddit.com/r/stata/comme...er_variables/?).

    Code:
       if b>1{
                        *code here that is not relevant to the question
                }
                else{
                    
                    format b %09.3g
                    
                    *code here that is not relevant to the question
                }

  • #2
    Your if statement evaluates the first observation in b. It is equivalent to

    Code:
    if b[1]>1
    As an aside, the else condition is b <= 1, which does not match your verbal description of "less than 1".

    Anyway, you cannot have a different (fixed) display format for different values within one variable; the display format applies to all values of a variable. You can force three decimal places as

    Code:
    format b %09.3f
    However, many commands that create output have their own format() options to control the layout.

    Comment


    • #3
      Code:
      help format
      Code:
      foreach num of numlist 9.2 0.75 12 .25{
          if abs(`num')<1{
              di %04.3f `num'
          }
          else{
              di %4.3f `num'
          }
      }
      Res.:

      Code:
      . foreach num of numlist 9.2 0.75 12 .25{
        2.
      .     if abs(`num')<1{
        3.
      .         di %04.3f `num'
        4.
      .     }
        5.
      .     else{
        6.
      .         di %4.3f `num'
        7.
      .     }
        8.
      . }
      9.200
      0.750
      12.000
      0.250
      Note: Crossed with #2.

      Comment


      • #4
        As is evident from the two responses so far, terminology is key here.

        I read

        ​​​
        Originally posted by Jay Gold View Post
        a number variable *b*
        as if b is a numeric variable. Andrew reads the same as if b is a (local) macro.

        We need a more precise and complete description of the problem to give useful advise.

        Comment


        • #5
          I don't have best practice here. I am using global and local variables.

          I cannot seem to get the code for -format- to work. Everything else about the code works as intended. I have checked without the issue with displaying `bval' on the graph.
          I still get the error:

          Code:
          format not found
          Code:
          local pval = 1
          
          foreach var of varlist om_muscle_fat_fraction{
              putexcel clear
              regress `x' `var'
              matrix list r(table)
              putexcel set results_F4.xlsx, open sheet (test) modify
              putexcel C`row' = matrix(r(table)), colnames
              local row = `row'+1
              putexcel B`row' = matrix(r(table)), rownames
              local row = `row'+11
              replace y = r(table)[4,1]
              replace b =  r(table)[1,1]
              *putexcel save
              
              if y < 0.05{
                  local pval = round(y,0.001)
                  
                  if b>1{
                      local bval = round(b,0.001)
                          scatter `x' `var' , mcolor(black%50)|| lfitci `x' `var' , ciplot(    rline) blpattern(dash) lcolor(black) ytitle("`: var label `x'' (pg/mL)") xtitle("`: var label `var'' (%)") legend(off) ylab(, nogrid) xlab(, nogrid) caption("p = 0`pval'" "{&beta} = `bval'" , pos(1) justification(right) ring(5))
                  graph export graph.jpg, replace
                  putexcel G`row_graph' = picture (graph.jpg)
                  local row_graph = `row_graph'+22
                  putexcel G`row_graph' = "p =", right
                  putexcel H`row_graph' = y
                  local row_graph = `row_graph'+ 4    
                  }
                  else{
                      local bval = format %04.3f b
                      scatter `x' `var' , mcolor(black%50)|| lfitci `x' `var' , ciplot(    rline) blpattern(dash) lcolor(black) ytitle("`: var label `x'' (pg/mL)") xtitle("`: var label `var'' (%)") legend(off) ylab(, nogrid) xlab(, nogrid) caption("p = 0`pval'" "{&beta} = `bval'" , pos(1) justification(right) ring(5))
            
                      
                  }
              
                      }
                  else {
                      
                  }
              putexcel save
              
                  }
          }

          Comment


          • #6
            Without going through all your code: If you want to display a value in graphs formatted in a special way you should have a look at macro functions for formatting results (e.g. via -h local-). For example,
            Code:
            . local b = 0.0146
            
            . local br : di %05.3f round(`b',.001)
            
            . di "`b' `br'"
            .0146 0.015

            Comment


            • #7
              Thank you Dirk! I was able to adopt that for my graph. Worked exactly as needed!

              Comment


              • #8
                Code:
                local br : di %05.3f `b'
                is all that is needed here. round() with second argument 0.001 is not only redundant at best; it can give bizarre results on occasion because of precision problems.

                Positively put, asking for 3 decimal places via the display format asks for what you want, and so stop there. For more on this slippery, subtle issue see Section 4 of https://journals.sagepub.com/doi/pdf...867X1801800311

                Comment


                • #9
                  Thank you for this!

                  Comment


                  • #10
                    Many thanks Nick, great catch!!

                    Comment

                    Working...
                    X