Announcement

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

  • Bug when calling _n-1 in a for loop using a local macro

    I am trying to run a for loop that calls [_n-1] of a variable that takes a local macro as part of its name. It produces the error message, "1 invalid name". Is this a known bug in the software? I haven't yet figured out a workaround other than manually coding each instance. An example is below.


    clear
    set obs 1000
    gen seq = _n
    gen val = runiform()
    replace val=(val<=.5)
    tab val

    forvalues i=1/10 {
    gen so_`i'=.
    }

    forvalues i=1/10 {
    local j `i'-1
    replace so_`i' = 1 if val==1 & so_`j'[_n-1]==1
    }



  • #2
    Code:
    local j `i'-1
    evaluates to

    Code:
    local j 1-1
    which then implies

    Code:
    replace so_1 = 1 if val==1 & so_1-1[_n-1]==1
    You want

    Code:
    local j = `i'-1

    Comment


    • #3
      Thank you! That did the trick.

      Comment

      Working...
      X