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, is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Weighted moving sum missing values

From   Dorian Garbe <>
Subject   st: Weighted moving sum missing values
Date   Mon, 24 Jun 2013 18:59:12 +0100

Dear all,

I have panel data with panel variable: Id and time variable Yr which
looks as follows:

Yr Id Var1 Var2

2002 1 0 ...
2002 2 3 ...
2003 1 4 ...
2003 2 0 ...
2004 1 1 ...
2004 2 5 ...

Var 2 is supposed to be the cumulated sum of Var1, weighted and over
the past five years.
However if I just use gen by Id: Var2= 1*L1.Var1 + 0.8*L2.Var1 +
0.6*L3.Var1 + 0.4*L4.Var1 + 0.2*L5.Var1 for 2002-2006 this will
generate a missing value, because not all 5 lagged variables are
available. I would like to treat this missing lagged variables as 0 or
just ignore them, so for 2004, Id 1 the value is calculated as Var2=
1*L1.Var1 + 0.8*L2.Var1 = 1*4 + 0.8*0
I also need to do this by Id.

Could I do some sort of nested if, saying If Yr=2002 then Var2=0, if
Yr=2003 then Var2=L1.Var1, if Yr=2004 then Var2= L1.Var1 + 0.8 L2.Var1
....? I think you cannot do this in Stata!?

I have been searching for a solution all day, but could not really
figure it out. I am also an absolute beginner.

Thank you very much,
*   For searches and help try:

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