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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 4496 views
  • 1 like
  • 2 in conversation