BookmarkSubscribeRSS Feed
Mike_Davis
Fluorite | Level 6


Hello everyone,

I have a time variable denotes 3662 seconds(3600+60+2),

I wonder how to convert the 3662 seconds to 1hour and 1minutes and 2 second (01:01:02) format.

Thanks

Mike

data have;

second=3662;

run;

data need;/*this need modification*/

set have;

time= input(second,hh:mm:ss.);

run;

7 REPLIES 7
Mike_Davis
Fluorite | Level 6

FYI,

data need;/*this need modification*/

set have;

time=put(second,time13.);

run;

PGStats
Opal | Level 21

Use hms function :

data have;
second=3662;
run;

data need;/*this need modification*/
set have;
format time time10.;
time= hms(0,0,second);
run;

proc print; run;

PG

PG
data_null__
Jade | Level 19

Could you have skipped that HMS function?

PGStats
Opal | Level 21

Yes, since SAS stores time values in units of seconds. But I prefer to remain independent of internal representations, even when they are documented. That's why I prefer to use INTNX("DAY', aDate, 1) instead of aDate+1 to find the day after aDate. Call me a purist!

PG

PG
mkeintz
PROC Star

If you need a character variable showing 01:01:02 Mike has provided the solution.  But you would need to keep the SECOND variable if you plan to do any time interval calculations or anlysis that goes beyond frequencies.  So you might simply apply the time8.0 format to SECOND for reporting purposes and avoid creating the new TIME variable entriely.

Then you could do things like:

proc freq data=have;

   tables second;

  format second time8.0;

run;

which would give you tables in whole second intervals, but reported in the hh:mm:ss format that you requested.  Or if you used time5.0 you'd get whole minute intervals (hh:mm).  Or time2.0 for hours.  But remember the TIME format is for a duration measure (not time-of-day), so it will display values of 24 hours and beyond.

Now if you want to see 25:01:01 reported as 01:01:01, then you can use the TOD8.0, TOD5.0, or TOD3.0 formats.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Mike_Davis
Fluorite | Level 6

Mike's method return character and PG's method keep numeric,

if we use call symput('ggg',time); in a datastep,

the character one will get atime formated  &ggg,while the numeric one will keep the original numeric value to the macro varialbe

mkeintz
PROC Star

If you want a macro var to get a formatted value then use the PUT function as the second argument of call symput:

    call symput('ggg',put(time,time8.0))

As a general rule, I avoid creating character vars based on numeric vars already in the data set, at least when those character values can be created by applying a format to the numeric value, and especially when the format is a standard SAS-supplied format.  It saves space and protects against a change in the numeric var not being propagated to the related character var.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 7 replies
  • 12857 views
  • 3 likes
  • 4 in conversation