help for mydatelabels

Axis labels from changes - usually date or time components

mydatelabels
[varname]
[=exp]
,
local(macname)
[
format(format)
start
end
]

Description

mydatelabels generate axis labels with a label every time a
component (hour, month, year) changes. Its use is best explained by examples, as below.

Remarks

When analysing data as sequential cases which occurred at irregular intervals,
it can be more informative to those interpreting the graph to have an axis
(which may be a secodary axis) display the date of the first case for each
hour/month/year etc. By way of example, a clustering of failures may be more usefully interpreted as
between July and September, than as case numbers 73 to 86.

The idea behind mydatelabels is that you provide it with the date or time variable within a date or time function.
Optionally you may also specify the sequence variable to 'map' the change in hour/minute/date to (i.e. whatever numeric variable you are using for your x-axis).
It will then place the appropriate specification in a local macro which you name. You may then use that
local macro as part of a later graph command.

Options

local(macname) inserts the option specification in
local macro macname within the calling program's space. Hence that
macro will be accessible after mydatelabels has
finished. This is helpful for subsequent use with graph or other
graphics commands. This is a required option.

format() specifies a format controlling the labels. see date/time formats along with the examples below.

start include the first observation in the output. default is to omit the first observation.

end include the last observation in the output. default is to omit the last observation unless it represents a change in value.

Examples

Setup data

. set obs 200

. gen sequence=_n

. gen date = td(#1jan2017#) in 1

. replace date = date[_n-1] + int(20*runiform()) in 2/l

. format date %td

. gen outcome = 0 in 1

. replace outcome = outcome[_n-1] + runiform() in 2/l

In use:

. mydatelabels sequence=year(date),local(mymac) start format(%tddd_Mon_CCYY)

. twoway line outcome sequence, xaxis(1 2) xlabel(`mymac', angle(30) axis(2)) xtitle("year (by 1st case)",axis(2))

data over a shorter period of time, best labeled by 1st case for each quarter of calendar year

. replace date = date[_n-1] + int(5*runiform()) in 2/l

. mydatelabels sequence=ceil(month(date)/3),local(mymac) start format(%tdMon_CCYY)

. twoway line outcome sequence, xaxis(1 2) xlabel(`mymac', angle(45) axis(2)) xtitle("",axis(2))

Author

Brent McSharry, Starship Children's Hospital, Auckland New Zealand

brentm@adhb.govt.nz

Acknowledgments

The concept and some code in this script came from Nick J. Cox's excellent findit mylabels

Also see

Online: help for axis_label_options