# st: RE: about 'forvalues' loop

 From "Nick Cox" <[email protected]> To <[email protected]> Subject st: RE: about 'forvalues' loop Date Tue, 8 Sep 2009 12:42:03 +0100

```No need to apologise for sending questions. But see the FAQ for one view

0. A problem that will bite
============================

The syntax `i-1' won't work. It would need to be `=`i' - 1'

That is legal, but in your case it looks as if you have various other
problems that affect your code earlier.

1. A problem that won't bite
============================

In

bysort cy: gen kstck = 0

the -bysort- does no harm, but it does nothing. The result is 0,
regardless of -cy-.

2. A general problem
====================

Reshaping to wide will on balance make your life _much_ more difficult
here.

It looks as if a better approach would be something like

gen kstck = 0
tsset cy yr
by cy: replace kstck = 0.93 * L.kstck + L.gfc

Nick
[email protected]

Marwan Elkhoury

sorry again to bother you with some very stupid question but I'm
getting stuck again on some simple matter that I believe is in the
writing of scalar.

the story is that I have a panel data and I'ld like to generate a new
variable called kstck (by country (cy)).

so I run this loop:

bysort cy: gen kstck = 0
reshape wide gdp gfc kstck, i(cy) j(yr)

* Calculate capital stock from 1990 to 2000 using the following
formula (where gfc* is the investment in year *)

bysort cy: replace kstck1990 = gfc1990

forvalues i = 1991/2000 {
bysort cy: replace kstck`i' = .93*kstck`i-1' + gfc`i'
}

However, it is not giving me the required result of the recursive
summation.  why ?

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