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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 5197 views
  • 11 likes
  • 5 in conversation