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]

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

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

```I think you already have the main idea. You'll be aware of the fact that using ln() requires positive values and that such products can get very big. One extra trick is illustrated below.

. set obs 10
obs was 0, now 10

. gen var2 = 2*_n

. l

+------+
| var2 |
|------|
1. |    2 |
2. |    4 |
3. |    6 |
4. |    8 |
5. |   10 |
|------|
6. |   12 |
7. |   14 |
8. |   16 |
9. |   18 |
10. |   20 |
+------+

. gen double product = exp(sum(ln(var2)))

. gen double product2 = exp(sum(ln(var2)) - sum(ln(var2[_n-3])))

. l

+-----------------------------+
| var2     product   product2 |
|-----------------------------|
1. |    2           2          2 |
2. |    4           8          8 |
3. |    6          48         48 |
4. |    8         384        192 |
5. |   10        3840        480 |
|-----------------------------|
6. |   12       46080        960 |
7. |   14      645120       1680 |
8. |   16    10321920       2688 |
9. |   18   1.858e+08       4032 |
10. |   20   3.716e+09       5760 |

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

Seamer Paul

I'm trying to create a new variable based on a sum of products covering a moving range of another variable,

I think this will be clearest if I use an example,

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).

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
...                             ...

```

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index