Announcement

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

  • How to Convert Numerical Date variable (Type Long and Format %12g) to Stata Date Variable

    Hello,

    I was trying to convert a numerical date variable with a type of long and format of %12g into a stata date variable. For example, my date variable is something like 20200101 and I want it to be in Stata date variable. Thank you.

    Best,
    H

  • #2
    one easy way is to first make your variable into a string:
    Code:
    tostring dt, gen(dt3)
    then you can use the daily function (but get the month and day correct as they are ambiguous in your example):
    Code:
    gen date=daily(dt3,"YMD")
    format date %td
    you can choose another format if you want; see
    Code:
    h datetime

    Comment


    • #3
      Thank you Rich Rich Goldstein. That worked!
      Last edited by Haoyang Xiong; 17 Sep 2023, 22:05.

      Comment

      Working...
      X