Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Converting Dates to Strings (i.e., swimming upriver)

    I have a need to store dates as strings and can't seem to find an easy way to do this. I was not able to find any suggestions online or in the statalist (either old or new). Any advice?

    ********* BEGIN ************
    clear all
    input str9 date_text1
    "28mar2008"
    "25jan2000"
    "19nov2003"
    "25aug2005"
    "09feb2010"
    end

    g date=date(date_text1,"DMY")
    format date %td

    *the part I do not know how to do
    * this is what I thought would work
    tostring date, g(date_text2) format(%td) //but it does not work

    list
    ********** END **********

    Thanks,

    Ben Hoen
    LBNL

  • #2
    "does not work" is explicitly forbidden without explanation!

    The displayed error message is

    Code:
    date cannot be converted reversibly; no generate
    This surprised me too at first, as the original author of tostring, but it's long since an official command and if this is a bug or misfeature it's StataCorp's, not mine. Insert sardonic smiley, or not, according to taste.

    But tostring is obeying the advertised logic.

    The criterion used by tostring is that unless real(string()) returns the original numeric argument, it won't play. But for example real("28mar2008") doesn't return 17619.

    A solution is

    Code:
     
    generate date_text2 = string(date, "%td")

    Comment


    • #3
      That works perfectly. Thanks Nick!

      Comment


      • #4
        Alternatively, if you prefer to use -tostring-, just add the -force- option.

        Code:
        clear all
        input str9 date_text1
        "28mar2008"
        "25jan2000"
        "19nov2003"
        "25aug2005"
        "09feb2010"
        end
        
        g date=date(date_text1,"DMY")
        format date %td
        
        tostring date, g(date_text2) format(%tdd_m_CY) force // This works, but you get a complaint:


        Code:
        . tostring date, g(date_text2) format(%tdd_m_CY) force
        date_text2 generated as str11
        date_text2 was forced to string; some loss of information
        
        .
        end of do-file
        
        . li
        
             +-------------------------------------+
             | date_te~1        date    date_text2 |
             |-------------------------------------|
          1. | 28mar2008   28mar2008   28 Mar 2008 |
          2. | 25jan2000   25jan2000   25 Jan 2000 |
          3. | 19nov2003   19nov2003   19 Nov 2003 |
          4. | 25aug2005   25aug2005   25 Aug 2005 |
          5. | 09feb2010   09feb2010    9 Feb 2010 |
             +-------------------------------------+
        
        
        . desc
        
        Contains data
          obs:             5                          
         vars:             3                          
         size:           140 (99.9% of memory free)
        -----------------------------------------------------------------------------------------
                      storage  display     value
        variable name   type   format      label      variable label
        -----------------------------------------------------------------------------------------
        date_text1      str9   %9s                    
        date            float  %td                    
        date_text2      str11  %11s                   date
        -----------------------------------------------------------------------------------------

        Comment


        • #5
          I was able to tailor these to my needs, thanks for your answers.

          Comment


          • #6
            Nick Cox - THANK YOU for this suggestion. Paul's solution won't work if you've got a time stamp attached to the date value (e.g. if you're pulling a raw data set from a Google form), but your solution worked great. Just wanted to thank you.

            Comment

            Working...
            X