## Datetime, adding time, minutes, ...

Solved
Regular Contributor
Posts: 246

# Datetime, adding time, minutes, ...

I got the following problem:

It works when varA = month, day, ... but the function intnx needs a datetime when varA is hour.

I got a date: 29AUG2011:17:15:00

I got a varA: HOUR

I got a varB: 10

What I want is new data = date + 10 HOUR

if Postunit eq 'HOUR' then do;

OD1 = put(intnx(varA,date,varB,'same'),datetime20.);

end;

else do;

OverDueTime = input(put(intnx(varA,datepart(date),varB,'same'),date9.)||':'||put(timepart(date),time8.),datetime20.);

end;

I hope my queston is clear..

Accepted Solutions
Solution
‎08-30-2011 10:48 AM
PROC Star
Posts: 8,163

## Datetime, adding time, minutes, ...

I can't test your code at the moment, thus will offer an alternative.  Rather than using the intnx function, why don't you just add 36,000 (i.e., 60*60*10) to the field?  That will increase the datetime by exactly 10 hours.

All Replies
PROC Star
Posts: 8,163

## Datetime, adding time, minutes, ...

From your post I'm not sure what problem you are confronting.  Is the data you have in the field you call "date" a text field, as sas date, or a sas datetime?

If the field is already either a sas date or sas datetime field, then it is simply a number representing either the number of days since Jan 1, 1960 or the number of seconds since Jan 1, 1960.

If you are changing a date field to be a date time field, make sure that you also change the field's format accordingly.

Regular Contributor
Posts: 246

## Datetime, adding time, minutes, ...

When I calculate this, it adds the hours correctly but there are no minutes anymore.

got a date: 29AUG2011:17:15:00

I got a varA: HOUR

I got a varB: 10

OD1 = put(intnx(Postunit,PlannedSchedTime,postval,'same'),datetime20.);

Result = 30AUG2011:05:00:00

Solution
‎08-30-2011 10:48 AM
PROC Star
Posts: 8,163

## Datetime, adding time, minutes, ...

I can't test your code at the moment, thus will offer an alternative.  Rather than using the intnx function, why don't you just add 36,000 (i.e., 60*60*10) to the field?  That will increase the datetime by exactly 10 hours.

PROC Star
Posts: 8,163

## Datetime, adding time, minutes, ...

Alternatively, as I look at your code, don't you need to specify the time shift as dthour rather than just hour?

Regular Contributor
Posts: 184

## Datetime, adding time, minutes, ...

I get the expected result ...

475  data _null_ ;

476  PlannedSchedTime = '29AUG2011:17:15:00'dt ;

477  Postunit = 'HOUR' ;

478  postval = 10 ;

479  OD1 = put(intnx(Postunit,PlannedSchedTime,postval,'same'),datetime20.);

480  put od1= ;

481  run ;

OD1=30AUG2011:03:15:00

`Filipvdr wrote: It is already a datetime.When I calculate this, it adds the hours correctly but there are no minutes anymore.got a date: 29AUG2011:17:15:00I got a varA: HOURI got a varB: 10 OD1 = put(intnx(Postunit,PlannedSchedTime,postval,'same'),datetime20.);Result = 30AUG2011:05:00:00`
PROC Star
Posts: 8,163

## Datetime, adding time, minutes, ...

Howard,

I get the correct result as well, but the documentation is quite clear that one is supposed to/must use the dt prefix for datetime values.  It might work differently on different systems.

Regular Contributor
Posts: 184

## Datetime, adding time, minutes, ...

That would be the tidy way, but I think a Time hour and a DateTime hour both have 3600 seconds.

`art297 wrote:Howard,I get the correct result as well, but the documentation is quite clear that one is supposed to/must use the dt prefix for datetime values.  It might work differently on different systems.`
PROC Star
Posts: 8,163

## Datetime, adding time, minutes, ...

Howard,

Of course I can't disagree with your logic.  And, similarly, day, month and year don't increment the same with dates and datetimes, and will produce an error if used with datetime.

However, since the OP stated that he/she got an incorrect result using a non-DT indicator when using intnx on a datetime field, I simply wonder if the logic holds for all systems.

I think it would be safer to simply precede all indicators with DT if the field to be incremented is a datetime field, and that would be consistent with the documentation.

🔒 This topic is solved and locked.