BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
UcheOkoro
Lapis Lazuli | Level 10

Hello,

 

I have datetime written like this " 19724.704861"and I need to convert it to the SAS datetime format.

 I used the following code but it did not work

DATA Pain2;
SET Pain;
Drugtime = datepart(medtime_sec);
format Drugtime  DATETIME.;
RUN;

 

Please, I need some help.

 

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You appear to already have a date value (number of days).  SAS counts days from 1960 with 01JAN1960 being day zero.

1798  data test;
1799    input x ;
1800    put x= 'As Date= ' x date9.;
1801  cards;

x=0 As Date= 01JAN1960
x=1 As Date= 02JAN1960
x=19724.704861 As Date= 01JAN2014

If you want to convert it to datetime value (so you can use the DATETIME format to display it) then multiply the number of seconds in a day.

data Pain2;
  set Pain;
  Drugtime = medtime_sec * '24:00't ;
  format Drugtime  datetime19.;
run;

 

View solution in original post

9 REPLIES 9
Reeza
Super User

The variable is a datetime variable. 

DATEPART() takes a datetime and keeps only the date portion, so you now there is a date variable, not a datetime variable.

Then the format applied to the date variable is a datetime format. This is mixing and matching up the variable types and formats.

 

Instead, apply your datetime format to your datetime variable and see if that's what you want. 

Or take the date variable and apply a date format to it.

 

DATA Pain2;
SET Pain;
format medtime_sec  DATETIME.;

drugTime = datepart(medtime_sec);
format drugTime date9.;

RUN;

 

@UcheOkoro wrote:

Hello,

 

I have datetime written like this " 19724.704861"and I need to convert it to the SAS datetime format.

 I used the following code but it did not work

DATA Pain2;
SET Pain;
Drugtime = datepart(medtime_sec);
format Drugtime  DATETIME.;
RUN;

 

Please, I need some help.

 

Thank you.


 

UcheOkoro
Lapis Lazuli | Level 10

Thank you, Reeza for your prompt response.

I used the following codes because I wanted the datetime:

DATA Pain2;
SET Pain;
format medtime_sec  DATETIME.;

However, it gave me date as  01Jan1960 for all the observations and different times. The year is 2014 and not 1960.

 

Thank you.

UcheOkoro
Lapis Lazuli | Level 10

Thank you so much for your prompt response.

The date is  01JAN2014 .

 

Thank you.

 

 

Kurt_Bremser
Super User

This must be a datetime value coming from other software. If it was a SAS datetime, it would be a time early in the morning of 1960-01-01.

The way it looks, the number to the left of the period is a count of days, while the fractional part represents the time on that day.

 

In order to make this a SAS datetime value, we need to know the offset of the date part (the date of day zero or day one), or the timestamp represented by this number.

ballardw
Super User

Can you at least tell us what actual date and time that number is supposed to represent?

I do not clearly see any indication of year, month or day of month in the value.

 

And with a name like medtime_sec I might be tempted to guess that the value represents the number of seconds after a medication was given. Which would make this value an interval and not a date or datetime variable.

If my guess is correct then what you need is the datetime of administration and then add this interval to it. Maybe.

UcheOkoro
Lapis Lazuli | Level 10

I cannot tell the time but  I am using the dates from other variables such as the arrival date and departure date to estimate the date. the date is 01JAN2014. It is not an interval but a fixed time. Now, I am not sure if it actually includes date or just time.

 

Thank you. 

Tom
Super User Tom
Super User

You appear to already have a date value (number of days).  SAS counts days from 1960 with 01JAN1960 being day zero.

1798  data test;
1799    input x ;
1800    put x= 'As Date= ' x date9.;
1801  cards;

x=0 As Date= 01JAN1960
x=1 As Date= 02JAN1960
x=19724.704861 As Date= 01JAN2014

If you want to convert it to datetime value (so you can use the DATETIME format to display it) then multiply the number of seconds in a day.

data Pain2;
  set Pain;
  Drugtime = medtime_sec * '24:00't ;
  format Drugtime  datetime19.;
run;

 

UcheOkoro
Lapis Lazuli | Level 10

Thank you so much!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 9 replies
  • 1672 views
  • 0 likes
  • 5 in conversation