Announcement

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

  • create macro (or mata scalar) from value in a selected row

    I am receiving parameters in a format like

    Code:
    clear
    input str15 parmname float parmvalue
    "j=1"        1.111
    "j=2"        2.111
    "constant"   3.111
    "j=1#t=1"    4.111
    "j=1#t=2"    5.111
    "j=2#t=1"    6.111
    "j=2#t=2"    7.111
    end
    I would like to store the the constant (that is, the value in the parmvalue column for the row where the parmname column takes value "constant"). I can think of two alternatives, neither of which are very appealing:

    (1) preserve-keep/drop-save-restore:

    Code:
    preserve
        keep if parmname=="constant"
        global parm_constant `=parmvalue'
    restore
    (2) select in mata
    Code:
    mata: st_global ("parm_constant", strofreal(select ( st_data (.,"parmvalue"),st_sdata (.,"parmname"):=="constant")) )
    Both of these options are clearly atrocious. If there were some straightforward way to find and store the relevant row in a local, say myrow, then I could do
    Code:
    global parm_constant `=parmvalue[`myrow']'
    which would be ideal in terms of simplicity and readability of the code. Is there such a thing, or some other reasonable way of approaching this problem?

  • #2
    I think the simplest you can get is:

    Code:
    gen obs_no = _n
    summ obs_no if parmname == "constant"
    assert r(min) == r(max) // VERIFY ONLY ONE SUCH OBSERVATION
    global parm_constant = parmvalue[`r(min)']
    By the way, you may have good reasons for using global macros, but in general, local macros are safer as you don't have to worry about clobbering something that was created elsewhere with the same name.

    Comment


    • #3
      Is there only one "constant" in your data set or many? If more than one, are they different from ech other?
      You should:

      1. Read the FAQ carefully.

      2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

      3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

      4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

      Comment


      • #4
        Thanks for your quick responses. Clyde's summ idea is exactly what I was looking for, with the added bonus that it lets me check for a unique match!

        Yes, I have 5-10 parameters analogous to "constant" (with different parmnames but to be treated in the same way), which is part of why I wanted to make my code as transparent as possible here. I also have a few other parameters here that belong in vectors (like the "j=*" above) or matrices (the "j=*#t=*"), but I'm okay with how I'm constructing those in mata for now.

        I think I understand the danger of using globals, but am still considering doing it (since my code is fairly simple, these are my only globals, and I'm prefixing them consistently). I might end up storing these values as globals, locals or mata scalars (which are effectively in the "global" mata environment, right?); I'm not sure yet.

        Thanks again!

        Comment

        Working...
        X