Help using Base SAS procedures

Conversion minutes to HH:MM

Reply
New Contributor
Posts: 3

Conversion minutes to HH:MM

My data contains a variable time as minutes (for example, 121). After I calculate average time(PROC MEANS; Var time), I wonder how to convert the average of time to HH:MM(for example 2:01) in the output.

 

Respected Advisor
Posts: 4,649

Re: Conversion minutes to HH:MM

 

sasTime = hms(0, myMinutes, 0);
format sasTime hhmm.;
PG
New Contributor
Posts: 3

Re: Conversion minutes to HH:MM

Thanks! I applied your tip as in below to print out the mean sleep time by year, sex and age. Is this an efficient way to acquire mean sleep time by year sex and age?

 

Data test(keep=year sex age10 sleep);
  set timeuse;
run;
Proc means data=test ;
  class year sex age10;
  var sleep;
  output out=test1
  mean= mean(sleep)=MeanSleep;
run;

Data test2;
  set test1;

  AveSleep = hms(0, MeanSleep, 0);
  format Avesleep hhmm.;

run;
Proc print data=test2;
  var year sex age10 AveSleep;
run;

Super User
Posts: 10,500

Re: Conversion minutes to HH:MM

I would probably use:

Proc means data=timeuse;

insted unless you have a real good reason for the set test. Note that when you use Proc means (or Summary) the only variables that appear in the output are the class and by (if used) variables, the requested statistics and automatic variables _freq_ and _type_. So the Keep dataset option really doesn't come into play.

 

This code:

Proc means data=test ;
  class year sex age10;
  var sleep;
  output out=test1
  mean= mean(sleep)=MeanSleep;
run;

Is a bit redundant. Your output would have two variables, sleep and MeanSleep, with the same value. If you don't want the variable sleep with the value the mean remove the mean= bit on the output statement.

Also there would not be any problem with using:

MeanSleep= hms(0, MeanSleep, 0); unless you actually need a value in minutes and another in seconds (which is what the HMS conversion does.

I would likely add a label to indicate that the value is actually a duration or interval and not a time of day in case you don't use the data set for awhile and forget.

Something like:

Label AveSleep = "Average sleep duration";

It is a good idea to post code in a code box opened with the forum {i} icon in the menu bar. The main message boxes can reformat your code and sometimes make it hard to read. Also sometimes html artifacts appear in code when copying from teh main window which can cause errors for people running example code to test.

Ask a Question
Discussion stats
  • 3 replies
  • 134 views
  • 1 like
  • 3 in conversation