# RE: st: dates and Stata

 From "Nick Cox" To Subject RE: st: dates and Stata Date Sun, 31 May 2009 12:31:45 +0100

```You do need such a loop, I think, if you have no data.

But if you have data with daily dates, then -count- is your friend.

. clear

. set obs 20000
obs was 0, now 20000

. gen date = _n

. format date %td

. tsset date
time variable:  date, 02jan1960 to 04oct2014
delta:  1 day

. count if month(date) == 9 & day(date) == 15
55

. count if month(date) == 9 & day(date) == 15 & tin(1may1980, 2dec2007)
28

Nick
n.j.cox@durham.ac.uk

Olexandr O. Vasetsky

it is a brilliant idea - it never occurred to me to use a loop cycle to
shuffle through the days one by one! thank you!

Keith Dear

> Maybe like this?
>
> cap prog drop datecount
> prog datecount
>     args month day start end
>     loc month=upper(substr("`month'",1,3))
>     scalar datecount=0
>     forval d=`=d(`start')'/`=d(`end')' {
>         loc thismonth=upper(ltrim("`:di %dm `d''"))
>         if "`month'"=="`thismonth'" & `day'==day(`d') scalar
> datecount=datecount+1
>     }
>     di as result datecount
> end
>
>
> datecount sept 15 28jan1997 17sep2002
> 6
> datecount Feb 2  28jan1997 17sep2002
> 6
> datecount february 29 28jan1997 17sep2002
> 1

Olexandr O. Vasetsky wrote:

>> I am trying to calculate the number of certain day and month
>> occurrences
>> between the two dates: e.g. calculating the number of September
15th's
>> and
>> February 2nd's from 28-Jan-1997 to 17-Sept-2002.
>>
>> any ideas will be appreciated

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```