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

# RE: st: New variable based on a sum of products of another variable

 From Nick Cox To "'statalist@hsphsun2.harvard.edu'" Subject RE: st: New variable based on a sum of products of another variable Date Wed, 18 May 2011 13:26:00 +0100

```Neat.

Nick
n.j.cox@durham.ac.uk

Seed, Paul

>I have a variable 'var2' and want to generate a new variable 'newvar' equal to a sum of products covering
>moving ranges of 'var2' (the values below are not significant, just wanted to keep it simple).

>example:
>var2                       newvar
>2                           2
>4                          (2*4) + 4
>6                          (2*4*6) + (4*6) + 6
>8                          (2*4*6*8) + (4*6*8) + (6*8) + 8
>10                         (2*4*6*8*10) + (4*6*8*10) + (6*8*10) + (8*10) + 10
>...                             ...

This particular example can be calculated iteratively:

clear
set obs 10
gen double var2 = 2*_n
gen double newvar = 2
replace newvar = var2*(newvar[_n-1] +1) if _n > 1
list

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