Convert HHMM (character format) into HH:MM format

Solved
Occasional Contributor
Posts: 11

Convert HHMM (character format) into HH:MM format

So I have a column that looks like this:

``````data time;

input  time \$4.;

datalines;
0930
1000
0800
1300
1530
;
run;

``````

and I want it to look like this:

09:30

10:00

08:00

13:00

15:30

Which is in a HHMM. or TIME. format

Tried the PUT statement and came back with an error

Is there any simple ways of doing this?

Thanks.

Accepted Solutions
Solution
‎09-07-2017 09:47 PM
Super User
Posts: 9,599

Re: Convert HHMM (character format) into HH:MM format

```data time;
input time \$4.;
time_calc=input(time,hhmmss4.);
format time_calc time5.;
datalines;
0930
1000
0800
1300
1530
;
run;```

All Replies
Valued Guide
Posts: 557

Re: Convert HHMM (character format) into HH:MM format

Maybe i am blind, but i don't see the mentioned put statement in your code.

I suggest using "hhmmss" instead of "hhmm".

Occasional Contributor
Posts: 11

Re: Convert HHMM (character format) into HH:MM format

Sorry I failed to include that in my post! So I guess using hhmmss instead of hhmm is because SAS calculates time based on seconds?
Solution
‎09-07-2017 09:47 PM
Super User
Posts: 9,599

Re: Convert HHMM (character format) into HH:MM format

```data time;
input time \$4.;
time_calc=input(time,hhmmss4.);
format time_calc time5.;
datalines;
0930
1000
0800
1300
1530
;
run;```
Occasional Contributor
Posts: 11

Re: Convert HHMM (character format) into HH:MM format

Thanks a lot! Now I know what I'm doing wrong.
Super Contributor
Posts: 271

Re: Convert HHMM (character format) into HH:MM format

I could not find relevant format to read it direclty. But you can use below code to get desired result.

``````data time(keep=t1 time);

input  time \$4.;
t1=substr(time,1,2)*3600 +substr(time,3,2)*60 ;
format t1 time5.;
datalines;
0930
1000
0800
1300
1530
;
run;``````
☑ This topic is solved.