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?
You do have gaps — between 201212 and 201301, for example. Consider a statement like
which parses your integers like 201201 into year and month components. So
floor(201201/100)isfloor(2012.01)and so2012whilemod(201201, 100)is1. The two components are then the arguments ofym()which expects a year and a month argument.Then and only then will your
formatstatement do you want. That command won’t create date variables.See
help datetimein 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.