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
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
RTFM on the
TIMEAMPMw.d Format
attrib MyTimeVar length = 8 format =timeampm5.2;
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;
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;
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;
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
Thanks guys! I'm sure all of them would work, but I got the last one to work first. Thanks again!
Steve
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.