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

Hi All,

 

I have calculated the time in hours (variable name timehrs) between two datetime variables. I have converted time in hours to time in minutes (timemin). Both of these variables have time formats on them. I'd like to use timemin as a numeric variable to use in bivariate and multivariable calculations. I cannot figure out how to use the actual time in either hours or minutes instead of SAS time. For instance, if the value of timemins should be 1632 minutes, the value being used in calculations is 5875200, which is SAS time. How do I convert timemins and timehrs to real minutes and hours?

 

My code:

Data have;
set a;
by Study_Number;
btime=dhms(date_b, 0,0,time_b);
ctime=dhms(date_c, 0,0,time_c);
timehrs=ctime-btime;
timemin=(timehrs*60);
format btime ctime datetime18. timemin timehrs time4.;
run; 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

SAS stores TIME (and DATETIME) values in SECONDS, not hours or minutes.

If you want to convert SECONDS to MINUTES then divide by 60.  To HOURS then divide by 60**2.

data want;
  set have;
  bdt=dhms(date_b, 0,0,time_b);
  cdt=dhms(date_c, 0,0,time_c);
  timemin=(cdt-bdt)/60;
  timehrs=timemin/60;
  format bdt cdt datetime20. timemin timehrs F4.;
run; 

 Don't attach TIME formats to numbers that aren't in seconds.  Also the DATETIME format does not display DATETIME19. properly. It will only show four digits for the year, even though it should be able to fit four digit years into 19 characters.  So use DATETIME20 instead.

If you want to round the time to minutes or hours then you could use the ROUND()  function

  timemin=round(cdt-bdt,60);
  timehrs=round(cdt-bdt,60**2);
  format timemin timehrs time8.;

or you could use INTNX() function to truncate to MINUTE or HOUR.

  timemin=intnx('minute',cdt-bdt,0,'b');
  timehrs=intnx('hour',cdt-bdt,0,'b');
  format timemin timehrs time8.;

Then you would still have a time value that you use with a TIME format.

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

SAS stores TIME (and DATETIME) values in SECONDS, not hours or minutes.

If you want to convert SECONDS to MINUTES then divide by 60.  To HOURS then divide by 60**2.

data want;
  set have;
  bdt=dhms(date_b, 0,0,time_b);
  cdt=dhms(date_c, 0,0,time_c);
  timemin=(cdt-bdt)/60;
  timehrs=timemin/60;
  format bdt cdt datetime20. timemin timehrs F4.;
run; 

 Don't attach TIME formats to numbers that aren't in seconds.  Also the DATETIME format does not display DATETIME19. properly. It will only show four digits for the year, even though it should be able to fit four digit years into 19 characters.  So use DATETIME20 instead.

If you want to round the time to minutes or hours then you could use the ROUND()  function

  timemin=round(cdt-bdt,60);
  timehrs=round(cdt-bdt,60**2);
  format timemin timehrs time8.;

or you could use INTNX() function to truncate to MINUTE or HOUR.

  timemin=intnx('minute',cdt-bdt,0,'b');
  timehrs=intnx('hour',cdt-bdt,0,'b');
  format timemin timehrs time8.;

Then you would still have a time value that you use with a TIME format.

sarahsasuser
Quartz | Level 8

Thanks Tom, I found the intick function to be the most efficient. 

Tom
Super User Tom
Super User

INTCK() is useful, but remember it is counting how many boundaries you cross. So depending on what you want to count make sure to use the right values for the last parameter.  The default for INTCK('hour',....) will be the number of times you move past 00 minutes.  So '10:59:59't to '11:00:01'T will count as one hour boundary crossed even though it is a difference of just a couple of seconds.

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
  • 3 replies
  • 5276 views
  • 1 like
  • 2 in conversation