DATA Step, Macro, Functions and more

change numeric values to sas time format

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 121
Accepted Solution

change numeric values to sas time format

HI!

 

I have numeric values in time variables using military time.  When I try the HHMM. the output is not correct

 

My data values look like this

Time
120
2130
10
1128
23
50

 

 

Want to look like this:

 

Time Want
120 01:20
2130 21:30
10 00:10
1128 11:28
23 00:23
50 00:50

 

 

Any suggestion appreciated!!


Accepted Solutions
Solution
2 weeks ago
Trusted Advisor
Posts: 1,615

Re: change numeric values to sas time format

[ Edited ]

Before you can use a time format, you have to convert the numbers to date-time numbers.

 

Something like this:

 

time2=hms(floor(time/100),mod(time,100),0); /* Convert time to hours and minutes using the hms function */
format time2 hhmm5.;

 

View solution in original post


All Replies
PROC Star
Posts: 288

Re: change numeric values to sas time format

Probably a slicker way, but this should work:

 

data have;
input have;
timepad =  strip(put(have, z4.));
want = hms(substr(timepad, 1, 2), substr(timepad, 3, 2), 0);
format want time5.;
datalines;
120
2130
10
1128
23
50
;
Solution
2 weeks ago
Trusted Advisor
Posts: 1,615

Re: change numeric values to sas time format

[ Edited ]

Before you can use a time format, you have to convert the numbers to date-time numbers.

 

Something like this:

 

time2=hms(floor(time/100),mod(time,100),0); /* Convert time to hours and minutes using the hms function */
format time2 hhmm5.;

 

PROC Star
Posts: 172

Re: change numeric values to sas time format

Mine isn't smart, but fun though-

 

data have;
input time;
datalines;
120
2130
10
1128
23
50
;
data want;
set have;
new_time=cats(reverse(substr(reverse(time),3)),':',substr(time,length(time)-1));
if length(new_time)=3 then new_time=cats('00',new_time);
run;

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 101 views
  • 4 likes
  • 4 in conversation