SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 43954 views
  • 5 likes
  • 5 in conversation