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
njcoxstata@gmail.com
On 12 April 2013 10:35, André Gyllenram <a_gyllenram@hotmail.com> 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/