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.
sasTime = hms(0, myMinutes, 0);
format sasTime hhmm.;
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;
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.