SAS 倒计时 years/months/days
SAS countdown to date years/months/days
为了好玩,我正在尝试编写一个宏来计算我大概退休年龄的倒计时。
不过,我似乎无法正确理解月份和日期。
这是我目前所拥有的,.
%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)。
这是我的主要障碍,由于天数不同(加上闰年!),无法准确计算月份。
我想要的是像
这样的输出
There are V years, X months, Y weeks and Z days until $DATE.
如果你做到了这一点,请帮忙,天哪,谢谢你你:-)
您可以而且应该使用 intck
/ intnx
来避免此类问题。只需分别计算每个 years/months 值,然后将基准日期增加那么多。所以计算年差,然后无论多少年,用 date() + yrsleft 作为值创建一个新变量。
因此,如果现在是 2021 年 10 月 4 日,而您将在 2049 年 7 月 15 日退休,则 yrsleft 为 27 - 新变量存储 10/4/2048。然后对月份执行相同操作 - 10/4/2048 和 7/15/2049 之间的 9 个月,这会将您带到 7/4/2049,因此将其存储在另一个变量中。然后减去天数。
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;
我想你需要打电话 IS8601_CONVERT。
dur=P2Y2M14D
data _null_;
length dur ;
start = today();
end='18DEC2023'd;
call is8601_convert('d/d','du',start,end,dur);
put dur=$n8601e.;
run;
我在这里找到了示例代码。
https://www.lexjansen.com/pharmasug/2012/DS/PharmaSUG-2012-DS22-SAS.pdf
为了好玩,我正在尝试编写一个宏来计算我大概退休年龄的倒计时。
不过,我似乎无法正确理解月份和日期。
这是我目前所拥有的,.
%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)。
这是我的主要障碍,由于天数不同(加上闰年!),无法准确计算月份。
我想要的是像
这样的输出There are V years, X months, Y weeks and Z days until $DATE.
如果你做到了这一点,请帮忙,天哪,谢谢你你:-)
您可以而且应该使用 intck
/ intnx
来避免此类问题。只需分别计算每个 years/months 值,然后将基准日期增加那么多。所以计算年差,然后无论多少年,用 date() + yrsleft 作为值创建一个新变量。
因此,如果现在是 2021 年 10 月 4 日,而您将在 2049 年 7 月 15 日退休,则 yrsleft 为 27 - 新变量存储 10/4/2048。然后对月份执行相同操作 - 10/4/2048 和 7/15/2049 之间的 9 个月,这会将您带到 7/4/2049,因此将其存储在另一个变量中。然后减去天数。
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;
我想你需要打电话 IS8601_CONVERT。
dur=P2Y2M14D
data _null_;
length dur ;
start = today();
end='18DEC2023'd;
call is8601_convert('d/d','du',start,end,dur);
put dur=$n8601e.;
run;
我在这里找到了示例代码。 https://www.lexjansen.com/pharmasug/2012/DS/PharmaSUG-2012-DS22-SAS.pdf