Announcement

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

  • How to trim a date variable

    Hi everyone,

    I have a survey with 2025 cases and a date variable called "EndDate" (stored as a double). I need to know what day in April the respondent took the survey, but I mostly keep creating variables with missing values. I have read through all of the documentation so must be missing something basic. I would greatly appreciate any help you can provide. I am using Stata 16.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double EndDate
    1902066981000
    1902067150000
    1902067177000
    1902067267000
    1902067348000
    end
    format %tcdd-Mon-CCYY_HH:MM:SS EndDate
    I think this format is more readable.

    list EndDate

    9-Apr-2020 15:56:21
    9-Apr-2020 15:59:10
    9-Apr-2020 15:59:37
    9-Apr-2020 16:01:07
    9-Apr-2020 16:02:28

    I have tried the following and received error messages:

    gen date1 = dofy(EndDate)
    (2,025 missing values generated)

    gen date1 = day(EndDate)
    (2,025 missing values generated)

    gen date1 = date(EndDate)
    invalid syntax

    gen date1 = date(EndDate, "DMYhms")
    invalid syntax

    gen double date1 = date(EndDate, "DMYhms")
    invalid syntax

  • #2
    You variable EndDate is in milliseconds (clock format) from which you need the date, so you need dofc()
    Code:
    gen date = dofc(EndDate)
    format %td date

    Comment


    • #3
      date() is a function for generating daily dates from string inputs. Not what you want here. daily() also works and is (in my view) the better function name for the same underlying code.

      day() is a function for extracting day of the month from a daily date (variable or scalar). Useful here but only when you have a daily date.

      dofy() is a (not very useful) function for extracting day of year from year, which in practice yields 1 January for the year specified. Not what you want here.

      Code:
      . di dofy(2020)
      21915
      
      . di %td dofy(2020)
      01jan2020

      Here is some positive code instead:

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input double EndDate
      1902066981000
      1902067150000
      1902067177000
      1902067267000
      1902067348000
      end
      format %tcdd-Mon-CCYY_HH:MM:SS EndDate
      
      gen daily = dofc(EndDate)
      format %td daily 
      
      gen day = day(daily)
      gen month = month(daily)
      gen year = year(daily)
      
      list 
      
           +------------------------------------------------------+
           |             EndDate       daily   day   month   year |
           |------------------------------------------------------|
        1. | 9-Apr-2020 15:56:21   09apr2020     9       4   2020 |
        2. | 9-Apr-2020 15:59:10   09apr2020     9       4   2020 |
        3. | 9-Apr-2020 15:59:37   09apr2020     9       4   2020 |
        4. | 9-Apr-2020 16:01:07   09apr2020     9       4   2020 |
        5. | 9-Apr-2020 16:02:28   09apr2020     9       4   2020 |
           +------------------------------------------------------+
      See

      Code:
      help datetime
      for a synopsis.

      The trick to (almost) any function is thinking

      What does it eat? More prosaically, what arguments or inputs does it take?

      What does it yield? Its outputs or results.

      For date and time functions, it's the same story. What goes in, what comes out.

      Comment


      • #4
        Thank you so much! The last thing I'd like to do is trim the variable to only have the day without the month '12' rather than 12apr2020. I plan to use the value in a regression.

        Comment


        • #5
          #3 already answered #4; presumably you can see it now.

          Comment


          • #6
            I posted too soon. Thank you both Wouter and Nick! The functions make much more sense to me now. The code did exactly what I needed.

            Comment

            Working...
            X