BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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?
4 REPLIES 4
deleted_user
Not applicable
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
Peter_C
Rhodochrosite | Level 12
> 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
deleted_user
Not applicable
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
deleted_user
Not applicable
and 40 was a typo I wanted to say 82.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 832 views
  • 0 likes
  • 2 in conversation