BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Amber_Nicole94
Obsidian | Level 7

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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?  

PaigeMiller
Diamond | Level 26

Or even simpler:

 

	new_time=input(char_time,hhmmss4.);
--
Paige Miller
Amber_Nicole94
Obsidian | Level 7

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

 

Thank you!

kbelang1
Fluorite | Level 6

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

PeterClemmensen
Tourmaline | Level 20
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;

sas-innovate-2024.png

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.

 

Register now!

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
  • 6 replies
  • 39173 views
  • 5 likes
  • 5 in conversation