我有一个带年份的字符串变量:
clear
input str4 year
"2012"
"2013"
"2014"
"2015"
"2016"
"2017"
"2018"
end
执行以下操作时,我不会无处不在31dec201X
:
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 |
+------------------+
如何获得所有观测结果中的十二月的最后日期?
答案 0 :(得分:1)
您可以使用date()
和yearly()
代替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 |
+------------------+
这是一个更通用的解决方案,其思想是找到明年的第一天并减去第一天。