Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Formatting months error


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Formatting months error
Date   Thu, 7 Mar 2013 13:21:40 +0000

I wrote this in a bit of a hurry before a meeting.

I think it's fair to say that I wrote my Tip not only because this is
often misunderstood, but also because what Paul wants to read is so
far as I know missing from the manuals. In effect, you need to put
together two ideas and see what they imply together. It's one of those
points which once understood appears blindingly obvious.

The first idea is simply that all dates are numbers at bottom. Stata
has no problem with your storing dates as strings, but it won't
manipulate them as such. You can extract e.g. particular date
components using string functions, but otherwise you need to use a
function such as -date()-  to convert dates as strings to numeric date
variables.

The second idea is that when you have done that conversion, that is
_all_ you have done. If you had daily dates such "1 Jan 2011", now you
have numbers such as 18628. Stata does not store anywhere or otherwise
know that _you_ think of that as a daily date variable.

Suppose you now say

format date %td

Again, you are as far as Stata is concerned, saying one thing and one
thing only: display the values in this variable as daily dates. That
is a matter of presentation only: there are no other implications and
still no sense in which Stata knows that this variable is inherently
or essentially a date variable.

This can easily be misunderstood if you are accustomed to software in
which dates are special, e.g. a special variable or data type. In
Stata, being a date is the user's perception, not the program's
memory. Stata has _no_ date variable types or storage types. Dates are
just numeric variables.

What is better understood, for example, is that changing a numeric
format to %4.1f or %4.2f does not itself change the data by rounding
to so many decimal places (not that that's possible, a different story
here).

This rule is crucial here:

* Changing the format does not change anything except how a variable
is displayed. *

You may have noticed that Stata is totally easy with your changing
formats. The reason is what we have already seen: formats are about
not what the data are, but purely how you display them.

So, when you change -format- to

%tm

18628 remains 18628 but interpreted as a monthly date it is some way
into the future.

No, what you want is something different.

If you want to display month (and possibly year) only, you need a
format starting %td...

If you want to convert a daily date to a monthly date, that's a change
of values and requires a conversion function, such as -mofd()-.

Nick

On Thu, Mar 7, 2013 at 10:58 AM, Nick Cox <njcoxstata@gmail.com> wrote:

> Misunderstandings about date formats have been explained many times on
> the list e.g.
>
> http://www.stata.com/statalist/archive/2013-01/msg00893.html
>
> http://www.stata.com/statalist/archive/2013-01/msg01350.html
>
> and I wrote a Tip about it
>
> Nicholas J. Cox
> Stata tip 113: Changing a variable's format: What it does and does not mean
> The Stata Journal
> Volume 12 Number 4: pp. 761-764
>
> When you change the -format- you don't change the date. You just
> change the instructions to Stata on how to think about a number.
>
> 18628 _could_ be a daily date: it is 16 jan 2011 to Stata. It could be
> a monthly date, May 3512. That is what %tm implies: you are saying to
> Stata "this is a monthly date, so display it accordingly".
>
> Changing the format just changes what is displayed: it doesn't change
> what is stored.
>
> You need a conversion function, in your case -mofd()-,
>
> or
>
> a display -format- that starts %td and includes month
>
> depending on what you want.
>
> Nick
>
> On Thu, Mar 7, 2013 at 10:41 AM, Paul O'Brien <paul.obrien@afe2.org.uk> wrote:
>
>> I cannot find the bit in the manuals that sorts this out for me. I get the wrong month when I format for month with %tm.
>>
>> . gen date=18628
>>
>> . format date %td
>>
>> . l
>>
>>      +-----------+
>>      |      date |
>>      |-----------|
>>   1. | 01jan2011 |
>>      +-----------+
>>
>> . format date %tm
>>
>> . l
>>
>>      +--------+
>>      |   date |
>>      |--------|
>>   1. | 3512m5 |
>>      +--------+
>>
>> What am I doing wrong?
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index