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: Re: define variable to be date


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Re: define variable to be date
Date   Mon, 18 Jun 2012 02:14:33 +0100

Philipp's approach is based on the assumption that -date()- can
produce monthly dates from what is apparently a numeric argument, but
that is mistaken in two key respects. First, -date()- produces only
daily dates; it goes back to a period when those were the only dates
handled by Stata. Second, -date()- requires a string argument, hence
the error message. All but the history behind that is evident from
close study of the help for -dates and times-.

In addition to Joseph's solution, another is

gen panel_date = ym(mod(Month, 10000), floor(Month/10000))

With dates in Philipp's form, the year is the remainder on dividing by
10000 and the month is obtained by dividing by 10000 and rounding
down. As in Joseph's careful code, you would want to format the
monthly date.

Nick

On Mon, Jun 18, 2012 at 1:38 AM, Joseph Coveney <jcoveney@bigplanet.com> wrote:

Joseph Coveney

> Something like that below would work.  Suggestion:  adopt ISO 8601.
>
> Joseph Coveney
>
> input byte panel long Month
> 1 12001
> 1 22001
> 1 32001
> 2 112001
> 2 122001
> 2 12002
> end
>
> *
> * Begin here
> *
> tostring Month, generate(panel_date)
> generate int month = ///
>    (real(substr(panel_date, -4, 4)) - 1960) * 12 - 1 + ///
>    real(substr(panel_date, 1, length(panel_date)-4))
> xtset panel month, format(%tm)
>
> list panel ?onth, noobs sepby(panel)
>
> exit

Philipp Braunfels wrote:

> I reshaped my panel-dataset from wide to long format and now have a variable
> called "month" that serves as date variable in my dataset (covering 10 years).
> Now I want to run the <xtset> command but apparently "month" is not identified
> as date variable by stata (current type is "float"). The variable Month looks as
> follows:
>
> Month
> 12001
> 22001
> 32001
> ...
> 112001
> 122001
> 12002
> ...
>
> I looked at the stata archive but still cannot figure out how I can tell stata
> that the values (e.g. 12001) are supposed to be dates (e.g. Jan 2001). I tried
> the command - gen date = date(month, "MY") - but I am returned "(r 109) "type
> mismatch". I would appreciate any help on this issue.
>
*
*   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/


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