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