SAS Functions SAS How To's

How to Convert a Datetime to a SAS Date Format

You have probably already noticed that SAS provides two formats to store information about dates, namely as a Datetime variable or as a Date variable. So, to compare them, you need to convert a Datetime variable into a Date variable first. In this article, we explain how to do extract only the Date from a Datetime variable.

The DATEPART function in SAS converts a Datetime variable into a Date variable. This function takes as an argument a Datetime variable and returns only the Date part.

In this article, we look into more detail how to use the DATEPART function.

How to Convert a Datetime into a Date with the SAS DATEPART Function

The DATEPART function converts your Datetime variable into a Date variable and requires one argument. The argument is, obviously, a Datetime variable.

DATEPART(DateTime Variable)

Why Does the DATEPART Function Convert a Datetime into a Number

As you might know, SAS Datetime variables and Date variables are stored as the number of seconds (for Datetime) or days (for Dates) from the 1st of January, 1960. So, if you use the DATEPART function, it returns the number of days between your date and the 1st of January, 1960. For example:

DATEPART("31AUG2020:0:0:0"dt) --> 22.158

However, to make the results of the DATEPART function interpretable, we need to apply a Date format. In the example below, we extract the datepart of a Datetime variable and present the result in 4 ways:

  • Without a format, i.e., the number of days from the 1st of January, 1960.
  • With the MM/DD/YYYY format, e.g., 08/31/2020
  • With the YYYY/MM/DD format, e.g., 2020-08-31
  • With the DATE9. format, e.g., 31AUG2020

For a complete list of Date formats, check out this page.

Convert a Datetime into a SAS Date with different formats
data work.my_ds;
	format my_datetime datetime23.;
	input my_datetime :datetime23.;
	datalines;
31AUG2020:0:0:0
01SEP2020:0:0:0
30SEP2020:0:0:0
15OCT2020:0:0:0
;
run;
 
data work.my_ds_format;
	set work.my_ds;
 
	my_date_no_format = datepart(my_datetime);
	my_date_mmddyyyy = datepart(my_datetime);
	my_date_yyyymmdd = datepart(my_datetime);
	my_date_date9 = datepart(my_datetime);
 
	format my_date_mmddyyyy mmddyy10.;
	format my_date_yyyymmdd yymmdd10.;
	format my_date_date9 date9.;
run;

Use the DATEPART Function to Filter Data

In the example above, we showed how to create a new Date variable extracting the datepart of a Datetime variable, and applying a format. You can also use the DATEPART function to filter your data. For example, we filter all observations until the 1st of September, 2020.

data work.my_ds_filter;
	set work.my_ds;
 
	if datepart(my_datetime) le "01SEP2020"D 
	then output;
run;

As you can see we used a Date format to filter the data (“01SEP2020″D). However, in the output dataset work.my_ds_filter, then column my_datetime has still the Datetime format.

Use the DATEPART Function in Another Function

As mentioned before, you can’t compare a Datetime variable with a Date variable. However, some functions that use Date(time) variables as arguments require that all arguments are of the same type. So, how do you convert a Datetime into a Date variable in a function?

You can easily convert your Datetime variable into a Date variable within a SAS function. For example, below we use the DATEPART function within the INTCK function to calculate the number of days between two dates.

Convert a Datetime into a Date in a SAS Function
data work.my_ds;
	format my_datetime datetime23. my_date date9.;
	input my_datetime :datetime23. my_date :date9.;
	datalines;
31AUG2020:0:0:0 04SEP2020
01SEP2020:0:0:0 12OCT2020
30SEP2020:0:0:0 17NOV2020
15OCT2020:0:0:0 2JAN2021
;
run;
 
data work.diff_in_days;
	set work.my_ds;
 
	diff_in_days = intck("day", datepart(my_datetime), my_date);
run;

Do you know? How to Convert a Number into a SAS Date

3 thoughts on “How to Convert a Datetime to a SAS Date Format

Comments are closed.