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

This is the question:

Z is a character variable whose value takes the form "x hr y min" for different integers x and y. Create a numeric variable X5 storing the corresponding number of minutes. For example if Z is "2 hr 25 min", then X5 should be 145.

I have had problems when time was displayed with : (ex: 9:30:30) or AM/PM (9:30 AM), but I never encountered this kind. How do I approach such problem? T

1 ACCEPTED SOLUTION

Accepted Solutions
pradeepalankar
Obsidian | Level 7

data have;

input time $50.;

if countw(time,': ')>=3 then

num=input(scan(time,1,': '),2.)*60+input(scan(time,3,': '),2.);

else

num=input(scan(time,1,': '),2.)*60+input(scan(time,2,': '),2.);

cards;

2 hr 25 min

1:30:30

9:30:30

9:30

00:12

1 hr 10 min

;

run;

output:

        Obs    time           num

         1     2 hr 25 min    145

         2     1:30:30         90

         3     9:30:30        570

         4     9:30           570

         5     00:12           12

         6     1 hr 10 min     70

View solution in original post

8 REPLIES 8
AncaTilea
Pyrite | Level 9

Hi.

Here is one poorly written way:

data in;

Z =  "2 hr 25 min";

hr = input(scan(z, 1,"hr"), 8.);

min = input(

            tranwrd(

                    reverse(

                            scan(

                                reverse(z), 1,"rh")

                            ),

                    "min",""),

             8.);

/* "nim 52 rh 2"*/

/* 25 min*/

             time_in_Min = hr*60+min;

run;

Smiley Happy

Takumi
Calcite | Level 5

Thank :smileygrin:

Linlin
Lapis Lazuli | Level 10

data have;

time='2 hr 25 min';

num=input(scan(time,1),2.)*60+input(scan(time,3),2.);

proc print;run;

Takumi
Calcite | Level 5

Thanks :smileygrin:

DBailey
Lapis Lazuli | Level 10

data have;

input x txt1 $ y txt2 $;

z=x*60 + y;

cards;

2 hr 25 min

;

run;

Takumi
Calcite | Level 5

Thanks :smileygrin:

pradeepalankar
Obsidian | Level 7

data have;

input time $50.;

if countw(time,': ')>=3 then

num=input(scan(time,1,': '),2.)*60+input(scan(time,3,': '),2.);

else

num=input(scan(time,1,': '),2.)*60+input(scan(time,2,': '),2.);

cards;

2 hr 25 min

1:30:30

9:30:30

9:30

00:12

1 hr 10 min

;

run;

output:

        Obs    time           num

         1     2 hr 25 min    145

         2     1:30:30         90

         3     9:30:30        570

         4     9:30           570

         5     00:12           12

         6     1 hr 10 min     70

pradeepalankar
Obsidian | Level 7

if you got ur answer then mark it as answered Smiley Happy

SAS Innovate 2025: Register Now

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!

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
  • 8 replies
  • 5926 views
  • 11 likes
  • 5 in conversation