问题
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