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

# Re: st: Looping through observations

 From Maarten buis To statalist@hsphsun2.harvard.edu Subject Re: st: Looping through observations Date Tue, 23 Nov 2010 09:03:32 +0000 (GMT)

```--- On Mon, 22/11/10, Laurie Molina wrote:
> The data set is about the time required achive a desired
> amount of saving. For each observation in the data set, i
> have income, and saving rate. But future income depends
> on the saving rate.

I am guessing you are dealing with a regular compound interest
problem. Income is confusing terminology in that case, so I
change that to value (v). To make explicit that v is a function
of time I use v_t. I assume that there are three knowns: the
initial savings (v_0), the goal (v_g), and the interest rate
(i), and that you want to know how long it takes to reach that
goal (t) given those three knowns.

Start with writing out how the value at time t depends on v_0,
i, and t:

v_t = v_0*(1+i)^t

Set v_t equal to v_g:

v_g = v_0*(1+i)^t

Take the logarithm of both the left hand side and the right hand
side:

ln(v_g) = ln(v_0*(1+i)^t)

Remember some basic rules for dealing with logarithms:

ln(a*b) = ln(a) + ln(b)
ln(a^b) = b*ln(a)

So:

ln(v_g) = ln(v_0) + t* ln(1+i)

Now you can see that you can compute t without resorting
to a loop:

t = [ln(v_g) - ln(v_0)]/ln(1+i)

In Stata you can implement that using a regular -generate-
command:

*----------- begin example --------------
// create some data
drop _all
input v_0  v_g  i
20   100  .02
500  1000 .015
100  200  .02
end

// compute the time till the goal is reached
gen t = (ln(v_g) - ln(v_0))/ln(1+i)

// display the results
list
*----------- end example ----------------
(For more on examples I sent to the Statalist see:
http://www.maartenbuis.nl/example_faq )

Hope this helps,
Maarten

--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany

http://www.maartenbuis.nl
--------------------------

*
*   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/
```