BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jbrau123
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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;

View solution in original post

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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. 

ballardw
Super User

@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.

jbrau123
Obsidian | Level 7

I don't quite understand what you are suggesting. Could you provide coding?

 

Kind regards,

Jacob

Astounding
PROC Star

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;

Reeza
Super User

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


 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 1756 views
  • 1 like
  • 5 in conversation