英文:
Intnx in SAS SQL
问题
我不明白这段代码的结果会是什么:
intnx('month', datepart(date_example), 0, 'end')
其中date_example = '2010-05-12'
它是'2010-05-31'吗?
基本上是在那一年中那个月的最后一天吗?
英文:
I don't understand what will be the outcome of this piece of code:
intnx('month', datepart(date_example), 0, 'end')
With date_example = '2010-05-12'
Is it '2010-05-31'?
Basically the last day of that month in that one specific year?
答案1
得分: 2
"END" 表示移动到区间的末尾。一个月的末尾是这个月的最后一天。0 表示移动到同一个月的末尾。
如果你的 date_example 变量有一个日期时间值(以秒为单位),如 '12MAY2010:00:00'dt,那么你的代码首先会将其转换为日期值(以天为单位),'12MAY2010'd,然后将其转换为2010年5月的月底,'31MAY2010'd。
如果你更愿意生成一个月底的日期时间值,那么可以使用 DTMONTH 区间并移除 DATEPART() 函数。那么生成的值将成为该月的最后一天的 '23:59:59't。
英文:
The "END" means to move to the end of the interval. The end of a month is last day of the month. The 0 means to move to the end of the same month.
If your date_example variable has a datetime value (number of seconds), like '12MAY2010:00:00'dt, then your code will first convert it to a date value (number of days), '12MAY2010'd , and then convert it to the end of the month of may in the year 2010, '31MAY2010'd.
If you would rather generate a datetime value for the end of the month then use the DTMONTH interval and remove the DATEPART() function. Then the generated value will become '23:59:59't on the last of the month.
答案2
得分: 1
如果你想将月底作为日期时间值(而不是日期值)返回,请使用间隔 DTMONTH
。
请记住,日期值是距离纪元的天数,而日期时间值是距离纪元的秒数。
data one;
date_example = '01JUL2023:12:00:00'dt ;
eom = intnx('month', datepart(date_example), 0, 'end') ;
eomdt = intnx('dtmonth', date_example , 0, 'end') ;
put eom date9. ;
put eomdt datetime16. ;
put eom yymmdd10. ;
put eomdt e8601dt24.4 ;
run;
日志:
31JUL2023
31JUL23:23:59:59
2023-07-31
2023-07-31T23:59:59.0000 (我应该惊讶它不是 .9999 吗)
英文:
If you want the end of the month as a datatime value (instead of a date value), use the interval DTMONTH
.
Remember that date values are number of days from epoch, and datetime values are number of seconds.
data one;
date_example = '01JUL2023:12:00:00'dt ;
eom = intnx('month', datepart(date_example), 0, 'end') ;
eomdt = intnx('dtmonth', date_example , 0, 'end') ;
put eom date9. ;
put eomdt datetime16. ;
put eom yymmdd10. ;
put eomdt e8601dt24.4 ;
run;
Logs
31JUL2023
31JUL23:23:59:59
2023-07-31
2023-07-31T23:59:59.0000 (should I be surprised it's not .9999)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论