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

# Re: st: generate the level series from the growth rate series

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: generate the level series from the growth rate series Date Fri, 4 Jan 2013 20:34:10 +0000

```Good. The credit really belongs to -by:- and -sum()-.

Nick

On Fri, Jan 4, 2013 at 2:51 PM, annoporci <annoporci@gmail.com> wrote:
>> Evidently, levels can be reconstructed from changes given an initial
>> value:
>
>
> This is very helpful. Thanks very much Nick.
>
> I was about to send in my own best effort. It's redundant now of course.
>
> I followed a roundabout way, so I'm thrilled to see how you did it shorter.
>
> I had problems putting it into a foreach loop. Adapting your code may well
> solve those problems too (see Version 3 below).
>
> Version 1 has no loop and works.
> Version 2 has a loop and works.
> Version 3 has a loop, uses tempname, works but is a real mess.
>
>
> /* Creating a "Level" Index from the "Change" or "Rate of Change" */
>
>  /// Without a loop
>  clear all
>  sysuse sp500
>  gen growth = change[_n]/close[_n-1]
>
>  gen close2 = .
>  replace close2 = 1283.27 in 1
>  gen t = _n
>  tsset t
>  replace close2 = close2[_n-1]*(1+growth[_n]) if t>1
>
>
>  /// In loop without tempname
>  clear all
>  sysuse sp500
>  sort date
>  gen growth = change[_n]/close[_n-1]
>  local ini = 1283.27
>  local variables close
>  foreach var of varlist `variables' {
>    gen `var'2 = .
>    replace `var'2 = `ini' in 1
>    gen t = _n
>    tsset t
>    replace `var'2 = `var'2[_n-1]*(1+growth[_n]) if t>1
>    drop t
>  }
>  ///
>
>
>  /// In loop with tempname.
>  /// It seemed like a good idea to use tempname, but probably wasn't!
>  clear all
>  sysuse sp500
>  sort date
>  gen growth = change[_n]/close[_n-1]
>  local ini = 1283.27
>  local variables close
>  local t _n
>  foreach var of varlist `variables' {
>    tempname `var'2
>    gen ``var'2' = .
>    replace ``var'2' = `ini' in 1
>    tempname tt
>    gen `tt' = `t'
>    tsset `tt'
>    replace ``var'2' = ``var'2'[`tt'-1]*(1+growth[`tt']) if `tt'!=1
>    gen `var'2 = ``var'2'
>  }
> ///
>
>
>
> Let me look into your code Nick, and learn from it.
>
> :-)
>
> ---
> Patrick Toche.
>
>
>> . sysuse sp500
>>
>> . gen close2 = close[1] + sum(change)
>> . assert close == close2
>>
>> If in your real data you have _percent_ changes (assuming equally
>> spaced times and no missings or omissions):
>>
>> gen double level = 100
>> replace level = level[_n-1] * (1 + pcchange / 100) if _n > 1
>>
>> This generalises to panels:
>> bysort panelid (time) : replace level = level[_n-1] * (1 + pcchange /
>> 100) if _n > 1
>>
>> Nick
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```