How can I create a date sequence in Stata? -
i wish create date sequence in stata. search on forum , others did not yield helpful code stata. there many answers r shown example on this link. want create sequence january 1, 2000 december 31, 2010. how can that?
dates can tricky in software. in stata, other software, there no substitute reading documentation carefully. help dates , times
answers questions. quirk may trap unwary date()
in stata function creating daily date variables, , no other kind. best, stata 13 on, new users use synonymous function daily()
when seek daily dates. said, function more convenient here creating date sequences, mdy()
.
here example:
clear di mdy(1,1,2000) di mdy(12,31,2010) di mdy(12,31,2010) - mdy(1,1,2000) set obs 4018 gen mydate = mdy(12,31,1999) + _n format mydate %td list in 1 list in l
notes:
mdy()
creates daily date variables arguments indicating month, day , year._n
observation number, 1 up.so need work out starting date , number of observations desired , it's case of adding required constant
_n
.stata works daily date origin 1 january 1960, solution not depend on knowing that.
p.s. in answer deleted, @caty seems asking sequence of monthly dates. here edited version:
when try that, dates 01jan2000, 02jan2000. should monthly: 01jan2000, 01feb2000.
if have monthly data, there precisely no need for, , precisely no advantages in, working daily dates first of each month, @ least far stata concerned. in fact, creating such sequence create sequence gaps variously 27, 28, 29 , 30 days long, not sequence regularly spaced. gaps make many analyses in stata awkward , impossible. similar comments apply quarters. (if have weeks, daily dates 7 days apart best framework, story.)
clear di ym(2000, 1) di ym(2010, 12) di ym(2010, 1) - ym(2000,1) set obs 121 gen mydate = ym(1999, 12) + _n format mydate %tm l in 1 l in l
notes:
ym()
creates monthly date variables arguments indicating year , month._n
observation number, 1 up.so need work out starting date , number of observations desired , it's case of adding required constant
_n
.stata works monthly date origin january 1960, solution not depend on knowing that.
edit end-of-month daily sequences easy trick may kick not seeing immediately. suppose want ends of january 2015 december 2016. end of each month 1 day before beginning of next month. there no need worry months of differing lengths or leap years. stata knows details of calendar, don't have tell are.
. clear . set obs 24 number of observations (_n) 0, 24 . gen mdate = ym(2015, 1) + _n . gen ddate = dofm(mdate) - 1 . format %td ddate . format %tm mdate . list +---------------------+ | mdate ddate | |---------------------| 1. | 2015m2 31jan2015 | 2. | 2015m3 28feb2015 | 3. | 2015m4 31mar2015 | 4. | 2015m5 30apr2015 | 5. | 2015m6 31may2015 | |---------------------| 6. | 2015m7 30jun2015 | 7. | 2015m8 31jul2015 | 8. | 2015m9 31aug2015 | 9. | 2015m10 30sep2015 | 10. | 2015m11 31oct2015 | |---------------------| 11. | 2015m12 30nov2015 | 12. | 2016m1 31dec2015 | 13. | 2016m2 31jan2016 | 14. | 2016m3 29feb2016 | 15. | 2016m4 31mar2016 | |---------------------| 16. | 2016m5 30apr2016 | 17. | 2016m6 31may2016 | 18. | 2016m7 30jun2016 | 19. | 2016m8 31jul2016 | 20. | 2016m9 31aug2016 | |---------------------| 21. | 2016m10 30sep2016 | 22. | 2016m11 31oct2016 | 23. | 2016m12 30nov2016 | 24. | 2017m1 31dec2016 | +---------------------+
Comments
Post a Comment