DATA Step, Macro, Functions and more

How to get month as name from a date

Reply
Contributor
Posts: 35

How to get month as name from a date

[ Edited ]

I am trying to use this and not getting anywhere.  Any help on how to use this would be appreciated.

 

I am using MONTH(tbl.field_name) to pull the month out i.e.... 1, 2, 3 etc... 12

 

Then I was using MONNAME to get the desired result 1 = January

 

Here is the code that I am using now:

 

SELECT (MONTH(t2.Sale_Date)) LENGTH=9 LABEL="Sale_Month" AS Sale_Month,

 

 

Super Contributor
Posts: 268

Re: MONNAME(MONTH(tbl.field_name))

What do the values of sale_date look like? Are they character or numeric?

Contributor
Posts: 35

Re: MONNAME(MONTH(tbl.field_name))

I do not have it specified..... should I have it as a char?
Super Contributor
Posts: 268

Re: MONNAME(MONTH(tbl.field_name))

The values need to be a SAS date value, which is numeric, in order to use the MONTH function.

Contributor
Posts: 35

Re: MONNAME(MONTH(tbl.field_name))

ok, so I will make it numeric, and as Reeza stated it is not a function, so I think with changing the value to numeric and using the format that should work. Reeza, there are multiple months, I am not using the year.
Contributor
Posts: 35

Re: MONNAME(MONTH(tbl.field_name))

Thank you as well GreggB, I will make these changes and see if that solves my problem.
Super User
Posts: 17,801

Re: MONNAME(MONTH(tbl.field_name))

Monname requires a date variable and I believe it's a format, not a function. 

 

Do do you have multiple years? 

Is January 2016= Jan 2015 = January? 

 

Select date_variable format=monname.,

put(date_variable, monname.) as new month

from ....

Trusted Advisor
Posts: 1,372

Re: MONNAME(MONTH(tbl.field_name))

MONNAME is unknown function in SAS;

 

There are several methods to get month name:

1) create a format like:

       proc format lib=work;

           value monname

               1 = "January"

               2 = "February"

              ...

              12 = "December"

        ; run;

 

  then, in your datastep use: month_name = put(month(date), monname.);

 

2) Alternatively use select statemnet to creat the month_name variable:

         data want

           set have;

                 mon = month(date);

                 select (mon);

                    when (1) month_name = 'January';

                    when(2) month_name = 'February';

                   ...

                   when (12) month_name = 'December';

                   otherwise month_name = 'Invalid Date';

               end;

        run;

 

Super User
Posts: 17,801

Re: MONNAME(MONTH(tbl.field_name))

You don't need to convert anything, if your variable is a SAS date.

 

data sample;
	date_var='01Nov2016'd;
	format date_var date9.;
	month_name=put(date_var, monname.);
run;

proc print ;
run;
Ask a Question
Discussion stats
  • 8 replies
  • 1289 views
  • 0 likes
  • 4 in conversation