Help using Base SAS procedures

Datetime Field DHMS weird behavior

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 90
Accepted Solution

Datetime Field DHMS weird behavior

SAS 9.2

I have a date field appointment_date and it shows '10Jan2014'. It is a date field and not text field.

And then I run this SAS code:

data NCA_OAOS3x;

set NCA_OAOS3 (where=(oaos_case_no='S140106-5'));

appt_date_timex=dhms(appointment_date,12,0,0);

format appt_date_timex datetime.;

run;

It returns 11JAN14:02:00:00

And if I run this SAS code:

data NCA_OAOS3x;

set NCA_OAOS3 (where=(oaos_case_no='S140106-5'));

appt_date_timex=dhms(appointment_date,0,0,0);

format appt_date_timex datetime.;

run;

It returns 10JAN14:14:00:00

Can anyone explain why the hour component is not returning as expected? There seems to be hour component in the date field. Date field is actually '10Jan14:14:00:00'


Accepted Solutions
Solution
‎06-10-2015 11:44 AM
Super User
Posts: 10,466

Re: Datetime Field DHMS weird behavior

Provide a numeric, not date formatted value of your variable.
You might get this behavior if there are some decimals floating around.

If this:

appt_date_timex=dhms(floor(appointment_date),12,0,0);

fixes the problem that was the likely cause.

View solution in original post


All Replies
Solution
‎06-10-2015 11:44 AM
Super User
Posts: 10,466

Re: Datetime Field DHMS weird behavior

Provide a numeric, not date formatted value of your variable.
You might get this behavior if there are some decimals floating around.

If this:

appt_date_timex=dhms(floor(appointment_date),12,0,0);

fixes the problem that was the likely cause.

Frequent Contributor
Posts: 90

Re: Datetime Field DHMS weird behavior

ballardw,

Thanks, it worked using floor. But why would there be a decimal in a date statement? Anyway it worked.

Super User
Posts: 10,466

Re: Datetime Field DHMS weird behavior

I would go back in your processes. Somebody may have converted hours to fraction of a day for another purpose. OR used a year to days conversion multiplying by 356.25 somewhere.

Frequent Contributor
Posts: 90

Re: Datetime Field DHMS weird behavior

I imported the data from an excel file. And in excel I can see 10-Jan-2014 in formatted. The underlying in excel could be having decimal.

Lesson learnt: It is always safe to floor the date field then. Among a dataset of 10,000 rows, only a few of them had decimal and I was not aware of it unless my user feedback to me.

I thought the whole point of a date field is not to have any time component.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 270 views
  • 0 likes
  • 2 in conversation