 Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: to write _n in [./.] form

 From Christopher Baum To "statalist@hsphsun2.harvard.edu" Subject Re: st: to write _n in [./.] form Date Sat, 26 Feb 2011 07:06:57 -0500

```On Feb 26, 2011, at 2:33 AM, ajjee wrote:

> I have to calculate a variable in this way
>
> gen awc_4=1 if awc[_n]==1 & awc[_n+1]!=1 & awc[_n+2]!=1 &  awc[_n+3]!=1 &
> awc[_n+4]!=1
>
> and on basis of the above variable, generate a new variable of average
>
> gen rgg3avgf=( rgg[_n]+ rgg[_n+1]+ rgg[_n+2])/3 if  awc_4[_n-1]==1
>
> Is there a way to write the above commands in short form ( like, _n/_n+3) as
> for other variables I've to include more terms in calculation of awc_3y and
> also for higher term in averages.

Assuming that as you are testing awc against 1, it can be either 0 or 1, this should do it:

clear
set obs 1000
g t = _n
tsset t
set seed 123456
g awc = (runiform() > 0.2)
g rgg = runiform()
// egenmore from ssc if not already installed
egen awc4 = filter(awc), l(-4/0) normalize
egen rggavg = filter(rgg) if L.awc4 == 1, l(-2/0) normalize

Kit Baum   |   Boston College Economics & DIW Berlin   |   http://ideas.repec.org/e/pba1.html
An Introduction to Stata Programming  |   http://www.stata-press.com/books/isp.html
An Introduction to Modern Econometrics Using Stata  |   http://www.stata-press.com/books/imeus.html

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```