Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: convert a date variable to a string variable with daynames


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: convert a date variable to a string variable with daynames
Date   Wed, 7 Aug 2013 10:37:56 +0100

-ntimeofday- and -stimeofday- were published by me in Stata Journal
6(1) in 2006. (Note that you are asked to say where programs come
from.) The history is that various colleagues had time of day data.
While StataCorp agreed that support for time of day was something that
could usefully be added to Stata, as usual they gave no promises on
when that would be done. So I did something myself, but in fact fuller
support for dates and times came very shortly afterwards in Stata 10,
immediately consigning my programs to the rubbish heap of history.

However, that is immaterial here. -stimeofday- never supported what
Haluk wants here and it must be done otherwise.

In fact no special programs are needed at all.

. clear

. set obs 4
obs was 0, now 4

. gen date = mdy(12,31,2012) + _n

. format date %tdd_m_Cy

. l

     +------------+
     |       date |
     |------------|
  1. | 1 Jan 2013 |
  2. | 2 Jan 2013 |
  3. | 3 Jan 2013 |
  4. | 4 Jan 2013 |
     +------------+

. gen sdate = string(date, "%tdd_m_Cy") + "  " + word("`c(Weekdays)'",
dow(date))

. l

     +------------------------------------+
     |       date                   sdate |
     |------------------------------------|
  1. | 1 Jan 2013      1 Jan 2013  Monday |
  2. | 2 Jan 2013     2 Jan 2013  Tuesday |
  3. | 3 Jan 2013   3 Jan 2013  Wednesday |
  4. | 4 Jan 2013    4 Jan 2013  Thursday |
     +------------------------------------+

You could get the days of the week in any language: just write the
names, separated by spaces, in place of

`c(Weekdays)'

For c(Weekdays) and its kin, look at -creturn list-.

Nick
[email protected]


On 7 August 2013 10:09, Haluk Vahaboglu <[email protected]> wrote:
> Dear Statalisters,
> I am using Stata 12.1 MP for Linux 64 bit.
> I want to convert a date variable to a string variable. This can be achieved by "-stimeofday-" command (by  Nicholas J. Cox,). Below; date variable, command and the output can be found.
> variable:
>           d
> 01.January.2013
> 02.January.2013
> 03.January.2013
> 04.January.2013
>
> des
> d               float  %td..
> -------------------------
>  stimeofday d, gen(stime) n(d) s(d h) dformat(%dd_m_CY) sformat(%02.0f)
>                d            stime
>  01.January.2013    1 Jan 2013 00
>  02.January.2013    2 Jan 2013 00
>  03.January.2013    3 Jan 2013 00
>  04.January.2013    4 Jan 2013 00
>
> des
> d               float  %td..
> stime           str14  %14s                   d (days hours)
> -----------------------------
>
> I get a string variable as "day month year h".
> My question is this:
> Can I get an output (a string variable) displaying as "day month year dayname" (1 Jan 2013 Tuesday).
*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index