DATA Step, Macro, Functions and more

How do I convert character time to numeric time?

Accepted Solution Solved
Reply
Contributor
Posts: 28
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: 9,416

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

Posted in reply to Amber_Nicole94

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: 9,416

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

Posted in reply to Amber_Nicole94

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?  

Respected Advisor
Posts: 2,825

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

[ Edited ]

Or even simpler:

 

	new_time=input(char_time,hhmmss4.);
--
Paige Miller
Contributor
Posts: 28

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

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

 

Thank you!

New Contributor
Posts: 3

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

Hi, why did you use "00" and why wont it work with any other time format such as HHMM.? Thanks!

Super User
Super User
Posts: 9,416

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

Please don't re-open old questions with new questions.  I add 00 so that the string is the correct format for the format I am using.  In the format the length is default 6, so 2 char for hour, and for min, and for sec.  As the data only has 4, 2 for hour, 2 for min, then I added 2 for second.  See @PaigeMiller's response on how to  shorten the length so that adding seconds is not needed.

PROC Star
Posts: 1,215

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

Posted in reply to Amber_Nicole94
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
  • 6 replies
  • 2919 views
  • 0 likes
  • 5 in conversation