yyyymm 转换为完整的 sas 日期 (dd/mm/ccyy) - SAS [英] yyyymm convert to full sas date (dd/mm/ccyy) - SAS
问题描述
我正在尝试从 SAS (ccyymm
) 中的字段数字中获取该月的最后一天.
I am trying to get the last day of the month from a field numeric in SAS (ccyymm
).
例如201401 将是 31-01-2014.
for example 201401 would be 31-01-2014.
我已经设法使该字段显示为日期类型字段(尽管仍显示为 ccyymm),并在 PROC SQL
SELECT 语句
I have managed to get the field to show as a date type field (still showing as ccyymm though) with the following code in a PROC SQL
SELECT statement
year_month_field INFORMAT YYMMN6. AS year_month_date
我认为我从另一个被问到的问题中找到了一些代码,一旦我可以得到完整的日期,应该给我这个月的最后一天.
I think that I found some code from another question that had been asked that should give me the last day of the month once I can get the full date.
INTNX ( MONTH , year_month_date , 1) -1
这行得通吗?如果没有任何其他建议,我们将不胜感激.
Will this work? If not any other suggestions would be appreciated.
丹
推荐答案
请注意下面如何将 yyyymm 转换为本月最后一天"MMMYYYY 格式之间的区别,具体取决于 yyyymm 是字符变量还是数字变量.
Note the difference between how one would convert a yyyymm to "last day of the month"MMMYYYY format below, depending on whether yyyymm is character or numeric variable.
data test;
yyyymm_character='201401';
yyyymm_numeric=201401;
date1=intnx('month', input(yyyymm_character, yymmn6.), 1)-1;
date2=intnx('month', input(put(yyyymm_numeric,6.), yymmn6.), 1)-1;
format date1 date2 date9.;
/*date1=date2=31jan2014*/
run;
或者,您可以使用 intnx
函数的内置选项将任何输入日期自动设置为相应月份的最后一天.如下图使用'e'
.
Alternatively, you can use the in-built options for intnx
functions to automatically set any input date to the last day of the respective month. Use 'e'
as shown below.
data test;
yyyymm_character='201401';
yyyymm_numeric=201401;
date1=intnx('month', input(yyyymm_character, yymmn6.), 0, 'e');
date2=intnx('month', input(put(yyyymm_numeric,6.), yymmn6.), 0,'e');
format date1 date2 date9.;
/*date1=date2=31jan2014*/
run
这篇关于yyyymm 转换为完整的 sas 日期 (dd/mm/ccyy) - SAS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!