Search code examples
stata

Lagged range in if statement


I have successfully used lagged values in the "IF" section of a replace. How do I specify a range instead of each individual lagged variables. Iteration is not an option. I have ~10million observations.

{
g buy2=0
replace buy2 = 5 if buy2[_n-1]==0  & buy2[_n-2]==0
}

As expected in buy2, the 0 was replaced with a 5 every third observation.

My problem is I need this from _n-1 - _n-900 without typing each out or iterating.


Solution

  • If buy2 is 5 after runs of 0 of length 900, then you want

    gen buy2 = 0 
    replace buy2 = 5 if mod(_n, 901) == 0 
    

    although that seems a fairly unusual thing to want.