Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: ST: simple date question


From   [email protected] (William Gould, StataCorp LP)
To   [email protected]
Subject   Re: st: ST: simple date question
Date   Tue, 15 Jan 2008 09:09:16 -0600

Eric Uslaner wrote,

> I've been trying to convert seven numbers to dates (without having done this
> before).  I've read the help, the manual, and several tutorials and I can't
> get past the obscure language so that I can create a variable that I can use
> on the LHS of a graph hbar command.
>
> I have created new variables for month and year (and even a fake one for
> day).  All I want is a date variable that will be sorted in this order:
>
> October2000
> April2001
> October2001
> April2002
> October2002
> April2003
> October 2003

The easiest solution is 

        . gen ndate = date(sdate, "MY")
        . format ndate %d

For instance, 

    . describe

    Contains data
      obs:             7                          
     vars:             1                          
     size:           112 (99.9% of memory free)
    -----------------------------------------------------------------------
                  storage  display     value
    variable name   type   format      label      variable label
    -----------------------------------------------------------------------
    sdate           str12  %12s                   
    -----------------------------------------------------------------------
    Sorted by:  

    . list

         +--------------+
         |        sdate |
         |--------------|
      1. |  October2000 |
      2. |    April2001 |
      3. |  October2001 |
      4. |    April2002 |
      5. |  October2002 |
         |--------------|
      6. |    April2003 |
      7. | October 2003 |
         +--------------+

    . gen ndate = date(sdate, "MY")

    . list

         +----------------------+
         |        sdate   ndate |
         |----------------------|
      1. |  October2000   14884 |
      2. |    April2001   15066 |
      3. |  October2001   15249 |
      4. |    April2002   15431 |
      5. |  October2002   15614 |
         |----------------------|
      6. |    April2003   15796 |
      7. | October 2003   15979 |
         +----------------------+

    . format ndate %d
 
    . list

         +--------------------------+
         |        sdate       ndate |
         |--------------------------|
      1. |  October2000   01oct2000 |
      2. |    April2001   01apr2001 |
      3. |  October2001   01oct2001 |
      4. |    April2002   01apr2002 |
      5. |  October2002   01oct2002 |
         |--------------------------|
      6. |    April2003   01apr2003 |
      7. | October 2003   01oct2003 |
         +--------------------------+

-- Bill
[email protected]
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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