Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: recursive variable for many groups


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: recursive variable for many groups
Date   Thu, 22 Sep 2005 17:57:48 +0100

The first part needs a fix, say 

1. Use -tsset- and exploit Stata's time-series operators.
 
tsset group time
gen x = y if group[_n-1] != group 
replace x = constant * L.x if mi(x) 

Nick 
[email protected] 

Bjorn Persson
 
> Thanks very much, Nick!
 
> Re: Two answers:
> 
> 1. Use -tsset- and exploit Stata's time-series operators.
> 
> tsset group time
> gen x = y
> replace x = constant * L.x
> 
> 2. Use -if- not -in-.
> 
> gen x = y
> bysort group (time): replace x = constant * x[_n-1] if _n > 2
> 
> Nick
> [email protected]
> 
> Bjorn Persson <[email protected]>
> 
> I am trying to create a variable that depends on its lagged 
> values, and
> I received this very helpful tip from Statalist:
> 
> gen x = y in 1
> 
> replace x = constant*x[_n-1] in 2/l(ast)
> 
> I am now wondering how to to this for different groups. That is,
> 
> gen x(i) = y(i) in 1(i)
> 
> replace x(i) = constant*x(i)[_n-1] in 2(i)/l(i)
> 
> for groups i=1,2,3...
> 
> I have tried to sort the data using "by", but the "by" 
> command does not
> allow the "in" specification.

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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