BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
apolitical
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

5 REPLIES 5
RickAster
Obsidian | Level 7

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

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
ballardw
Super User

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;
PGStats
Opal | Level 21

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
apolitical
Obsidian | Level 7

worked like a charm. thanks everyone!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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