1
votes

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;
2

2 Answers

2
votes

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
votes

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;