Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: st: New variable based on a sum of products of another variable
From
Nick Cox <[email protected]>
To
"'[email protected]'" <[email protected]>
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
[email protected]
Seed, Paul
"Seamer Paul" [email protected] asks:
>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/