I have the variable "Time" ( character text string) of minutes; I want to convert them into seconds ( numeric format). Unfortunately, I can not change the source data, and this is how it is written in the source.
I appreciate your help.
data have;
time = "08:00";
output;
time ="08:45";
output;
time = "27:38";
output;
time ="59:59";
output;
run;
Want
data have;
time = "08:00";
output;
time ="08:45";
output;
time = "27:38";
output;
time ="59:59";
output;
run;
data want;
set have;
time_dt=input(time,stimer.);
run;
proc print;run;
8 minutes have 480 seconds, not 48.
What is your real intended formula?
Sorry that’s a typo.
Then both suggested solutions will work.
Since SAS time values are counts of seconds, simply using INPUT will do it:
seconds = input('00:'!!time,time8.);
P.S. what is your "source data"? A SAS dataset or something else?
Shape your string into a form that's suitable for an informat.
data have;
time = "08:00";
output;
time ="08:45";
output;
time = "27:38";
output;
time ="59:59";
output;
run;
data want;
set have;
time_dt=input(cats('00:',time),time.);
run;
proc sql;
select
time
,time_dt format=best32.
,time_dt format=time.
from want
;
quit;
So you have strings in XX:XX style and you want to interpret that as MM:SS.
First let's make a dataset with some example data. Not need to write a lot of statements. Just use normal SAS code to read in the example values.
data have;
input time :$5.;
cards;
08:00
08:45
27:38
59:59
;
The TIME informat will assume a string in XX:XX style is HH:MM , not MM:SS.
So you have two choices. Prefix an extra 00: in front so the TIME informat knows you have strings in HH:MM:SS style.
Or just divide by 60 since there are 60 seconds in a minute.
Let's try both.
data want;
set have;
sastime=input(time,time5.);
format sastime time12.;
seconds1=sastime/60;
seconds2=input(cats('00:',time),time8.);
run;
Result:
Obs time sastime seconds1 seconds2 1 08:00 8:00:00 480 480 2 08:45 8:45:00 525 525 3 27:38 27:38:00 1658 1658 4 59:59 59:59:00 3599 3599
data have;
time = "08:00";
output;
time ="08:45";
output;
time = "27:38";
output;
time ="59:59";
output;
run;
data want;
set have;
time_dt=input(time,stimer.);
run;
proc print;run;
Thank you @Ksharp @Kurt_Bremser @Tom @Patrick . All of you are amazing. All your suggestions work. I am choosing one solution to close this thread. Thanks again guys.
One question that remains: are values beyond one hour possible, and how would they be represented?
No. All the time is less than one hour. So no possibility of getting the hours information in the data.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.