[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

From |
"Nick Cox" <n.j.cox@durham.ac.uk> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
st: RE: Generating new variables with lags |

Date |
Sun, 5 Oct 2008 19:35:52 +0100 |

I can't comment easily on what did not work in your code, as you don't show it. But very likely you are using " " where they don't belong. Not surprisingly, the advice is not to do that. But note further that time series operators in Stata are prefixes, not suffixes. You could try something like this, which may be similar in spirit to what you tried: local lag = <whatever> gen sumx = x qui forval l = 1/`lag' { replace sumx = sumx + l`l'.x } However, there is another approach without looping. For example, the result for lag 3 (i.e. this value PLUS the previous 3 values) can be got without looping: tsset id time tsfill, full bysort id (time) : gen cumx = sum(x) by id : gen x3 = x + cumx - cumx[_n-3] I learnt this trick from Michael Blasnik. Nick n.j.cox@durham.ac.uk Malcolm Wardlaw I'm trying to generalize a program to be able to create a new variable out of a specified number of lagged variables. gen sumx=x+x.l1+x.l2+x.l3 ... I'd like to generalize this to be able to use various lags without having to type out the entire sequence in the program. Essentially, I'd like to specify "lag=3" and generate sumx=x+x.l1+x.l2+x.l3 or "lag=2" and generate sumx=x+x.l1+x.l2 I've tried building this expression into a macro and then evaluating the results of the macro, but as I'm sure the more advanced people know, this doesn't work. Instead it creates a string variable sumx="sumx=x+x.l1+x.l2+x.l3". I've never figured out exactly how to properly evaluate macros as directives within a program, but I don't even know if that's the best way to handle this problem anyways. * * 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/

**References**:**st: Generating new variables with lags***From:*Malcolm Wardlaw <malcolm@mail.utexas.edu>

- Prev by Date:
**st: RE: RE: Efficient coding with -replace-** - Next by Date:
**Re: st: Mediating variables** - Previous by thread:
**st: Generating new variables with lags** - Next by thread:
**st: Mediating variables** - Index(es):

© Copyright 1996–2017 StataCorp LLC | Terms of use | Privacy | Contact us | What's new | Site index |