BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Walternate
Obsidian | Level 7

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!

1 ACCEPTED SOLUTION

Accepted Solutions
CTorres
Quartz | Level 8

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

15 REPLIES 15
CTorres
Quartz | Level 8

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.)

 

Walternate
Obsidian | Level 7

Thanks for your response!

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

Reeza
Super User

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

Walternate
Obsidian | Level 7

The original variable is in datetime20 format.

CTorres
Quartz | Level 8

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.

CTorres
Quartz | Level 8

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

Reeza
Super User

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

DTDATE9.

can you post the proc contents from your dataset please.

CTorres
Quartz | Level 8

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.

Walternate
Obsidian | Level 7

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

Reeza
Super User

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.

SASKiwi
PROC Star

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.;


Walternate
Obsidian | Level 7

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!

Tom
Super User Tom
Super User

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.

Tom
Super User Tom
Super User

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 15 replies
  • 247124 views
  • 10 likes
  • 6 in conversation