Announcement

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

  • Converting "YYYY.MM" Formatted Dates to "%tm" Format in Stata

    Hello,

    I have a date variable in the "YYYY.MM" format (e.g., "1990.01" for January 1990). Is there a function that can create a Stata variable in the "%tm" format for me? I've attempted to use the "date()" function, but it doesn't seem to meet my requirements. Thank you!

    Here's what I've tried:

    ```stata
    gen ym_date = date(Data, "Y.M")
    ```

    However, it's not producing the desired result.
    ```

    Data
    1990.01
    1990.02
    1990.03
    1990.04
    1990.05
    1990.06
    1990.07
    1990.08
    1990.09
    1990.10
    1990.11
    1990.12
    1991.01
    1991.02
    1991.03
    1991.04
    1991.05

  • #2
    date() is for creating daily dates, as is documented. See help datetime or more formally help date().

    If your Data variable is a numeric variable with fractional parts, this should work

    Code:
    gen wanted = monthly(strofreal(Data, "%7.2f")), "YM")
    If it is a string variable, this should work.

    Code:
    gen wanted = monthly(Data, "YM")
    followed in either case by

    Code:
    format wanted %tm
    where naturally you should choose a variable name other than wanted and a different display format if you want one.

    See https://www.statalist.org/forums/help#stata which explains the special importance of using dataex with date-time variables to avoid ambiguity about how they are currently held.

    Comment

    Working...
    X