Hello
I am struggling a bit making a table with average dates / average age. I can make tables with date formats, but I am not able to calculate average date / average age. If I try, I just get the unformatted output, which is "2819.92".
proc Means data=My.data;
var Date;
format date9.;
run;
I would have thought I could find other cases on this forum with same issue, but unfortunately not, so I hope someone can help
Instead of letting PROC MEANS print the output, create an output data set. Then you can choose the formatting:
proc means data=have noprint;
var date;
output out=want (keep=mean_date) mean=mean_date;
run;
proc print data=want;
var mean_date;
format mean_date date9.;
run;
Please provide some test data/required output. Mean of a date doesn't make sense, 2819.92 tells you that the mean does not end up as an integer. Numeric dates are number of days since the cuttoff, they can only be integer. Perhaps you want median rather than mean. Mean age should be alright though. Need some demonstration to show what you are doing / want.
@jbrau123 wrote:
Hello
I am struggling a bit making a table with average dates / average age. I can make tables with date formats, but I am not able to calculate average date / average age. If I try, I just get the unformatted output, which is "2819.92".
proc Means data=My.data;
var Date;
format date9.;
run;
I would have thought I could find other cases on this forum with same issue, but unfortunately not, so I hope someone can help
Proc means does that: statistics don't inherit a format from the variable used.
To see a date formatted value for the mean then 1) direct the output of proc means to a data set and 2) apply the format when displaying or using that statistic. The date formats will only display the integer portion of the numeric value as a date.
Caveat: if you later test the mean to a value such as : if data_mean = '10AUG2018'd you likely won't get matches as the decimal portion is in the mean and seldom matches an exact date. I use a literal here but the same warning applies if comparing to any date value.
I don't quite understand what you are suggesting. Could you provide coding?
Kind regards,
Jacob
Instead of letting PROC MEANS print the output, create an output data set. Then you can choose the formatting:
proc means data=have noprint;
var date;
output out=want (keep=mean_date) mean=mean_date;
run;
proc print data=want;
var mean_date;
format mean_date date9.;
run;
To calculate a mean age, you first need to calculate an age for each individual in your data set and then take the average of that variable in PROC MEANS.
Here's how to calculate someone's age.
https://blogs.sas.com/content/sasdummy/2011/07/12/computing-age-in-sas-9-3/
And then you can use PROC MEANS as shown to do the average of the age variable, which you've already shown.
@jbrau123 wrote:
Hello
I am struggling a bit making a table with average dates / average age. I can make tables with date formats, but I am not able to calculate average date / average age. If I try, I just get the unformatted output, which is "2819.92".
proc Means data=My.data;
var Date;
format date9.;
run;
I would have thought I could find other cases on this forum with same issue, but unfortunately not, so I hope someone can help
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.