Announcement

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

  • Converting dates from DMY format to DM format

    Hi all Statalists!
    I have dates in my data set in DMY format. Here is a subset of the data:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int test_date
    16560
    16579
    16589
    16589
    16602
    16615
    16618
    15465
    15467
    15467
    end
    format %td test_date
    I want to generate a new variable that takes the day and the month of the corresponding full date (test_date).
    For example, if test_date == 16560 (04may2005 in DMY format), the new variable takes 04may (in DM format).
    I need this new variable for regression analysis.

    Many thanks!

  • #2
    You can always specify a display format that suppresses the year.

    Code:
    . di %tddd_Mon mdy(11, 2, 2023)
     2 Nov
    
    . di %tddd_Mon mdy(11, 22, 2023)
    22 Nov
    
    .
    Otherwise, don't confuse what is stored with what is displayed. See e.g. https://journals.sagepub.com/doi/pdf...867X1201200415

    If you want day of year, use the function doy().

    Comment


    • #3
      Thanks Nick!
      I think the function doy() is close to what I need, but not exactly.
      Here is an exmple:
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input int test_date float doy
      16560 124
      16194 124
      16560 124
      16560 124
      16560 124
      16560 124
      16560 124
      16560 124
      16560 124
      16560 124
      end
      format %td test_date
      I generated the variable "doy" using the doy function
      Code:
      gen doy = doy(test_date)
      However, 04may2005 and 03may2004 get the same value of doy:
      Code:
      . list
      
           +-----------------+
           | test_date   doy |
           |-----------------|
        1. | 04may2005   124 |
        2. | 03may2004   124 |
        3. | 04may2005   124 |
        4. | 04may2005   124 |
        5. | 04may2005   124 |
           |-----------------|
        6. | 04may2005   124 |
        7. | 04may2005   124 |
        8. | 04may2005   124 |
        9. | 04may2005   124 |
       10. | 04may2005   124 |
           +-----------------+
      My dataset includes exam dates from 2000 to 2005. Thus, for instance, I need to assign the same value for all exams that were taken on May 4th (May 4th, 2000, May 4th, 2001, ..., May 4th, 2005) and another for all exams that were taken on May 3rd.
      Last edited by Asaf Yancu; 23 Nov 2023, 02:27.

      Comment


      • #4
        The reason you get different values for 2004 and 2005 is naturally that 2004 was a leap year and 2005 was not. You will get identical values for any particular day of year if you use the same year as reference. If you have any instances of 29th February you must use a leap year as reference. The price to be paid is a jump of 2 not 1 from February 28 to March 1 in any non-leap year.

        For example

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input int test_date float doy
        16560 124
        16194 124
        16560 124
        16560 124
        16560 124
        16560 124
        16560 124
        16560 124
        16560 124
        16560 124
        end
        format %td test_date
        
        gen wanted = doy(mdy(month(test_date), day(test_date), 2024)) 
        
        list 
        
            +--------------------------+
             | test_date   doy   wanted |
             |--------------------------|
          1. | 04may2005   124      125 |
          2. | 03may2004   124      124 |
          3. | 04may2005   124      125 |
          4. | 04may2005   124      125 |
          5. | 04may2005   124      125 |
             |--------------------------|
          6. | 04may2005   124      125 |
          7. | 04may2005   124      125 |
          8. | 04may2005   124      125 |
          9. | 04may2005   124      125 |
         10. | 04may2005   124      125 |
             +--------------------------+

        Comment

        Working...
        X