DATA Step, Macro, Functions and more

How do I convert character time to numeric time?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

How do I convert character time to numeric time?

I have a data set with multiple time variables.

 

Time Variables: GCO Time, AVB Time, Event Time, etc.

Times: 0800 1200 0300 etc.

 

When I input my data set it inputs those variables as character functions. However, I am going to need to add and subtract the times, so character format will not work. I have tried to change the format when the dataset inputs to a time format, however I recieve an error when I do it that way. This should be an easy fix, just cant seem to find the solution.

 

I am using SAS Enterprise Guide 5.1

 


Accepted Solutions
Solution
‎06-08-2017 09:53 AM
Super User
Super User
Posts: 7,404

Re: How do I convert character time to numeric time?

You can't format a column as time format if its character as time and date are numeric.  To do it your way you would need to create a new numeric variable with the time5. format and input() the data into that:

data want;
  set have;
  new_time=input(cats(char_time,"00"),hhmmss.);
  format new_time time5.;
run;

Although I would first check how your getting the data in, is it some sort of import?  If so is it from Excel maybe?  

View solution in original post


All Replies
Solution
‎06-08-2017 09:53 AM
Super User
Super User
Posts: 7,404

Re: How do I convert character time to numeric time?

You can't format a column as time format if its character as time and date are numeric.  To do it your way you would need to create a new numeric variable with the time5. format and input() the data into that:

data want;
  set have;
  new_time=input(cats(char_time,"00"),hhmmss.);
  format new_time time5.;
run;

Although I would first check how your getting the data in, is it some sort of import?  If so is it from Excel maybe?  

Trusted Advisor
Posts: 1,615

Re: How do I convert character time to numeric time?

[ Edited ]

Or even simpler:

 

	new_time=input(char_time,hhmmss4.);
Occasional Contributor
Posts: 17

Re: How do I convert character time to numeric time?

input(cats(char_time,"00"),hhmmss.) worked perfectly!

 

Thank you!

PROC Star
Posts: 552

Re: How do I convert character time to numeric time?

data have;
input GCO$ AVB$ Event$;
datalines;
0800 1200 0300
;

data want;
	set have;
	num_GCO = input(GCO,hhmmss4.);
	num_AVB = input(AVB,hhmmss4.);
	num_Event = input(Event,hhmmss4.);
	format num: time.;
run;
☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 214 views
  • 0 likes
  • 4 in conversation