Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

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

st: Counting using a rolling time window

From   "Louis, Philippe" <>
To   "" <>
Subject   st: Counting using a rolling time window
Date   Tue, 10 May 2011 13:12:42 +0000

Dear Stata users,

I have the following problem. My dataset contains an id-number, a time variable measured in months, and a binary variable indicating action or no action. The time frame is not the same for each person (i.e. id 1 is monitored between January 2000 and May 2000, id 2 between August 1997 and October 1997, etc.). The action variable records whether an action happened during that month or not.

I would like to create a new variable that counts the number of positive actions (i.e. action variable equal to 1). More specifically, I am looking for a code that would loop through the dataset and that would count the number of action==1 using a three months' time window (i.e. current month + 2 previous months), resetting the counter at every new id. If less than three months are available, the loop should just count the number of action==1 of the available months.

An example:
Id	time		action	count
1	2000m1	1	1
1	2000m2	1	2
1	2000m3	1	3
1	2000m4	0	2
1	2000m5	0	1
2	1997m8	0	0
2	1997m8	1	1
2	1997m9	0	1
2	1997m10	1	2

I have been struggling for a while to write a code that would generate the count variable.

Any help or suggestions would be appreciated.

Philippe Louis

*   For searches and help try:

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