To get the month of a date variable in a SAS data step, the easiest way is to use the SAS month() function.

data data_with_month;
	set data_with_dates;
	mo = month(d);
run;

When working with data, many times we are working with dates and need to make adjustments to our data depending on which month something occurred.

We can find the month of a date in SAS very easily. With the SAS month() function, we can get the month of a date variable.

Let’s say we have the following SAS dataset.

data data_with_dates;
    input d date9.;
    format d date9.;
    datalines;
31DEC2021
24OCT2020
12DEC2019   
07JUN2019
17FEB2021
12JAN2021
03MAR2020
;
run;

We can get the month from a date variable with the SAS month() function. Below is SAS code with a data step getting the months from the variable “d”.

data data_with_month;
	set data_with_dates;
	mo = month(d);
run;

The resulting dataset with the months of the date variable is as follows.

          d     mo
1	31DEC2021	12	
2	24OCT2020	10	
3	12DEC2019	12	
4	07JUN2019	 6	
5	17FEB2021	 2	
6	12JAN2021	 1	
7	03MAR2020	 3

Getting the Month Name in SAS from Date Variable

When working with dates, sometimes having the month number can be useful, but it’s possible we want to work with the month name instead.

Getting the name of a month in SAS from a date variable takes a little more work, but it’s easy to do.

There are two ways we can get the name of a month in SAS. One with PROC Format, and the other with an if/else statement. Depending on style, either will be useful.

Let’s say with the PROC format way of getting the name from a month number. Below is the SAS code with the PROC Format giving us the month names.

proc format;
   value monthname 1 = 'January'
                   2 = 'February'
                   3 = 'March'
                   4 = 'April'
                   5 = 'May'
                   6 = 'June'
                   7 = 'July'
                   8 = 'August'
                   9 = 'September'
                   10 = 'October'
                   11 = 'November'
                   12 = 'December'
                   other = 'N/A'
                ;
run;

This new ‘monthname’ format will give us the month name from a month number. Using it in a data step, we can get the month names from our date variable.

data data_with_month;
    format monthname monthname.;
	set data_with_dates;
	mo = month(d);
	monthname = mo;
run;

The resulting data set with month names is as shown below:

    monthname          d    mo
1	 December  31DEC2021    12
2	  October  24OCT2020    10
3	 December  12DEC2019    12
4	     June  07JUN2019     6
5	 February  17FEB2021     2
6	  January  12JAN2021     1
7	    March  03MAR2020     3

You can also get the month names using if/else if/else statements in a SAS data step. Below is the code to get the name of a month from a date in SAS.

data data_with_month;
        length monthname $ 12.;
        set data_with_dates;
        mo = month(d);
        if mo = 1 then monthname = "January";
        else if mo = 2 then monthname = "February";
        else if mo = 3 then monthname = "March";
        else if mo = 4 then monthname = "April";
        else if mo = 5 then monthname = "May";
        else if mo = 6 then monthname = "June";
        else if mo = 7 then monthname = "July";
        else if mo = 8 then monthname = "August";
        else if mo = 9 then monthname = "September";
        else if mo = 10 then monthname = "October";
        else if mo = 11 then monthname = "November";
        else if mo = 12 then monthname = "December";
        else monthname = "N/A";
run;

Personally, I like the PROC format way as it is much cleaner and easier to re-use.

One other way to get the name of the week day is to use the built-in SAS MONNAMEw. format. While this will work, I like the custom PROC format that I have for you above.

Hopefully this article has been helpful for you to understand how to get the month from a date variable in SAS.

Categorized in:

SAS,

Last Update: February 26, 2024