Announcement

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

  • Date Conversion to similar metric

    Hello All,

    I am working with some data that requires the same format for dates. I am certain this is answered or addressed somewhere, but I haven't been able to find it. My issue is I have two date variables both are in a long format. The data came this way. If I had to do it myself it would not look like this at all. I have copied and pasted similar data to mine so that the issue is clear. One date is %12.0g format (date1) and the other is in %dD_m_Y format (date2).


    input long(date1 date2)
    5032011 11 Feb 86
    5032011 11 Feb 86
    2232010 17 Aug 91
    4062010 17 Aug 91
    9292011 24 Dec 81
    11252014 24 Dec 81

    end

    format %tdD_m_Y date2

    I tried to use dataex, on my version of Stata, it kept cutting off the data for date2, so I provided it this way. My purpose is simple. I do not have a specific interest in what the final product looks like. I just need to be able to have the dates on the same metric so I can do some other calculations (e.g., date2 - date1) and some conversions to normal time (e.g., months). Any ideas?

    Thank you in advance.



    George




  • #2
    This works with your data example, after some minor rocket surgery.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long date1 float date2
     5032011  9538
     5032011  9538
     2232010 11551
     4062010 11551
     9292011  8028
    11252014  8028
    end
    format %tdD_m_Y date2
    
    gen sdate1  = strofreal(date1, "%10.0f")
    replace sdate1 = "0" + sdate1 if date1 < 1e7
    
    gen newdate1 = daily(sdate1, "MDY")
    format %tdD_m_Y newdate1 
    
    list 
    
         +---------------------------------------------+
         |    date1       date2     sdate1    newdate1 |
         |---------------------------------------------|
      1. |  5032011   11 Feb 86   05032011   03 May 11 |
      2. |  5032011   11 Feb 86   05032011   03 May 11 |
      3. |  2232010   17 Aug 91   02232010   23 Feb 10 |
      4. |  4062010   17 Aug 91   04062010   06 Apr 10 |
      5. |  9292011   24 Dec 81   09292011   29 Sep 11 |
         |---------------------------------------------|
      6. | 11252014   24 Dec 81   11252014   25 Nov 14 |
         +---------------------------------------------+
    Strictly, long is a variable or storage type, not a (display) format.

    Comment


    • #3
      Hello, I will give this a try. Thank you Nick and report back tomorrow. George

      Comment


      • #4
        Thank you Nick. Worked perfectly.

        Comment

        Working...
        X