Date: Wed, 23 Jul 2008 22:41:13 -0400
Reply-To: "Howard Schreier <hs AT dc-sug DOT org>"
<schreier.junk.mail@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Howard Schreier <hs AT dc-sug DOT org>"
<schreier.junk.mail@GMAIL.COM>
Subject: Re: Time calculation
On Wed, 23 Jul 2008 15:11:12 -0400, DP <adsingh78@GMAIL.COM> wrote:
>Hi all,
>
>What would be the best way to calculate minutes from a character variable.
>Not sure if "dhms" can be useful in this case.
>
>Given:
>VAR1
>1:26
>2:01
>3:24
>4:05
>
>Want:
>VAR1 Minutes
>1:26 86
>2:01 121
>3:24 204
>4:05 245
>
>Thanks!
>DP
There are indeed several approaches and numerous variations on each. I'll
suggest one more, plus a basically different strategy.
Expanded test data:
data given;
input VAR1 $6. ;
cards;
1:26
2:01
3:24
4:05
166:39
166:40
;
Solution:
data want;
set given;
Minutes = intck( 'minute' , '00:00't , input(var1,time6.) );
SAS_TIme = input(var1,time6.);
run;
Notice that this formula for MINUTES does not require or reflect any
knowledge of the SAS internals for storing and manipulating time values. It
just uses time functions, informats, and literals (one of each, actually).
The other assignment statement computes the time in accordance with the SAS
time convention. It's usually a good idea to carry that variable, as we'll see.
The alternate strategy is to use the time value with an appropriate format
*instead* of the derived number of minutes. This works for some purposes but
not others, so I don't know if it is appropriate here.
The format to use is MMSS4. Example:
proc print data=want;
format SAS_TIme mmss4.;
run;
Result:
VAR1 Minutes SAS_TIme
1:26 86 86
2:01 121 121
3:24 204 204
4:05 245 245
166:39 9999 9999
166:40 10000 **
The behavior of the MMSS format causes this technique to be limited to
values less than 166:40. If that's not a problem you may be able to get skip
computing MINUTES and get rid of VAR1 after computing SAS_Time.