Announcement

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

  • Stata extract year from date

    My data has a date variable, and I want to extract year, month, date information from it:

    date
    03jun1960 00:00:00
    06jun1960 00:00:00
    07jun1960 00:00:00
    08jun1960 00:00:00
    09jun1960 00:00:00
    10jun1960 00:00:00
    13jun1960 00:00:00

    So I use:

    gen year = year(date)

    But generate missing values only.

    How to solve this?

  • #2
    The variable you show is either a string variable, or if it is a Stata numeric datetime variable, it is a clock variable. Either way, that won't fly with the -year()- function which requires a date variable. On the assumption that the variable is numeric (which is most compatible with the result you got, and the absence of any -type mismatch- error message):
    Code:
    gen year = year(dofc(date))
    gen month = month(dofc(date))
    gen day = day(dofc(date))
    The correctness of my solution depends on my guess/inference about what kind of variable you actually have. Guesswork isn't a good way to debug code. And tables/listings are not a good way to show example data, because they often lack important information. The helpful way to show example data is with the -dataex- command. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    In the future, please always use -dataex- to show example data.

    Comment


    • #3
      Thank you! It's working!

      Comment


      • #4
        We can't be certain, absent a dataex example, but one possibility is that your variable is a date-time, in which case you need to push it through dofc() first.


        Code:
        . clear
        
        . set obs 1
        Number of observations (_N) was 0, now 1.
        
        . gen double datetime = clock("20 Feb 2023", "DMY")
        
        . format datetime %tc
        
        . gen year = year(dofc(datetime))
        
        . l
        
             +---------------------------+
             |           datetime   year |
             |---------------------------|
          1. | 20feb2023 00:00:00   2023 |
             +---------------------------+
        If that doesn't help, please visit https://www.statalist.org/forums/help#stata and show us a data example using dataex.


        EDIT This thread proves that 4 = 2.
        Last edited by Nick Cox; 20 Feb 2023, 16:06.

        Comment

        Working...
        X