Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[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 <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <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

"Seamer Paul" Paul.Seamer@westmidlands.nhs.uk 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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index