Announcement

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

  • Does -display- leave anything behind? How does one access the result when using -display- as a hand calculator?

    Good morning,

    I am using -display- as a hand calculator a lot. (Ryan, P. (2004). Stata tip 4: Using display as an online calculator. The Stata Journal, 4(1), 93-93.)

    Major defect of -display- in this hand calculator use (or alternatively something that I have not been able to figure out how to do) is that it does not seem to leave anything behind:

    Code:
    . display 2*100/3
    66.666667
    
    . return list
    
    . ereturn list
    I want to be able to access the answer post -display-, the 66.666667 in the example above. Do you know of any way how to do that?

  • #2
    You can store what display returns into a local (or global) macro:

    Code:
    local result : display 2*100/3
    display "`result'"
    Note that the macro will always contain a string using the requested format. That might or might not be what you want.

    Why not just go with a scalar (or variable) in the first place?

    Code:
    scalar result = 2*100/3

    Edit:

    If you are doing this more often, a two-liner program might be handy:

    Code:
    program calculate , rclass
        return scalar result = `0'
        display return(result)
    end
    That gives

    Code:
    . calculate 2*100/3
    66.666667
    
    . return list
    
    scalars:
                 r(result) =  66.66666666666667
    Last edited by daniel klein; 26 Jul 2020, 02:46.

    Comment


    • #3
      Thank you very much, Daniel !

      Solution 2 with the scalar is self explanatory.

      Can you say a couple of words about Solution 1 and 3, or point me to the relevant reading?

      In Solution 1, what is this, -display-here acts as an extended macro function?

      In Solution 3, I totally have not idea what is going on. The program has just started, and yet you put something in the return by referring to `0'... Then what is -
      display return(result)- ?

      Comment


      • #4
        r-class commands are sometimes a nuisance -- as their results overwrite those of other r-class commands. daniel klein's command could be rewritten to leave behind a scalar or local macro in the caller space.

        Local macro 0: see [U] 18.4

        return(result) Daniel just defined the scalar r(result) to be returned at the end of the program, so he is also at liberty to access the about-to-be-returned scalar for immediate display.

        Comment


        • #5

          return() is described in [P] return https://www.stata.com/manuals/p.pdf — return — Return stored results page 419

          Distinguish between r() (returned results) and return() (results being assembled that will be returned). The program you write actually stores results in return(). Then when your program completes, whatever is in return() is copied to r(). Thus the program you write can consume r() results from other programs, and there is no conflict.

          Comment

          Working...
          X