Announcement

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

  • Generating a variable for year from a variable with day, month, year, time,

    I have a date variable stored as numeric stata date variable as type double which holds day, month, year, time (admission_date) which is displaced as DD-MM-YYYY H:M:S with the %tc format (eg displaced as 12aug2021 00:00:00. I wish to generate a new variable that holds only the year (admission_year). The new variable for year only (admission_year) is generated with all missing values, although the existing variable (admission_date) has no missing values. I have tried the code below and all generate the new variable with all values missing.

    gen admission_yr = year(admission_date)
    gen double admission_yr = year(admission_date)
    gen admission_yr = dofy(admission_date)

    Please advise what I am doing wrong.

  • #2
    year() is for pulling year out of daily dates. dofy() is for pulling daily dates out of yearly dates (in practice, it returns 1 January of the year(s) input).

    Code:
    . di dofy(2024)
    23376
    
    . di %td dofy(2024)
    01jan2024
    You want to pull year out of a datetime variable [not a date variable], which can be done by

    Code:
    gen admission_yr = year(dofc(admission_date))

    Comment


    • #3
      Thanks so much Nick! That worked perfectly. It was indeed a problem of a datetime variable versus date variable. I really appreciate the response!

      Comment


      • #4
        The history is a little relevant here. Almost everybody who deals with yearly dates doesn't need special Stata support or to think hard what to do: integer years with positive values work fine in Stata. (The uses of negative years among Stata users may not extend far beyond some uses of mine, as geologists, archaeologists and ancient historians don't seem numerous among Stata users. But Stata really doesn't know anything about BC/BCE or AD/CE as such, let alone the fact that there was no year 0, even retrospectively: 1 AD is reckoned to have followed 1 BC immediately, because those who devised the system in Europe didn't have the concept of zero, invented or discovered by Indian mathematicians, IIUC.)

        Stata first supported daily dates with dedicated functions and display format as often arising for many people across several disciplines. Calling those daily date variables by the name date variables without qualification may seem unsurprising and it matches much ordinary English usage of the kind What is the date? -- meaning today -- where an answer like 9 February often is immediate (with those asking and those answering presuming that the year is known).

        But Stata's names have caused some confusion too. On Statalist we've often seem usage close to imagining that date() is a function smart enough to look at its input and also to read your mind and work out what you want. Not so. Hence, sometimes people here use the synonym daily() as a way of underlining that the function only tries to yield daily dates.

        Stata later still added support for half-yearly, quarterly, monthly and weekly dates and for date-times, but daily dates remain at the core of the functionality. So many of the functions presume daily date input or output, and if you want something else you often need an extra conversion function, or even more.

        Comment

        Working...
        X