BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Filipvdr
Pyrite | Level 9

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..

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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.

View solution in original post

8 REPLIES 8
art297
Opal | Level 21

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.

Filipvdr
Pyrite | Level 9

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:00

I got a varA: HOUR

I got a varB: 10

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

Result = 30AUG2011:05:00:00

art297
Opal | Level 21

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.

art297
Opal | Level 21

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

Howles
Quartz | Level 8

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:00

I got a varA: HOUR

I got a varB: 10

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

Result = 30AUG2011:05:00:00

art297
Opal | Level 21

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.

Howles
Quartz | Level 8

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.

art297
Opal | Level 21

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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 8 replies
  • 27620 views
  • 0 likes
  • 3 in conversation