Announcement

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

  • Include note in a graph with a number with specified decimal places from a local macro

    I can't seem to find out how to do this... please help
    I just need to include a note in a graph with a p-value from a previously performed statistical test. The issue arises when I try to format the number in a specific way, namely concerning a specific number of decimal places, and a zero before the decimal separator (e.g. .8899999 -> 0.890).
    I tried checking the format options for variables but it doesn't work with a value in a macro.

    Specific example:
    Code:
    foreach drug in crt mtx mmf iet prost aas ieca bcc pentox nitratos {
    display ""
    display "======== `drug' ======== "
    display ""
    ttest hey2_ct, by(`drug')
    local pvalue=round(r(p),.001)
    local pvaluebon=`pvalue'*8
    if `pvaluebon'>1 local pvaluebon=1
    local vlabel : variable label `drug'
    graph box hey2, over(`drug') ///
    yscale(log extend range(.4)) ylabel(.4 1 2 3) yline(1) ///
    name(`drug', replace) title("`vlabel'") ytitle("Hey2 RQ", size(small)) ///
    note("p=0`pvalue' / p=0`pvaluebon'", size(small)) nodraw
    }
    I kind of solved the "zero before decimal separator" issue by including a "0" in the note text string before the macro call, but it still doesn't work because when `pvaluebon'==1 it becomes "01"...


    Many thanks in advance,

  • #2
    In your note option:
    Code:
    note("p = `:display %05.3f `pvalue'' / p = `:display %05.3f `pvaluebon''")
    should do the trick.

    Comment


    • #3
      That easy..! Thanks a lot Clyde.
      By the way: could you please point me out the reference file to understand the synthax and use of ":" commands? It's the second time I have came up with using one of those but I really don't understand what I am doing, and can't find where to look in help PDF's.

      Comment


      • #4
        The `:macro_fcn' syntax is used to access Stata's macro functions inline, without needing to define a local macro ahead of time. You could achieve the same effect by doing:

        Code:
        local p_one: display %05.3f `pvalue'
        local p_two: display %05.3f `pvaluebon'
        Then placing the following in your note:

        Code:
        note("p = `p_one'" / p = `p_two'")
        More details and documentation on this and four other macro expansion operators (such as `=exp') are contained in the manual entry for [P] Macro, more specifically under the Macro Expansion Operators and Function subheading in the Remarks and Examples section (see here https://www.stata.com/manuals/pmacro.pdf, esp. p.14).
        Last edited by Ali Atia; 15 Mar 2022, 20:41.

        Comment


        • #5
          Excellent! Thank you very much Ali! My best

          Comment

          Working...
          X