How can we utilize the DAY, MONTH, and YEAR functions in SAS? 2

How can we utilize the DAY, MONTH, and YEAR functions in SAS?

The DAY, MONTH, and YEAR functions in SAS allow for efficient manipulation and extraction of specific date components within a dataset. These functions can be utilized to perform various tasks such as calculating age, creating new variables based on specific date criteria, and sorting data by date. By using these functions, SAS users can easily access and analyze specific date information within their datasets, making data analysis and reporting more accurate and streamlined. Additionally, the DAY, MONTH, and YEAR functions can be combined with other SAS functions and procedures to further enhance data analysis capabilities. Overall, the utilization of these functions in SAS can greatly improve the efficiency and accuracy of date-related tasks and enhance the overall data analysis process.

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 printdata=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 printdata=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 printdata=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 printdata=new_data;

Additional Resources

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

Cite this article

stats writer (2024). How can we utilize the DAY, MONTH, and YEAR functions in SAS?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-we-utilize-the-day-month-and-year-functions-in-sas/

stats writer. "How can we utilize the DAY, MONTH, and YEAR functions in SAS?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-we-utilize-the-day-month-and-year-functions-in-sas/.

stats writer. "How can we utilize the DAY, MONTH, and YEAR functions in SAS?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-we-utilize-the-day-month-and-year-functions-in-sas/.

stats writer (2024) 'How can we utilize the DAY, MONTH, and YEAR functions in SAS?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-we-utilize-the-day-month-and-year-functions-in-sas/.

[1] stats writer, "How can we utilize the DAY, MONTH, and YEAR functions in SAS?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. How can we utilize the DAY, MONTH, and YEAR functions in SAS?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top