DATA Step, Macro, Functions and more

Convert HHMM8. to Days

Reply
N/A
Posts: 0

Convert HHMM8. to Days

Hi,

I need to represent HHMM8. in days e.g. convert 1958hrs and 56minutes to days i.e 40:12, what is the best way to represent it FORMAT?

When I tried to make a dataset like below, it reads it as 32:39

data test;
FORMAT t1 HHMM8. ;
INPUT t1 :stimer.;
datalines;
1958:56
;
run;

What's wrong here?
N/A
Posts: 0

Re: Convert HHMM8. to Days

Hello,

The stimer informats, whe a single colon is present, considers the value before as number of minutes and the value after number of seconds. so in your case, what you are actually reading with stimer informat is 1958minutes and 56seconds.

TIME informat is better for your data.

Marius
Valued Guide
Posts: 2,175

Re: Convert HHMM8. to Days

> Hi,
>
> I need to represent HHMM8. in days e.g. convert
> 1958hrs and 56minutes to days i.e 40:12, what is the
> best way to represent it FORMAT?
>
> When I tried to make a dataset like below, it reads
> it as 32:39
>
> data test;
> FORMAT t1 HHMM8. ;
> INPUT t1 :stimer.;
> datalines;
> 958:56
> ;
> run;
>
> What's wrong here?

when you have a look at informat STIMER documentation ( http://support.sas.com/documentation/cdl/en/lrdict/63026/HTML/default/viewer.htm#a002295695.htm ) I think you will realise you chose the wrong informat!
It appears to be the only built-in informat to accept that string, but treats a string with just one ':' as minutes:seconds (as I read the doc).
There is a work-around which may or not work for you[pre]1784 data test;
1785 FORMAT t1 HHMM8. ;
1786 input dataline $char10. @1 @ ;
1787 * add :00 as seconds, to the end of the infile buffer ;
1788 substr( _infile_, length(_infile_)+1) = ':00' ;
1789 INPUT t1 :stimer. ;
1790 put t1= dataline= ;
1791 datalines;

t1=1958:56 dataline=1958:56
NOTE: The data set WORK.TEST[/pre]I added the dataline variable just for demonstration.

However, I don't see how that relates to 40 days and 12 hours?
There isn't a built-in format for days and hours, but I can construct one[pre]1795 proc format ;
1796 picture my_d_h(round) other = '%j:%H'( datatype=datetime) ;
NOTE: Format MY_D_H has been output.
1797 run ;

NOTE: PROCEDURE FORMAT used[/pre]This is OK for up to a year because the "%j" selects the number of days in the year, and your value T1 would hold a number of seconds. The kind of value in T1 can be treated as a datetime value. These start at the beginning of 1960. The datepart() of T1 is the number of days in the time/duration T1. We can use the %j directive for picture strings in PROC FORMAT (for a description of these directives, see: http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/a002473467.htm#a000530223 ) to get the number of days in your T1 duration - as long as it is less than or equal to the number of days in 1960!
When I use my custom format it does not produce the answer you suggested 40:12[pre]1798 %put %sysfunc( inputn( 1958:56:0, stimer), my_d_h5 );
82:14
[/pre]That 80:14 is reasonable!

With 24 hours in a day, 40 days would be 960 hours and 80 days= 1920 hours, which is much closer to your 1958:56!

I look forward to some clarification!
peterC
N/A
Posts: 0

Re: Convert HHMM8. to Days

Hey thanks guys for such a quick response.

1.> @Maurius- Indeed I forgot seconds

2.> @Peter gr8 solution with Proc Format %j

but I did this instead t1=input(t1,8.1)/86400, which gave me exactly 82.6
N/A
Posts: 0

Re: Convert HHMM8. to Days

and 40 was a typo I wanted to say 82.
Ask a Question
Discussion stats
  • 4 replies
  • 204 views
  • 0 likes
  • 2 in conversation