简体   繁体   English

如何从SAS中的日期中提取月份

[英]How to extract month from a date in SAS

I am trying to extract a month from a date in SAS, but so far all my new month variables are coming up as missing.我试图从 SAS 中的日期中提取一个月,但到目前为止,我所有的新月份变量都丢失了。

I have attempted to use some combinations of the month() function in SAS, but so far it just comes up as missing.我曾尝试在 SAS 中使用一些 month() 函数的组合,但到目前为止它只是丢失了。 The dates are formatted as follows: 01/31/2017 (MMDDYY10.)日期格式如下:01/31/2017 (MMDDYY10.)

I have tried我试过了

month = month(end_date) 

Month =catx('/',put(month(end_date),z2

I would like the Month to show up as a number (01) or a 3 letter code (JAN), currently it is just missing (.)我希望月份显示为数字 (01) 或 3 个字母的代码 (JAN),目前只是缺少 (.)

Thanks in advance!提前致谢!

For month() to return a missing value the end_date variable must be numeric and missing.要使month()返回缺失值, end_date变量必须是数字且缺失。 If end_date were a character variable the log would show invalid numeric data .如果 end_date 是一个字符变量,日志将显示invalid numeric data

Use the monname3.使用monname3. format to convert a date value to a $3.格式将日期值转换为 $3。 character value mon字符值mon

monthname = put (end_date, monname3.);

Other alternatives are:其他替代方案是:

  • keep the date value unchanged and change the format, or保持日期值不变并更改格式,或
  • map the date value to the first of the month value and also format that将日期值映射到月份值的第一个,并格式化

For example:例如:

end_date_copy = end_date;
format end_date_copy monname3.;

end_date_month = intnx('month', end_date, 0);
format end_date_month monname3.;

What you ultimately do depends on how the mon is to be used downstream in reporting or aggregating.你最终做什么取决于在报告或聚合的下游如何使用mon

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM