I need help converting a numeric time column into a number. For example, I want to create a new column that converts 05:30 into 5.5 I am not providing a sample dataset as I'm unaware of how to quickly create one using a datalines statement.
Time is stored in seconds. So if you want a number, you can just divide by 60.
time_number = time/60;
format time_number 8.1;
@mmm7 wrote:
I need help converting a numeric time column into a number. For example, I want to create a new column that converts 05:30 into 5.5 I am not providing a sample dataset as I'm unaware of how to quickly create one using a datalines statement.
@Reeza wrote:
Time is stored in seconds. So if you want a number, you can just divide by 60.
Or by 3600 (='1:00't) if your example "05:30" means "5 hours, 30 minutes" or "half past five a.m." (rather than "5 minutes, 30 seconds").
data have;
t='05:30't;
format t time5.;
run;
data want;
set have;
td=t/'1:00't;
run;
SAS stores time in seconds. To convert seconds to minutes divide by one minute ('00:01:00't) or 60 seconds. To convert seconds to hours divide by one hour('01:00:00't) or 60*60 seconds.
data want ;
set have;
minutes = time/60;
hours = time/60/60;
run;
or
data want ;
set have;
minutes = time/'00:01:00't;
hours = time/'01:00:00't;
run;
Example:
386 data test; 387 seconds=time(); 388 minutes=seconds/60; 389 hours=seconds/60/60; 390 put seconds=tod8.; 391 put (_all_) (=/); 392 run; seconds=12:46:06 seconds=45965.954 minutes=766.09923333 hours=12.768320556
Just exactly how do you intend to use that "numeric" version of the time variable?
It will be harder to calculate differences between variables (the function INTCK for time, date and datetime values), harder to display in a nice format consistently, rounded to single decimal for some values may be inaccurate enough to cause problems for some uses.
Easy way to create "time" values in data steps: HMS function and would clarify exactly what "5:30" meant.
data example ; input hour minute second; time = hms(hour,minute,second); format time time8.; datalines; 1 1 1 13 27 43 0 16 22 5 30 0 0 5 30 ;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.