# Re: st: recursively defined variables

 From Bjorn Persson <[email protected]> To [email protected] Subject Re: st: recursively defined variables Date Fri, 16 Sep 2005 14:29:45 -0400

```Bjorn Persson wrote:

```
Dear Statalist,

I received a very helpful answer (from Nick Cox) to the problem I posted yesterday re recursive variables, as well as a reference to Roger Newson's useful Stata Tip in Stata Journal 4(4) 2004. However, when I try to replicate the examples in Newson's note I keep getting the error message " obs.nos. out of range" after the command:

replace y = y[_n-1] +... (from Newson's Fibonacci sequence example)

Any ideas on what's going wrong here?

(Sorry if this is a double posting - my first mail seems not to have gone through)

Best,
Bjorn

Nick Cox wrote:

This notation I understand to mean
xt -> x at time t xt-1 -> x at time t - 1
x1 -> x at time 1

If so, you initialise
gen x = z in 1
and then you replace
replace x = b * x[_n-1] + z in 2/l
where I guess b is a place-holder for a number you know and note that "l" stands for "l"ast.
The principle is given by the title
of a useful Stata Tip by Roger Newson:
generate and replace use the current sort order. Stata Journal 4(4):484--485 (2004)

Nick [email protected]
[email protected]

I was wondering if anybody could help me to define a variable
recursively in stata? That is, I would like define each observation x
as the sum of a flow variable, z, and the lagged x variable,
discounted by a factor b:

xt = zt + b*xt-1

The three first x variables would then be:

x1 = z1

x2 = z2 + b*z1

x3 = z3 + b*z2 + b^2*z1

and so on).

```*
*   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/
```
Dear Statalist,

Please ignore the previuos message re recursive variables, I discovered the error I made.

Best
Bjorn
*
* 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/