I have a character variable specifying a duration of time by the number of hours, minutes and seconds. It looks something like"xh ymin. zsec.", where x, y and z can be single or double digits, for example "14h 20min. 2sec.", or "0h 5min. 50sec.". There is a dot after min and sec, but not after h. How can I create 3 numeric variables, hour, minute and second, filled by values of x, y and z? Thanks.
Similar but remove unwanted and scan is slightly less typing.
data junk;
string = "14h 20min. 2sec. ";
string = compress(string,"." ,'A');
hour = input(scan(string,1),best2.);
Minute = input(scan(string,2),best2.);
Second = input(scan(string,3),best2.);
run;
If you think of the markers h, min., etc. as delimiters, you can use the FIND function to locate them, then the SUBSTRN function to pick out the text values between them. Here I used the INPUT function to convert the text values to numeric values to assign to the result variables.
data _null_;
do string = "14h 20min. 2sec. ", "0h 5min. 50sec. ";
dlmat1 = find(string, "h ");
dlmat2 = find(string, "min. ");
dlmat3 = find(string, "sec.");
if dlmat1 > 0 and dlmat2 > 0 and dlmat3 > 0 then do;
hour = input(substrn(string, 1, dlmat1 - 1), ?? f8.);
minute = input(substrn(string, dlmat1 + 2, dlmat2 - dlmat1 - 2), ?? f8.);
second = input(substrn(string, dlmat2 + 5, dlmat3 - dlmat2 - 5), ?? f8.);
put (hour minute second) (=);
end;
end;
run;
hour=14 minute=20 second=2
hour=0 minute=5 second=50
Hi,
data tmp; a="14h 20min. 2sec."; b=tranwrd(tranwrd(tranwrd(a,"h ",":"),"min. ",":"),"sec.",""); c=input(b,time.);
h=hour(c);
m=min(c); format c time.; run;
Similar but remove unwanted and scan is slightly less typing.
data junk;
string = "14h 20min. 2sec. ";
string = compress(string,"." ,'A');
hour = input(scan(string,1),best2.);
Minute = input(scan(string,2),best2.);
Second = input(scan(string,3),best2.);
run;
Perfect case for regular expressions matching:
data have;
do timeStr = "14h 20min. 2sec.", "0h 5min. 50sec.";
output;
end;
run;
data want;
if not prx1 then prx1 + prxParse("/(\d{1,2})h\s(\d{1,2})min\.\s(\d{1,2})sec\./");
set have;
if prxMatch(prx1, timeStr) then do;
hour = input(prxPosn(prx1,1,timeStr), best.);
minute = input(prxPosn(prx1,2,timeStr), best.);
second = input(prxPosn(prx1,3,timeStr), best.);
output;
end;
drop prx1;
run;
worked like a charm. thanks everyone!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: