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

Hi ,

 

 

CandidateDuration
Arun0hr 0min 44sec.
Arun0hr 0min 9sec.
Arun12hr 4min 10sec.
Vishal1hr 0min 24sec.
Vishal0hr 0min 35sec.
Vishal0hr 7min 29sec.
Vishal2hr 1min 9sec.


A) I want to find the total sum of duration candidate wise. 

B) How to covert duration to Time format (eg: 1:04:10)





1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

@AgentA wrote:

I was doing this but couldn't get through:

data d(keep=username duration time);
set a;
time=substr(duration,1,1)||':'||substr(duration,4,2)||':'||compress(substr(duration,10,2),'','kd');
run;



I'm not sure what that's trying to do. 

 

To convert to time you need to extract the time components and use them in the HMS function. 

Use SCAN to extract the specific component and COMPRESS to keep only digits. 

 

First_part = scan(old_time, 1);

first_char = compress(first_part, , 'kd');

first_digit = input(first_char, 8.);

 

Repeat for minutes and seconds. Then pass each component to HMS. 

View solution in original post

13 REPLIES 13
PaigeMiller
Diamond | Level 26

@AgentA wrote:

Hi ,

 

 

Candidate Duration
Arun 0hr 0min 44sec.
Arun 0hr 0min 9sec.
Arun 12hr 4min 10sec.
Vishal 1hr 0min 24sec.
Vishal 0hr 0min 35sec.
Vishal 0hr 7min 29sec.
Vishal 2hr 1min 9sec.


A) I want to find the total sum of duration candidate wise. 

B) How to covert duration to Time format (eg: 1:04:10)






You will have to parse the strings and then apply the HMS function to get the results into a SAS time value

 

The ANYALPHA or NOTALPHA function can help you determine where the numbers end and the alphabetic characters begin in each "word" (a "word" being a string of text separated by a space)

 

 

--
Paige Miller
AgentA
Obsidian | Level 7
code please
Reeza
Super User

1. Proc means

2. What variable type is it now? Use INPUT() to convert and Apply a format with the FORMAT statement. Didn't we do this on your last question for a date variable? 

 

FYI You forgot to include your data as a datastep this time. 

 

AgentA
Obsidian | Level 7
2. The data type is character. Kindly provide code for data step .
PaigeMiller
Diamond | Level 26

Partial code (UNTESTED)

 

hour_word = scan(duration,1,' ');
first_char = anyalpha(hour_word);
hour = substr(hour_word,1,first_char-1);
--
Paige Miller
Reeza
Super User

@AgentA wrote:
2. The data type is character. Kindly provide code for data step .

That's not how this works. Please try something, we aren't here to do your work. If you post what you've tried, we'll be more than happy to help correct or guide you in the right direction. 

 

If you must have code, hire a consultant. 

AgentA
Obsidian | Level 7

I was doing this but couldn't get through:

data d(keep=username duration time);
set a;
time=substr(duration,1,1)||':'||substr(duration,4,2)||':'||compress(substr(duration,10,2),'','kd');
run;


Reeza
Super User

@AgentA wrote:

I was doing this but couldn't get through:

data d(keep=username duration time);
set a;
time=substr(duration,1,1)||':'||substr(duration,4,2)||':'||compress(substr(duration,10,2),'','kd');
run;



I'm not sure what that's trying to do. 

 

To convert to time you need to extract the time components and use them in the HMS function. 

Use SCAN to extract the specific component and COMPRESS to keep only digits. 

 

First_part = scan(old_time, 1);

first_char = compress(first_part, , 'kd');

first_digit = input(first_char, 8.);

 

Repeat for minutes and seconds. Then pass each component to HMS. 

andreas_lds
Jade | Level 19

Using a regular expression with prxposn could solve the problem; can't test it right now.

Patrick
Opal | Level 21

@AgentA

Here one way to go.

proc format;
   invalue $toTimeString(default=16) 
    's/^[^\d]*(\d+)[^\d]+(\d+)[^\d]+(\d+)[^\d]*$/\1:\2:\3/o' (regexpe) = _same_ 
    other=_same_
    ;
run;

data test;
  format t time10.;
  timeString='0hr 0min 44sec';
  t=input(input(timeString,$toTimeString.),? time.);
run;

And before you ask here the links to the docu:

https://support.sas.com/documentation/cdl/en/proc/70377/HTML/default/viewer.htm#n1jriq5xib5j45n1pwpw...

http://support.sas.com/documentation/cdl/en/lefunctionsref/69762/HTML/default/viewer.htm#p0s9ilagexm...

 

Ksharp
Super User
data have;
infile cards expandtabs truncover;
input Candidate	 $ Duration $40.;
cards;
Arun	0hr 0min 44sec.
Arun	0hr 0min 9sec.
Arun	12hr 4min 10sec.
Vishal	1hr 0min 24sec.
Vishal	0hr 0min 35sec.
Vishal	0hr 7min 29sec.
Vishal	2hr 1min 9sec.
;
run;

data want;
 set have;
 time=hms(input(scan(duration,1,,'kd'),best.),
 input(scan(duration,2,,'kd'),best.),
 input(scan(duration,3,,'kd'),best.));
format time tod10.;
run;
AgentA
Obsidian | Level 7
Thank you so much

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
  • 13 replies
  • 1475 views
  • 6 likes
  • 7 in conversation