DATA Step, Macro, Functions and more

Convert datetime16 to datetime9

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 77
Accepted Solution

Convert datetime16 to datetime9

Hello, I have a dataset that has a date variable that is in datetime16. I would like to convert it to datetime9. Not quite sure how to do this. I tried this sas code but the output jarbled the date variable to "*******"

 

data janfeb;
set janfeb;
  format dcdeathdate date9.;
run;

 

Here is a sample dataset:

data JANFEB;
  infile datalines dsd truncover;
  input dcdeathdate:DATETIME16. nationalid:$24. HCVScreening:$9. HCVresult:$27.;
datalines4;
19JAN17:00:00:00,01001000070,No,
10JAN17:00:00:00,01001000357,Yes,Negative
20JAN17:00:00:00,01001000402,Yes,Negative
02FEB17:00:00:00,01001000799,Yes,Negative
18FEB17:00:00:00,01001000859,No,
;;;;

Thanks for your help.


Accepted Solutions
Solution
‎04-25-2017 09:40 AM
Super User
Posts: 7,762

Re: Convert datetime16 to datetime9

SAS datetime values are seconds from 01-01-1960:00:00:00; date values are days from 01-01-1960.

To extract the date from a datetime value, use the datepart() function.

data JANFEB;
  infile datalines dsd truncover;
  input dcdeathdate:DATETIME16. nationalid:$24. HCVScreening:$9. HCVresult:$27.;
datalines4;
19JAN17:00:00:00,01001000070,No,
10JAN17:00:00:00,01001000357,Yes,Negative
20JAN17:00:00:00,01001000402,Yes,Negative
02FEB17:00:00:00,01001000799,Yes,Negative
18FEB17:00:00:00,01001000859,No,
;;;;
run;

data janfeb;
set janfeb;
dcdeathdate = datepart(dcdeathdate);
format dcdeathdate date9.;
run;

Or you simply assign the datetime9. format to dcdeathdate; it is then still a datetime value, but only the date is displayed.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎04-25-2017 09:40 AM
Super User
Posts: 7,762

Re: Convert datetime16 to datetime9

SAS datetime values are seconds from 01-01-1960:00:00:00; date values are days from 01-01-1960.

To extract the date from a datetime value, use the datepart() function.

data JANFEB;
  infile datalines dsd truncover;
  input dcdeathdate:DATETIME16. nationalid:$24. HCVScreening:$9. HCVresult:$27.;
datalines4;
19JAN17:00:00:00,01001000070,No,
10JAN17:00:00:00,01001000357,Yes,Negative
20JAN17:00:00:00,01001000402,Yes,Negative
02FEB17:00:00:00,01001000799,Yes,Negative
18FEB17:00:00:00,01001000859,No,
;;;;
run;

data janfeb;
set janfeb;
dcdeathdate = datepart(dcdeathdate);
format dcdeathdate date9.;
run;

Or you simply assign the datetime9. format to dcdeathdate; it is then still a datetime value, but only the date is displayed.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,039

Re: Convert datetime16 to datetime9

Dates are stored in days and Time and DateTime are stored in seconds.

You can apply a different format (DTDATE9. for example).

You can convert using the DATEPART() function.

Or you could read it using a different informat that ignores the time part.

data JANFEB;
  infile datalines dsd truncover;
  length dcdeathdate dcdeathdatetime 8 nationalid $24 HCVScreening $9 HCVresult $27 ;
  informat dcdeathdate date7. dcdeathdatetime datetime16.;
  format dcdeathdate date9. dcdeathdatetime datetime20.;
  input dcdeathdate @1 dcdeathdatetime nationalid HCVScreening HCVresult ;
datalines4;
19JAN17:00:00:00,01001000070,No,
10JAN17:00:00:00,01001000357,Yes,Negative
20JAN17:00:00:00,01001000402,Yes,Negative
02FEB17:00:00:00,01001000799,Yes,Negative
18FEB17:00:00:00,01001000859,No,
;;;;

 

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 211 views
  • 1 like
  • 3 in conversation