Announcement

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

  • Looping over a lagged variable with macro

    Dear all,

    I would like to run a regression testing different lag structures. In order to do so, since I have numerous models I'd like to test, I am trying to use macro but with scarce results.

    More in detail (code below), I would like to regress my dependent variables strin_N and strin_M different lags of ks_C. In addition, since i need to run a lot of regression, I'd like to specify my explanatory variables using a global macro (in the example below Xforeign_KS). I tried the following command but it does not work. Stata does use the correct lags. Do you have any idea about why this is happening?

    Thanks,
    Enrico


    forvalues i=0(1)2 {

    global Xforeign_KS ks_A ks_B l`i'.ks_C

    xtnbreg y strin_N $Xforeign_KS $Xcontrol i.year
    outreg2 using modelN_lag`i'.doc, append ctitle(N.B. BVGR ) addtext(M)
    xtnbreg y strin_M $Xforeign_KS $Xcontrol i.year
    outreg2 using modelM_lag`i'.doc, append ctitle(N.B. BVGR - GREY) addtext(N)

    }
    *

  • #2
    Your macros seem to work fine when I try:

    Code:
    . clear*
    
    . forvalues i=0(1)2 {
      2. 
    .         global Xforeign_KS ks_A ks_B l`i'.ks_C 
      3. 
    .         display `"$Xforeign_KS"'
      4. }
    ks_A ks_B l0.ks_C
    ks_A ks_B l1.ks_C
    ks_A ks_B l2.ks_C
    Since the macro expansion seems to work just fine, I suspect the problem lies elsewhere, in aspects of the code or data that you have not shown us.

    Just knowing that you are not getting what you expect doesn't help nearly as much as knowing what you are getting. Why don't you show us some more of the code (in particular, the creation and any modifications of global macro $Xcontrol), and also show us the output you are getting from -xtnbreg-.
    By the way, it would be safer to have Xforeign (and Xcontrol) as local macros rather than global, but I do not think that is the source of your problem here.

    Finally, please post your code in a code block, as I have done here. The results are much more readable that way. If you don't know how to create a code block, you can find instructions in FAQ #12. This applies also to showing Stata output. It is also important to reproduce both the code and the output exactly, as the devil is often in the details. To assure that no subtle changes are made to either, you should not retype anything. Rather, copy from the Results window or your log file and paste directly into the code block.

    Comment


    • #3
      Dear Clyde, thanks a lot for your prompt answer and suggestions. Next time I will follow your indications on how to post correctly on Stata list. You were right, I went though all my code and i found a bug somewhere else. Thanks a lot for you help.

      Best,
      Enrico

      Comment

      Working...
      X