I want to extract month data from a datetime format column in SAS

天大地大妈咪最大 提交于 2020-12-13 02:57:06

问题


I have a data in format 01 Jan 19.00.00 (datetime), and I want to extract the month name only from it.

Tried the below code but getting output in numbers i.e. 1 2 3 and so on. I want the output either in Jan Feb mar format or January February march format.

data want;
  set detail;
  month = month(datepart(BEGIN_DATE_TIME));
run;

回答1:


You can use the MONNAME format.

data test;
  dt = datetime();
  monname = put(datepart(dt),MONNAME.);
  put monname=;
run;

If you want "OCT" not "OCTOBER" you can add a 3 to the format (MONNAME3.).




回答2:


If you are using the value in a report the better approach might be to use a date value formatted with MONNAME.

The values of a date formatted variable will be ordered properly when the variable is used in a CLASS or BY statement. If you had instead computed a new variable as the month name, the default ordering of values would be alphabetical.

data want;
  set have;
  begin_date = datepart(BEGIN_DATE_TIME);
  format begin_date MONNAME3.;
run;


来源:https://stackoverflow.com/questions/64419876/i-want-to-extract-month-data-from-a-datetime-format-column-in-sas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!