BookmarkSubscribeRSS Feed
mmm7
Calcite | Level 5

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. 

 

4 REPLIES 4
Reeza
Super User

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. 

 


 

FreelanceReinh
Jade | Level 19

@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;

 

Tom
Super User Tom
Super User

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
ballardw
Super User

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
;

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 4 replies
  • 1665 views
  • 4 likes
  • 5 in conversation