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

st: numbering observations

From   Simon Moore <>
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?


Simon Moore
* For searches and help try:

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