date - SAS 倒计时到日期年/月/日
问题描述
为了好玩,我正在尝试编写一个宏来计算我的大致退休年龄的倒计时。
不过,我似乎无法正确计算月份和日期。
这是我到目前为止所拥有的,.
%let mybirthday = ; /* in date9 format */
DATA _NULL_;
call symput('actualend',put(intnx('years', "&mybirthday."d, 65, 'sameday'),date9.));
RUN;
DATA _NULL_;
call symput('days', INTCK('DAY',date(),"&actualend."d,'C'));
call symput('weeks', INTCK('WEEK',date(),"&actualend."d,'C'));
run;
DATA _NULL;
call symput('yrsleft', floor(&weeks./52));
run;
到目前为止,一切都很好。
但是,当我尝试计算月份时,使用
DATA _NULL_;
call symput('mthsleft', floor((&weeks. - &years.*52)/4));
run;
我得到 10,而答案应该是 9(为方便起见,使用 Wolfram Alpha)。
这是我的主要绊脚石,由于天数不同(加上闰年!),无法准确计算月份。
我要寻找的是输出
距离 $DATE 有 V 年、X 个月、Y 周和 Z 天。
请帮忙,如果你能做到这一点,天哪,谢谢你:-)
解决方案
你可以而且应该用intck
/做所有事情intnx
来避免这样的问题。只需分别计算每个年/月的值,然后将基准日期增加那么多。所以计算年差,然后不管它是多少年,用 date() + yrsleft 作为值创建一个新变量。
因此,如果是 2021 年 10 月 4 日,而您将于 2049 年 7 月 15 日退休,那么 yrsleft 是 27 - 新变量存储 2048 年 10 月 4 日。然后对这几个月做同样的事情——从 2048 年 10 月 4 日到 2049 年 7 月 15 日之间的 9 个月,这将带你到 2049 年 7 月 4 日,所以将它存储在另一个变量中。然后只需减去即可获得天数。
DATA _NULL_;
*calculate YRSLEFT and then store the "ending year" point in a variable;
yrsleft = intck('Year',date(),"&actualend."d,'c');
year = intnx('year',date(),yrsleft,'s');
put year= date9.;
*calculate MTHSLEFT and then store the "ending month" point in a variable;
mthsleft=intck('Month',year,"&actualend."d,'c');
month = intnx('month',year, mthsleft,'s');
*store them in macro variables;
call symputx('yrsleft', yrsleft);
call symputx('mthsleft',mthsleft);
*calculate days left directly;
call symputx('daysleft',"&actualend."d - month);
run;
%put &=yrsleft &=mthsleft &=daysleft;
推荐阅读
- android - 如何创建一个可以360度循环的Recylerview?
- reactjs - 如何使用 React Navigation v5 在 React Native 中获取当前路由名称
- sql-server - Heroku PHP MSSQL 扩展/ SQL_SRV
- java - Java 指定从 1 到 100 的命令行参数
- php - 从php中的数组中检索一个单词
- sql - SQL查询一批地址
- sql - 平均库存天数
- jquery - Jquery:ID 匹配正则表达式或过滤器的所有输入?
- docker - Nginx over TLS 与 docker compose
- jsp - 访问 .jsp 文件中的 java 映射