DATA Step, Macro, Functions and more

Converting datetime to SAS date format

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 138
Accepted Solution

Converting datetime to SAS date format

Hi,

I have a SAS dataset with a date variable that is in datetime format. I need to have it in SAS date format to run some code, but I'm not sure how to convert a variable from datetime format to SAS date format.

Any help is much appreciated.

Thanks!


Accepted Solutions
Solution
‎03-24-2017 04:14 PM
Regular Contributor
Posts: 180

Re: Converting datetime to SAS date format

[ Edited ]

Create a new date variable with the DATEPART function:

 

date=datepart(datetime);

 

A SAS datetime is the number of seconds since (or until) Jan 1, 1960.  A SAS date is the number of days since (or until) Jan 1, 1960.  The DATEPART function converts the datetime to a date value.

 

Or if you simply need to format it for display purposes, or as a class variable in a SAS proc, use the DTDATE9. format to display just the date portion.

 

(Editor's note: more example and explanation in this blog post.)

 

View solution in original post


All Replies
Solution
‎03-24-2017 04:14 PM
Regular Contributor
Posts: 180

Re: Converting datetime to SAS date format

[ Edited ]

Create a new date variable with the DATEPART function:

 

date=datepart(datetime);

 

A SAS datetime is the number of seconds since (or until) Jan 1, 1960.  A SAS date is the number of days since (or until) Jan 1, 1960.  The DATEPART function converts the datetime to a date value.

 

Or if you simply need to format it for display purposes, or as a class variable in a SAS proc, use the DTDATE9. format to display just the date portion.

 

(Editor's note: more example and explanation in this blog post.)

 

Frequent Contributor
Posts: 138

Re: Converting datetime to SAS date format

Thanks for your response!

I actually need it to be in SAS date format (ie, the number of days from Jan 01 1960).

Super User
Posts: 17,963

Re: Converting datetime to SAS date format

Is your original variable a character or numeric variable? Sample data is always helpful.

Frequent Contributor
Posts: 138

Re: Converting datetime to SAS date format

The original variable is in datetime20 format.

Regular Contributor
Posts: 180

Re: Converting datetime to SAS date format

If the original variable is in datetime20. format it means the type of this variable is datetime. You cannot give this variable a date. format. You need to change the type from datetim to date to be able to apply the date format.

Regular Contributor
Posts: 180

Re: Converting datetime to SAS date format

It is not the format what makes a variable date or datetime It's the contents: if it contains the number of days since Jan 01 1960 then it is a date but if it contains the number of seconds it is a datetime.

For this reason you can not have a date variable with datetime format and viceversa (a datetime variable with date format). You need to change the contents of a variable to change the type from date to datetime or viceversa

Regards

Super User
Posts: 17,963

Re: Converting datetime to SAS date format

You can have a datetime variable with a date format...

DTDATE9.

can you post the proc contents from your dataset please.

Regular Contributor
Posts: 180

Re: Converting datetime to SAS date format

Reeza you are right! Thanks.

You can display the datepart of a datetime variable with the DTDATE9. format but this does not change the type of the variable.

Frequent Contributor
Posts: 138

Re: Converting datetime to SAS date format

It says that the variable is numeric with a format and informat of datetime20.

Super User
Posts: 17,963

Re: Converting datetime to SAS date format

Then CTorres solution should work. If its not, post the code and log.

Usually errors like this are caused by 1) Character rather than numeric 2)typos 3) Not overwriting a variable or dataset properly.

Super User
Posts: 3,115

Re: Converting datetime to SAS date format

If you want to permanently convert a datetime column to date and store it as such assign date informat and format at the same time as converting it:

datevar = datepart(datevar);

format datevar date9.;

informat datevar date9.;


Frequent Contributor
Posts: 138

Re: Converting datetime to SAS date format

Hi all,

Thanks for your help, but I think I'm not communicating my issue well enough so let me try to be clearer.

I have a column that is currently akin to:

Date1

21DEC198700:00:00

22DEC198800:00:00

08AUG198700:00:00

etc.

All I want is to convert this to SAS date, so that I will have a new column:

Date2

39876 (or however many days Dec. 21, 1987 is from Jan. 1, 1960)

39877

39801

I know the numbers aren't correct, but basically I just want my DDMMMYYYYTT:TT:TT format converted to the sas date "number of days since Jan. 1, 1960" format.

Thanks!

Super User
Super User
Posts: 6,502

Re: Converting datetime to SAS date format

Not sure about the missing colon between your date and time parts, but if your variable is a NUMBER instead of a character string then DATEPART() will convert it. If you want it to display as a date by default then attach the format you want to it.

45   data want ;

46    set have ;

47    put date1= datetime20. @25 date1 comma15. ;

48    date1 = datepart(date1);

49    put date1= date9. @25 date1 comma15. ;

50    format date1 date9.;

51    informat date1 date9.;

52   run;

date1=21DEC1987:00:00:00    882,662,400

date1=21DEC1987                  10,216

date1=22DEC1988:00:00:00    914,371,200

date1=22DEC1988                  10,583

date1=08AUG1987:00:00:00    870,998,400

date1=08AUG1987                  10,081


NOTE: There were 3 observations read from the data set WORK.HAVE.

NOTE: The data set WORK.WANT has 3 observations and 1 variables.

Super User
Super User
Posts: 6,502

Re: Converting datetime to SAS date format

That is exactly what the DATEPART() function does. It converts number of seconds into number of days.  Basically it divides by 24*60*60.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 15 replies
  • 23799 views
  • 4 likes
  • 6 in conversation