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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

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.

Tom
Super User Tom
Super User

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

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 9183 views
  • 3 likes
  • 3 in conversation