Find last date of year

冷暖自知 提交于 2019-12-13 11:14:32

问题


I have a string variable with years:

clear
input str4 year
"2012"
"2013"
"2014"
"2015"
"2016"
"2017"
"2018"
end

When I do the following I do not get 31dec201X everywhere:

g date = date(year, "Y") + 364
form date %td 

l

     +------------------+
     | year        date |
     |------------------|
  1. | 2012   30dec2012 |
  2. | 2013   31dec2013 |
  3. | 2014   31dec2014 |
  4. | 2015   31dec2015 |
  5. | 2016   30dec2016 |
     |------------------|
  6. | 2017   31dec2017 |
  7. | 2018   31dec2018 |
     +------------------+

How can I get the last date of December in all observations?


回答1:


Instead of the date() function you can use yearly() and dofy():

generate wanted = dofy(yearly(year, "Y") + 1) - 1
format wanted %td 

list, separator(0) 

     +------------------+
     | year      wanted |
     |------------------|
  1. | 2012   31dec2012 |
  2. | 2013   31dec2013 |
  3. | 2014   31dec2014 |
  4. | 2015   31dec2015 |
  5. | 2016   31dec2016 |
  6. | 2017   31dec2017 |
  7. | 2018   31dec2018 |
     +------------------+

This is a more general solution the idea of which is to find the first day of next year and subtract one.



来源:https://stackoverflow.com/questions/55772102/find-last-date-of-year

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!