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

Re: st: numbering observations


From   "Eric G. Wruck" <ewruck@econalytics.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: numbering observations
Date   Fri, 15 Apr 2005 11:06:00 -0400

Ok -- I'm not sure why you are interested in count.  If you want to identify weekends & cumulate the incidence numbers, then you can do something along the following lines.

First, put your date in Stata elapsed date form (check out the date(s1,s2) function).

Second, generate the year, week & day of week using other Stata functions (namely year(), week() & dow())

Only keep those observations for the weekends (keep if dow == 5 | dow == 6)

Sort the data by year & week

And finally

collapse (sum) incid, by(year week)


Please note in your example below that May 6, 2000 falls on a Saturday, not a Friday.  Also, this will cumulate the incid values for all Fridays & Saturdays of a given year & week.  In other words, the program doesn't care whether you have Friday only, or say five observations for a given Saturday.

Hope this helps,

Eric


>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/


-- 

===================================================

       Eric G. Wruck
       Econalytics
       2535 Sherwood Road
       Columbus, OH  43209

       ph:      614.231.5034
       cell:    614.327.4182
       fax:     614.231.5039
       eFax:    614.573.6639
       eMail:   ewruck@econalytics.com
       website: http://www.econalytics.com

====================================================
*
*   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