How to Convert Datetime to Date in SAS

The easiest way to convert a datetime to a date in SAS is to use the DATEPART function.

This function uses the following basic syntax:

date = put(datepart(some_datetime), mmddyy10.);

The argument mmddyy10. specifies that the date should be formatted like 10/15/2022.

The following example shows how to use this syntax in practice.

Example: Convert Datetime to Date in SAS

Suppose we have the following dataset in SAS that contains one column of datetimes:

/*create dataset*/
data original_data;
    format some_datetime datetime23.;
    input some_datetime :datetime23.;

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

The following code shows how to use the DATEPART function to create a new dataset in which the values in the datetime column are formatted as dates with various formats:

/*create new dataset with datetime formatted as date*/
data new_data;
    set original_data;
    date_mmddyyyy = put(datepart(some_datetime), mmddyy10.);
    date_yyyymmdd = put(datepart(some_datetime), yymmdd10.);
    date_date9 = put(datepart(some_datetime), date9.);
    date_default = datepart(some_datetime);

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

Notice that the four new columns display the date from the original datetime column in various formats.

By default, the DATEPART function converts a datetime to the number of days since January 1, 1960.

Thus, the new column called date_default displays the number of days since January 1, 1960 for each datetime.

Note: You can find the complete documentation for the SAS DATEPART function here.

Additional Resources

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

How to Add Days to Date in SAS
How to Convert Numeric Variable to Date in SAS
How to Calculate Difference Between Two Dates in SAS

Featured Posts

One Reply to “How to Convert Datetime to Date in SAS”

  1. I just tried this and including the PUT function is not an ideal way to do this. It turns it to a character, and then it is much harder to use the variable. If you want it formatted but still in a numeric date format, I had success with the last option without the PUT function plus a format statement after that:

    date_default = datepart(some_datetime);
    format date_default date9.;

Leave a Reply

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