The reason Stata does it this way is that
the first days of the months are StataCorp's
idea of a nice date list, and on the whole
that seems to me a fair assumption. Equally,
Eric's request is one that should be easy
to satisfy routinely, modulo some programming.
Scott's code crunches through most of
the possibilities, but falls short of
those pesky special cases for leap years.
(The year being divisible by 4 is necessary but
not sufficient.)
Building on his good ideas, an alternative
approach is based on the truism that the
last day of each month is just the day
previous to the first day of the next month.
Then Stata is delegated the job of thinking
about the vagaries of the calendar.
Assuming that the data have been -tsset-, a more
ambitious approach then uses a program to put
the last days in a local macro, with options
for more selection for longer time series.
*! NJC 1.0.0 2 August 2005
program lastdays
version 9
syntax [if] [in] , local(str) [Quarterly Yearly]
quietly {
tsset
if "`r(unit)'" != "daily" {
di as err "`r(timevar)' not daily dates"
exit 498
}
local varlist "`r(timevar)'"
marksample touse
count if `touse'
if r(N) == 0 error 2000
su `varlist' if `touse', meanonly
local min = r(min)
local max = r(max)
tempvar work
gen `work' = year(`varlist') if `touse'
levelsof `work', local(years)
if "`quarterly'`yearly'" == "" local months "1/12"
else if "`quarterly'" != "" local months "1(3)10"
else if "`yearly'" != "" local months "1"
foreach y of local years {
foreach m of num `months' {
local d = mdy(`m',1,`y') - 1
if inrange(`d',`min',`max') local D "`D'`d' "
}
}
}
noi di as txt "`D'"
c_local `local' `D'
end
With -lastdays- installed, the code then could be
sysuse sp500, clear
tsset date, daily
lastdays, local(last)
twoway tsline high low , tlabel(`last', format(%dmd) alt) xtitle("")
Nick
[email protected]
Scott Merryman
> One way would be to use the -tlabel- option, something like:
>
> sysuse sp500, clear
> tsset date, daily
> twoway tsline high low , tlabel(31jan2001 31dec2001,
> format(%dmd) alt)
> xtitle("")
>
> If you have a lot of dates to enter, the following code picks up the
> last day of the month:
>
>
> sysuse sp500, clear
> tsset date, daily
>
> gen year = year(date)
> levelsof year, local(years)
>
> local day31 "Jan", "Mar", "May", "Jul", "Aug", "Oct", "Dec"
> local day30 "Apr", "Jun", "Sep", "Nov"
>
> local month = c(Mons)
> foreach k of local years {
> foreach l of local month {
> if inlist("`l'", "`day31'") == 1 {
> local months "`months'31`l'`k' "
> }
>
> if inlist("`l'", "`day30'") == 1 {
> local months "`months'30`l'`k' "
> }
>
> if mod(`k', 4)==0 & "`l'" == "Feb" {
> local months "`months'29`l'`k' "
> }
> if mod(`k', 4)==1 & "`l'" == "Feb" {
> local months "`months'28`l'`k' "
> }
> }
> }
>
> twoway tsline high low , tlabel("`months'", format(%dmd) alt) xtitle
> ("")
Eric G. Wruck
> > I'm working on some overlaid graphs where the x-axis is the date.
> > The date within the Stata file is always the last day of the
> > month.
> > However, when I go to create my twoway graph, Stata labels the
> > axes
> > with dates that are the first day of the month (e.g., 01 Oct 00) --
> >
> > dates that are not actually found on any record within the file.
> > Why
> > does Stata do this? I will try re-formatting the dates but it
> > doesn't seem to me that I should have to do this. Any thoughts?
*
* 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/