DATA Step, Macro, Functions and more

Converting time to a numeric variable

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 106
Accepted Solution

Converting time to a numeric variable

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; 


Accepted Solutions
Solution
a month ago
Super User
Super User
Posts: 7,074

Re: Converting time to a numeric variable

[ Edited ]
Posted in reply to sarahsasuser

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


All Replies
Solution
a month ago
Super User
Super User
Posts: 7,074

Re: Converting time to a numeric variable

[ Edited ]
Posted in reply to sarahsasuser

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.

Frequent Contributor
Posts: 106

Re: Converting time to a numeric variable

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

Super User
Super User
Posts: 7,074

Re: Converting time to a numeric variable

[ Edited ]
Posted in reply to sarahsasuser

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.

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 80 views
  • 0 likes
  • 2 in conversation