Parsing problem

Accepted Solution Solved
Reply
Contributor
Posts: 73
Accepted Solution

Parsing problem

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.


Accepted Solutions
Solution
‎08-09-2016 02:42 PM
Super User
Posts: 11,130

Re: Parsing problem

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;

View solution in original post


All Replies
Contributor
Posts: 22

Re: Parsing problem

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

 

Super User
Super User
Posts: 7,720

Re: Parsing problem

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;
Solution
‎08-09-2016 02:42 PM
Super User
Posts: 11,130

Re: Parsing problem

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;
Respected Advisor
Posts: 4,816

Re: Parsing problem

[ Edited ]

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;
PG
Contributor
Posts: 73

Re: Parsing problem

worked like a charm. thanks everyone!

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 316 views
  • 0 likes
  • 5 in conversation