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;
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.
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.
Thanks Tom, I found the intick function to be the most efficient.
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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.