datetimetimestatayearmonth

Formatting year month variable as date


In Stata I have a variable yearmonth which is formatted as 201201, 201202 etc. for the years 2012 - 2019, monthly with no gaps. When I format the variable as

format yearmonth %tm 

The results look like: 2.0e+05 for all periods, with the exact same number each time. A Dickey-Fuller test tells me I have gaps in my data (I don't) and a tsfill command generates dozens of empty observations between each period.

How do I properly format my yearmonth variable so I can set it as a monthly date?


Solution

  • You do have gaps — between 201212 and 201301, for example. Consider a statement like

    gen wanted = ym(floor(yearmonth/100), mod(yearmonth, 100))
    

    which parses your integers like 201201 into year and month components. So floor(201201/100) is floor(2012.01) and so 2012 while mod(201201, 100) is 1. The two components are then the arguments of ym() which expects a year and a month argument.

    Then and only then will your format statement do you want. That command won’t create date variables.

    See help datetime in Stata for more information and Problem with displaying reformatted string into a four-digit year in Stata 17 for an explanation of the difference between a date value and a date display format.