Announcement

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

  • Simulation fails when global macro is called

    Hello everyone,

    I am trying to implement a simulation and face an error message. Apparently it has to do with the previously defined global macro that I use in the simulation program. The error can be illustrated by this simple example code:
    Code:
    clear
    set seed 123
    set obs 100
    gen binary = runiformint(0,1)
    gen indep = runiformint(0,100)
    
    // EXAMPLE PROBIT REGRESSION, SAVING ONE REGRESSION OUTPUT VALUE AS A GLOBAL MACRO
    probit binary indep
    global probit_cons _b[_cons]
    
    // SIMULATION PROGRAM
    capture program drop sim1
    program define sim1
    
        // IRRELEVANT EXAMPLE CALCULATION IN THE SIMULATION
        clear
        set obs 10
        gen uni = runiform()
        sum uni, meanonly
        scalar a = r(mean)
    
        // THIS IS THE PROBLEMATIC LINE - WHY?
        gen test = normal($probit_cons)
        
    end
    
    simulate a, reps(10): sim1
    And I get the error message:
    Code:
    no variables defined
    an error occurred when simulate executed sim1
    r(111)
    Usually I have not faced any problems using global macros in the program used by the simulation. So what exactly am I overlooking in this case?

    I would be greatful for any hint.
    Best,
    Tom Storwitz

  • #2
    Your definition put the text _b[_cons] in the global, as you should be able to see by inspection


    Code:
    . global probit_cons _b[_cons]
    
    . mac li
    probit_cons:    _b[_cons]
    That's perfectly legal, just useless for your purpose.

    Assuming that you want the value of the intercept to be inserted you need

    Code:
    global probit_cons = _b[_cons]

    Comment


    • #3
      Ok, sometimes the answers are very simple.
      Thank you very much!!

      Comment


      • #4
        Hi Tom
        In this case, your program example tells the source of the problem.
        The way you defined the macro $probit_cons, it does not contain a number, but the string "_b[_cons]", so when the "sim1" program runs, tries to use the global _b[_cons], but cannot find it, which is why its giving you the error.
        HTH
        Fernando

        Comment


        • #5
          Thank you too, Fernando!

          Comment


          • #6
            I think it's a good question here precisely when _b[_cons] disappears as the expression
            Code:
            normal(_b[_cons])
            is legal. Its disappearance could well be a side-effect of code you aren't showing us.

            Comment


            • #7
              In Stata 16.0 the example code shown in post #1, when pasted into a do-file and run in its entirety, fails at the following point
              Code:
              . simulate a, reps(10): sim1
              _b not allowed when e(b) is not present
              an error occurred when simulate executed sim1
              r(111);
              with a different error message than was reported in post #1.

              Comment

              Working...
              X