Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: numbering observations


From   Simon Moore <simoncmoore@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   st: numbering observations
Date   Fri, 15 Apr 2005 13:37:54 +0100

I have data of the form:

date dw incid
6/may/00 friday 5
7/may/00 saturday 9
13/may/00 friday 10
14/may/00 saturday 12

dw is day of week and incid is the number of violent incidents recorded in police stats.

I wanted to collapse by weekend (here assuming a weekend is a Friday and Saturday) and therefore added in the following id (count):

gen count = 0

local cntr = 1
forval i = 1(2)`=_N' {
qui replace count = `cntr' in `i'
local cntr = `cntr' + 1
}
local cntr = 1
forval i = 2(2)`=_N' {
qui replace count = `cntr' in `i'
local cntr = `cntr' + 1
}

which gives:

date dw incid count
6/may/00 friday 5 1
7/may/00 saturday 9 1
13/may/00 friday 10 2
14/may/00 saturday 12 2

and then:

collapse (sum) incid (min) date, by(count)

I have two questions. First, I am certain I have seen a more elegant way of creating the count id posted on Statalist but searching has not revealed anything. Is there one?

More importantly, if, for some reason, there is a missing saturday in the data (there isn't at the moment but it is a growing dataset) the collapse ..., by(count) will not work. Is there an easier way of numbering successive weekends in Stata?

Regards

Simon Moore
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/




© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index