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]

st: RE: counting observations


From   "Keith Dear" <[email protected]>
To   <[email protected]>
Subject   st: RE: counting observations
Date   Thu, 30 Dec 2010 15:55:59 +1100

Jurgen,
You say you want to "add" the observations, but apparently you do mean
to count them, not sum them...
Does this do it?

	gen int ym=mofd(obsdate)
	collapse (count)n=obsdate, by(ym)
	format n %6.0g
	tsset ym

	* next two lines unnecessary if every month was present
	tsfill
	replace n=0 if n==.

	gen long c3 = l1.n + l2.n + l3.n	// see -help tsvarlist-

If desired, you could then merge this back onto the original dataset.
kd



-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Jurgen
Sidgman
Sent: Thursday, 30 December 2010 11:33 AM
To: [email protected]
Subject: st: counting observations

Hi,

I have a dataset that has observations for each day of many months. What
I need to do is: for each month I need to add all observations for each
of the previous 3 months. For example, for january I need to add all
observations from October to December from the previous year. Next, for
february I need to add all observations from November to January and so
on. What I have been doing so far is calculating the cumulative value
for each month as follows:

. sort obsdate
. egen count_obs = count(obsdate) if year(obsdate) == 2004 &
month(obsdate) <= 3 (this is for the first time I perform the
calculation)



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