I am trying to simply count the months between two dates in Stata, given in year-month (%tm) format, data storage type being int. Basically, I want to do what the datediff(d1, d1, "month") function does in Stata 17 - except I have Stata 15. I saw a few other forums about this, but they all talked about solutions for a year-month-day format, which doesn't seem to work for me. Is there a way to do this?
CodePudding user response:
As both variables are numeric and the units are consistent, no special function is needed as a subtraction gives the answer.
. clear
. set obs 1
Number of observations (_N) was 0, now 1.
. gen now = ym(2022, 7)
. gen lastyear = ym(2021, 7)
. format now lastyear %tm
. di now - lastyear
12
. gen diff = now - lastyear
. l
--------------------------
| now lastyear diff |
|--------------------------|
1. | 2022m7 2021m7 12 |
--------------------------