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)
@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.
@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)
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.
Partial code (UNTESTED)
hour_word = scan(duration,1,' '); first_char = anyalpha(hour_word); hour = substr(hour_word,1,first_char-1);
@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.
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;
@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.
Using a regular expression with prxposn could solve the problem; can't test it right now.
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:
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.