Announcement

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

  • changing only year in the date

    Hello all,

    The dataset am working on has errors in only year entries for stop date. I was wondering how I could go about creating a variable that has the date with the correct year.

    So the stop_date is meant to have the same year as the start_date.

    I created the variable yr using code below:

    "gen yr = year(start_date)"

    See below is the sample of the data


    +--------------------------------+
    | stop_date start_date yr |
    |--------------------------------|
    1 | 12/31/2022 10/8/2021 2021 |
    2 | 12/9/2022 9/15/2021 2021 |
    3 | 5/17/2023 3/18/2022 2022 |
    4 | 12/28/2022 10/5/2021 2021 |

    For example the correct stop_date for 1) would have to be 12/31/2021 instead of 12/31/2022.


    Any assistance will be highly appreciated as I have looked through a number of previously posted date-related questions and none seemed to help.

  • #2
    Date functions such as -mdy()- may be useful. See

    Code:
    help datetime
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float date
    22657
    14881
    22886
    end
    
    gen wanted= mdy(month(date), day(date), year(date)-1)
    format %td date wanted
    Res.:

    Code:
    . l
    
         +-----------------------+
         |      date      wanted |
         |-----------------------|
      1. | 12jan2022   12jan2021 |
      2. | 28sep2000   28sep1999 |
      3. | 29aug2022   29aug2021 |
         +-----------------------+

    Comment


    • #3
      Also, if you are using version 17 or later:
      Code:
      gen wanted = birthday(stop_date, year(start_date))
      Note that the -birthday()- function handles the anomalous case where stop_date is given as 29 Feb in a leap year and the year of start_date is not a leap year. (It also has an optional third argument allowing you to override its default handling of this.) The approach shown in #2 will fail in this (admittedly rare) situation.

      Comment


      • #4
        Thank you so much Andrew. It's exactly what I needed and it has worked.

        Thank you too Clyde for the other alternative. I am unfortunately using version 15.1 so it hasn't worked just like indicated.
        Last edited by Galenda Nagudi; 11 May 2023, 14:34.

        Comment


        • #5
          This is why we ask in the FAQ Advice that you tell us about using an older version of Stata. But in your situation as in anybody else's

          Code:
           
           gen wanted= mdy(month(date), day(date), year(date)-1)
          will return missing if you ask for 29 February in a non-leap year. If so, use your own rule such as
          Code:
          replace wanted = mdy(3, 1, year(date) - 1) if month(date) == 2 & day(date) == 29 & wanted == .
          or
          Code:
          replace wanted = mdy(2, 28, year(date) - 1) if month(date) == 2 & day(date) == 29 & wanted == .

          Comment


          • #6
            Thank you for your response Nick.

            Comment

            Working...
            X