How to Use DAY, MONTH, and YEAR Functions in SAS


You can use the DAY, MONTH, and YEAR functions in SAS to extract the day, month, and year as numeric values from a date variable.

The following examples show how to use these functions in practice.

Example 1: Extract Day, Month, Year from Date in SAS

Suppose we have the following dataset in SAS that shows the birth date for seven individuals:

/*create dataset*/
data original_data;
    format birth_date date9.;
    input birth_date :date9.;
    datalines;
01JAN2021
22FEB2022
14MAR2022
29MAY2022
14OCT2023
01NOV2024
26DEC2025
;
run;

/*view dataset*/
proc print data=original_data;

We can use the following code to create three new variables that show the day, month, and year of the birth date for each individual:

/*create new dataset*/
data new_data;
    set original_data;
    day = DAY(birth_date);
    month = MONTH(birth_date);
    year = YEAR(birth_date);
run;

/*view new dataset*/
proc print data=new_data;

DAY, MONTH, YEAR functions in SAS

The three new variables show the day, month, and year of the birth_date variable, respectively.

Example 2: Extract Only Month & Year from Date in SAS

The following code shows how to create a new variable that displays just the month and year of a date variable in SAS:

/*create new dataset*/
data new_data;
    set original_data;
    month_year = birth_date;
    format month_year mmyyn6.;
run;

/*view new dataset*/
proc print data=new_data;

Notice that the new variable month_year contains only the month and year of the birth_date variable.

If you’d like the month to appear after the year, simply use a format of yymmn6. instead:

/*create new dataset*/
data new_data;
    set original_data;
    month_year = birth_date;
    format month_year yymmn6.;
run;

/*view new dataset*/
proc print data=new_data;

Additional Resources

The following tutorials explain how to perform other common tasks in SAS:

How to Use Proc Summary in SAS
How to Rename Variables in SAS
How to Create New Variables in SAS
How to Remove Duplicates in SAS

Leave a Reply

Your email address will not be published. Required fields are marked *