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]

st: RE: counting observations


From   "Keith Dear" <Keith.Dear@anu.edu.au>
To   <statalist@hsphsun2.harvard.edu>
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: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Jurgen
Sidgman
Sent: Thursday, 30 December 2010 11:33 AM
To: statalist@hsphsun2.harvard.edu
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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index