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
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.