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

st: RE: displaying date but also the time!

From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: displaying date but also the time!
Date   Wed, 18 Dec 2002 13:31:06 -0000

Iwan Barankay

> I am at a loss on how to display dates that also contain the time of
> day.
> I converted data from Exel where an entry is the date but also the
> time.
> That is I had a variable 'datetod' with an entry like "11/04/2002
> 08:00:00"
> which converted into "15441.33333"
> then I wanted to display it but Stata only let's me display the date
> but not the time, i.e. I can only do
> format datetod %dD_m_Y
> which then displays only as "11 Apr 02" (e.g. on graphs) but I would
> like it to display as something like
> "11 Apr 02 08:00:00" that is also the time!
> is there a way to do this?
> A thousand thanks and have a nice end of year break (whatever the
> time...),

No and yes.

Official Stata offers nothing directly for time of day formats,
so far as I know.

But you can knit your own or see what users have done.

In a thread yesterday, I drew attention to a bundle
of stuff, mostly written by Kit Baum, in the -egenmore-
package on SSC. At first glance, none of those provide
exactly what you want, but moving in your direction is not
too difficult. For display, a string variable will
often be fine.

You want first a string equivalent of your daily

. gen str1 sdatetime = ""
. replace sdatetime = string(date,"%dd_m_Cy")

Then you need to concatenate with a string time of
day variable in the form "hh:mm:ss".

. replace sdatetime = sdatetime + " " + stod

Alternatively, you need to go back to Excel,
and just copy the original column to Stata as
a string variable. It is almost what you want,
modulo some surgery.

Suppose a string variable -excellike- contains dates

"11/04/2002 08:00:00"

something like this maps to your format:

tokenize Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
forval i = 1/12 {
	local j : di %02.0f `i'
	qui replace excellike = /*
	*/ subinstr(excellike,"/`j'/","``i''",.)
replace excellike = subinstr(excellike,"200","0",.)

However, these string variables cannot be used
in graphs, which you also mention.

Incidentally if, on a graph, one label is

"11 Apr 02 08:00:00"

then there won't be room for many others!

I just recently drafted an FAQ on nice time labels,
which I have not yet submitted to Stata Corp.
I give it below without translating directly
to your example, not least because it may
interest others.

<begin of draft FAQ>

How can I get "nice" time of day labels on a graph?

1. The problem

If you are graphing data in Stata against time of day,
one of your axes will show time. For all but the crudest
exploratory graphics, good labeling of that axis is
important. Tastes and conventions may vary, but many
people like to show times for whole hours and in the
form 12:00. Sometimes people like to distinguish times
that are a.m. and times that are p.m. These may be
called "nice" or "round" labels and go beyond what
may be achieved by using -format-.

A companion FAQ addresses the question of date labels
for daily dates:
How can I get "nice" date labels on a graph?

The main trick suggested is to use -foreach- to define
value labels including the required text. A detailed
tutorial on this command is given in
Stata Journal 2(2), 202-222 (2002).
For labels, see the on-line help for -label-.

In what follows, we assume that time goes on the x
axis. If you put time on the y axis, you just need
to think in terms of the corresponding options;
that is, for -xlabel-, read -ylabel-, and so forth.
We also assume that time is measured on a scale
running from 0 (usually midnight) to, or almost to,
24 (usually also midnight). If your time origin
is not midnight, you will need to modify the recipes here.

2. The solution

Let us first suppose that you want labels for your
variable -timeofday- to be in steps of three hours from
midnight to midnight in the form 9:00 or 15:00. We type

. foreach t of num 0(3)24 {
.	label def tod `t' "`t':00", modify
. }
. label val timeofday tod
. graph whatever timeofday, options xla(0(3)24)

First, given the numlist 0(3)24 the value label for 0 is
defined as "0:00", that for 3 as "3:00", and so forth.
Then these value labels are attached to our -timeofday-
 variable. Given a request to show those numbers as
-xlabel-s on a graph on which -timeofday- is shown on the
x axis, Stata automatically uses attached value labels instead.

As you may know, the entity (here t) controlling the
-foreach- loop is a local macro; inside the loop,
its contents are referenced by `t'.

Next suppose that you want labels for your variable
-timeofday- to be in a.m. or p.m. form such as "9 a.m.".
If only a few labels are required, defining them
directly may be simplest:

. label def tod 0
"12 a.m." 6 "3 a.m." 12 "12 p.m." 18 "6 p.m." 24 "12 a.m."
. label val timeofday tod
. graph whatever, options xla(0(6)24)

Naturally, you may prefer some other convention for
showing midnight or noon. Notice that there is no
problem if the same label is used for different
integers. If we wanted to do this in a loop, the
boundary cases may need special care, so much so
that typing out definitions one by one is
attractive by comparison:

. foreach t of num 0(3)24 {
.       local T = mod(`t',12)
.       local T = cond(`T' == 0, 12, `T')
.       local txt = cond(`t' < 12 | `t' == 24, "a.m.", "p.m.")
.	label def tod `t' "`T' `txt'", modify
. }
. label val timeofday tod
. graph whatever timeofday, options xla(0(3)24)

The functions -mod()- and -cond()- used here are
explained at [U] 16.3.1 Mathematical functions
and [U] 16.3.6 Special functions respectively.
For further discussion, see Stata Journal 2(4), 411-427 (2002).

If you want for some reason to show a label for a
time such as 12:45 p.m., then you need to use a
scale on which that time is an integer. In this
example, multiplying 12.75 by 4 yields 51; this
and other times which are multiples of 15 minutes can then be labeled.

<end of draft FAQ>

[email protected]

*   For searches and help try:

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