DATA Step, Macro, Functions and more

Time Conversion

Accepted Solution Solved
Reply
Contributor
Posts: 33
Accepted Solution

Time Conversion

Hi All,

I have variable I need converted from a number to a time value.  The format now is 15, 30, 45, 100, 115 and I want 12:00AM, 12:15AM, 12:30AM, 12:45AM 1:00AM etc.

Steve


Accepted Solutions
Solution
‎06-24-2015 02:08 PM
Regular Contributor
Posts: 180

Re: Time Conversion

This is Mark Johnson' solution improved:

data want;

set have;

txt=put(number,z4.);

time = put(input(substr(txt,1,2),2.)*3600+(input(substr(txt,3),2.)*60),timeampm.);

drop txt;

run;

CTorres

View solution in original post


All Replies
Regular Contributor
Posts: 200

Re: Time Conversion

RTFM on the

TIMEAMPMw.d Format

attrib MyTimeVar length = 8 format =timeampm5.2;

Super User
Super User
Posts: 7,407

Re: Time Conversion

Post some test data (as a datastep).  At a guess:

Step 1 - split the variable and convert to minutes (note the best coding method, but its to show the process):

data want;

     set have;

     i=1;

     do while (scan(variable,i,",") ne "");

          mins=input(scan(variable,i,","),best.);

          if mins=100 then mins=60;

          if mins=115 then mins=75;

          new_time="12:00"t + mins;

          output;

     end;

run;

Valued Guide
Posts: 858

Re: Time Conversion

Are you sure 100 should be 1:00 am?  You go from 45 minutes to 100 minutes, logically 45 = 12:45am and 100 = 1:00am doesn't jive:

data have;

input number;

cards;

15

30

45

100

115

;

run;

data want;

set have;

time = put(number*60,timeampm.);

run;

Contributor
Posts: 20

Re: Time Conversion

Are you saying that your actual data values are HOUR:MINUTE, but w/o the colon?

0245 (the integer) <=> 02:45 (the time)??

If so, then pull out the hour, minute values, convert to seconds.

(but w/o any data, we're just guessing).

data want; set have;

minutes = mod(badtime,100);

hours = floor(badtime/100);

seconds = hours*3600 + minutes*60;

format seconds timeampm. ;

run;

Solution
‎06-24-2015 02:08 PM
Regular Contributor
Posts: 180

Re: Time Conversion

This is Mark Johnson' solution improved:

data want;

set have;

txt=put(number,z4.);

time = put(input(substr(txt,1,2),2.)*3600+(input(substr(txt,3),2.)*60),timeampm.);

drop txt;

run;

CTorres

Contributor
Posts: 33

Re: Time Conversion

Thanks guys!  I'm sure all of them would work, but I got the last one to work first.  Thanks again!

Steve

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 293 views
  • 6 likes
  • 6 in conversation