Bookmark and Share

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: Replace with missing value


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Replace with missing value
Date   Fri, 12 Apr 2013 11:23:36 +0100

You don't specify panel structure, but I will assume that you might
have it. I will assume no gaps in the time series.

bysort id (time) : gen countmissing = sum(missing(y))
by id : gen count30 = countmissing - countmissing[max(1, _n-30)]
replace x = . if count30

The bias here is to avoid looping over observations if you possibly
can. The "Yes, of course!" trick is that the count in the last 30 days
is

cumulative count until now - cumulative count up to 30 days ago

and cumulative counts are easy with -sum()- and an indicator (0, 1) argument.

I say "Yes, of course!" but I learned this trick from Michael Blasnik
on this list some years ago and it was me that went "Yes, of course!".

The code needs to be careful for the first 30 days because then _n -
30 is less than 1.

If you don't have panel structure then

sort time
gen countmissing = sum(missing(y))
gen count30 = countmissing - countmissing[max(1, _n-30)]
replace x = . if count30

If you have irregular time series, you can always fall back on the techniques in

SJ-7-3  pr0033  . . . . . . . . . . . . . .  Stata tip 51: Events in intervals
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q3/07   SJ 7(3):440--443                                 (no commands)
        tip for counting or summarizing irregularly spaced
        events in intervals

which is accessible at http://www.stata-journal.com/sjpdf.html?articlenum=pr0033
Nick
[email protected]


On 12 April 2013 10:35, André Gyllenram <[email protected]> wrote:
> So i have a simple problem. I have a time series and two variables. I have a variable that is nonmissing and i would like to replace it with a missing value if my other variable has a missing value somwhere during the last 30 days. Can anyone help me?
>
> Kind regards
> André
> *
> *   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/

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index