Quartz | Level 8

## How dhms is calculating hours, minutes and seconds?

Hi All,

In the

https://documentation.sas.com/, I see the following code:

data one;
dtid=dhms('09feb21'd, 15, 30, 15);
put dtid;
put dtid datetime.;

dtid2=dhms('09feb21'd, 15, 30, 61);
put dtid2;
put dtid2 datetime.;

dtid3=dhms('09feb21'd, 15, .5, 15);
put dtid3;
put dtid3 datetime.;
run;

SAS writes these results to the log:

```1928503815
09FEB21:15:30:15
1928503861
09FEB21:15:31:01
1928502045
09FEB21:15:00:45```

Can anyone please clarify why dtid3 is showing 15:00:45 for the time part when we have given 15 for hours, .5 for minutes and 15 for seconds?

For dtid2, I am thinking that it is calculating as follows:

we have given 15,30, 61 for hour, minute and second respectively. Since 61 seconds = 1 min + 1 second, hence in the output we have 30+ this 1 minute=31 minutes and for seconds, it is taking the remaining 1 second out of 61 seconds. Please, let me know if my understanding regarding dtid2 is correct or not.

For dtid3, I am not able to apply such logic. Please, help me understand how dhms works with the time part and why dtid2 time part is 15:00:45.

1 ACCEPTED SOLUTION

Accepted Solutions
Diamond | Level 26

## Re: How dhms is calculating hours, minutes and seconds?

@Moksha wrote:

Can anyone please clarify why dtid3 is showing 15:00:45 for the time part when we have given 15 for hours, .5 for minutes and 15 for seconds?

Let's see here ... 15 hours, one half a minute and 15 seconds ... without using SAS, that's 15 hours, and then half a minute and then 15 seconds, how many seconds is that? How many minutes is half a minute and then another 15 seconds? Please figure that out in your head, without SAS. Show the steps/explain your answer.

--
Paige Miller
10 REPLIES 10
Diamond | Level 26

## Re: How dhms is calculating hours, minutes and seconds?

@Moksha wrote:

Can anyone please clarify why dtid3 is showing 15:00:45 for the time part when we have given 15 for hours, .5 for minutes and 15 for seconds?

Let's see here ... 15 hours, one half a minute and 15 seconds ... without using SAS, that's 15 hours, and then half a minute and then 15 seconds, how many seconds is that? How many minutes is half a minute and then another 15 seconds? Please figure that out in your head, without SAS. Show the steps/explain your answer.

--
Paige Miller
Quartz | Level 8

## Re: How dhms is calculating hours, minutes and seconds?

Thanks PaigeMiller. As mentioned by you, now I understand that .5 minutes is 30 seconds. So, out of 60 seconds (1 minute), we have mentioned 30 seconds only, hence minute part is 00 and this 30 seconds added to the seconds part and hence 30 + 15 seconds = 45 seconds. Is that correct?

Diamond | Level 26

## Re: How dhms is calculating hours, minutes and seconds?

@Moksha wrote:

Thanks PaigeMiller. As mentioned by you, now I understand that .5 minutes is 30 seconds. So, out of 60 seconds (1 minute), we have mentioned 30 seconds only, hence minute part is 00 and this 30 seconds added to the seconds part and hence 30 + 15 seconds = 45 seconds. Is that correct?

Correct! 🙂 👍 💯

--
Paige Miller
Quartz | Level 8

## Re: How dhms is calculating hours, minutes and seconds?

Ok, thank you very much PaigeMiller. Your answer helped me a lot.

Super User

## Re: How dhms is calculating hours, minutes and seconds?

@Moksha wrote:

Ok, thank you very much PaigeMiller. Your answer helped me a lot.

Now with that understanding what would you expect for

`dtid4=dhms('09feb21'd, 39, 30, 61);`

As in what happens when hours are increased for 15 to 39 (hint: +24)

Quartz | Level 8

## Re: How dhms is calculating hours, minutes and seconds?

Hi ballardw, thanks for bringing this out.

I have tried the code you have given:

dtid4=dhms('09feb21'd, 39, 30, 61);
put dtid4;
put dtid4 datetime.;

and output is :

1928590261
10FEB21:15:31:01

My understanding is that : 39 hours = 24 hours + 15 hours. Out of these, since 24 hours is 1 day, date changed to 10FEB2021 and hence remaining 15 hours are mentioned for hours.

Now, for minutes and seconds: since 61 seconds = 1 minute + 1 second, out of these 1 minute is added to 30 minutes and hence 31 minutes and the seconds part is 1 second.

Hope, my understanding is correct.

Super User

## Re: How dhms is calculating hours, minutes and seconds?

Datatime values are number of seconds since the start of 1960.

It is just when you format it for display using something like the DATETIME format that the concept of days or hours or minutes comes into play.

Super User

## Re: How dhms is calculating hours, minutes and seconds?

Correct, though actually running the code should have been to confirm what you expected.

The behavior of the DHMS and the related MDY functions is a bit more flexible than sometimes expected. I had what I thought was an odd value when doing some conversion from some old files with a date value that was out of order. The records were very intermittent dates and could go a fair amount of time between measures but the records were supposed to be sequential. After reading the source file using the MDY function to build the dates one was out of order. The Day value was 67. So a November date, that likely should have been either 6 or 7 became one in January of the next year.

Part of the fun with ETL processes. Trying to track down the correct date on a file that originated as manual data entry 20 years earlier was entertaining.

Quartz | Level 8

## Re: How dhms is calculating hours, minutes and seconds?

Thank you.

Yes, the dhms() and mdy() functions results for some values are little bit tricky.

Super User

## Re: How dhms is calculating hours, minutes and seconds?

It is just doing arithmetic.

``````DateTime
= Number_of_days*(Seconds_per_day)
+ Number_of_Hours*(Seconds_per_hour)
+ Number_of_Minutes*(Seconds_per_minute)
+ Number_of_Seconds
;``````

Or

``DateTime = D*'24:00:00't + H*'01:00:00't+M*'00:01:00't+S;``
Discussion stats
• 10 replies
• 467 views
• 2 likes
• 4 in conversation