In SAS you can store dates as a DateTime variable or a Date variable. However, you can’t compare these two types of variables. Therefore, you need to convert one of these. You can use the DATEPART function. This function reads the date part of a DateTime variable. and converts it into a Date variable. The syntax of the DATEPART function is:
The datetime parameter of the DATEPART function can be a variable or a constant. Since both Date and DateTime variables in SAS are in fact numbers, it’s necessary to format the new variables to make them easier to interpret. The code below shows an example.
DATA WORK.DS; SET SASHELP.TIMEDATA; /* CONVERT A DATETIME INTO A DATE VARIABLE */ MY_DATE_VAR = DATEPART(DATETIME); FORMAT MY_DATE_VAR DATE7.; RUN;
However, if you want to convert a Date variable into a DateTime variable, see this example.
You can find the official SAS documentation of the DATEPART function here.
2 thoughts on “How to Quickly Convert DateTime to Date variable”
Comments are closed.