Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- Programming
- /
- How dhms is calculating hours, minutes and seconds?

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

☑ This topic is **solved**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 05-10-2023 09:51 AM
(466 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

@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

Paige Miller

10 REPLIES 10

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

@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

Paige Miller

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

@Moksha wrote:

Correct! 🙂 👍 💯

--

Paige Miller

Paige Miller

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

@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)

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thank you.

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

**If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. **

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.