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: Weighted moving sum missing values


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: Weighted moving sum missing values
Date   Mon, 24 Jun 2013 19:27:41 +0100

The presumption that missing means zero is a very strong one but I am
trusting that there is a very, very strong defence.

That said you just need terms such as

cond(missing(L1.var1), 0, L1.var1)

rather than

L1.var1

Alternatively, why not work with a clone

clonevar fudged_var1 = var1
replace fudged_var1 = 0 if missing(var1)

Nick
njcoxstata@gmail.com


On 24 June 2013 18:59, Dorian Garbe <dorian.garbe@gmail.com> wrote:

> 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.
>
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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