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!
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.)
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.)
Thanks for your response!
I actually need it to be in SAS date format (ie, the number of days from Jan 01 1960).
Is your original variable a character or numeric variable? Sample data is always helpful.
The original variable is in datetime20 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.
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 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.
It says that the variable is numeric with a format and informat of datetime20.
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.
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.;
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!
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.
That is exactly what the DATEPART() function does. It converts number of seconds into number of days. Basically it divides by 24*60*60.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.