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

I'm not sure how to find the differences in mean age between groups with standard deviation for a data file formatted in this layout.

 

Age   Group    Frequency

0         A             27 

0         B             42

1         A            109

1         B            22

2         A             65

2         B            72 

...

 

What I originally did was multiply frequency with age and totaled it, and divided by total frequency for that group, however, I'm not sure if there is a more efficient way. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @vuhandrew and welcome to the SAS Support Communities!


@vuhandrew wrote:

I'm not sure how to find the differences in mean age between groups with standard deviation for a data file formatted in this layout.

I think you're looking for two statistics which are part of PROC TTEST output (highlighted in red below).

data have;
input Age Group :$1. Frequency;
cards;
0 A 27
0 B 42
1 A 109
1 B 22
2 A 65
2 B 72
;

ods select statistics;
proc ttest data=have;
class group;
freq frequency;
var age;
run;

Result:

Variable:  Age

Group         Method              N        Mean     Std Dev     Std Err     Minimum     Maximum

A                               201      1.1891      0.6512      0.0459           0      2.0000
B                               136      1.2206      0.8919      0.0765           0      2.0000
Diff (1-2)    Pooled                    -0.0315      0.7574      0.0841
Diff (1-2)    Satterthwaite             -0.0315                  0.0892

Edit: Needless to say, you can write these statistics to a dataset (e.g. WORK.STATS) for further processing or simply to see more decimals. Just insert an ODS OUTPUT statement into or before the PROC TTEST step:

ods output statistics=stats;
proc print data=stats noobs label;
where method=:'P';
format _numeric_ best17.;
var mean stddev;
label mean='Difference in mean age (A - B)';
run;

Result:

    Difference in
 mean age (A - B)              Std Dev

-0.03153350892596     0.75744715265964

 

View solution in original post

3 REPLIES 3
FreelanceReinh
Jade | Level 19

Hello @vuhandrew and welcome to the SAS Support Communities!


@vuhandrew wrote:

I'm not sure how to find the differences in mean age between groups with standard deviation for a data file formatted in this layout.

I think you're looking for two statistics which are part of PROC TTEST output (highlighted in red below).

data have;
input Age Group :$1. Frequency;
cards;
0 A 27
0 B 42
1 A 109
1 B 22
2 A 65
2 B 72
;

ods select statistics;
proc ttest data=have;
class group;
freq frequency;
var age;
run;

Result:

Variable:  Age

Group         Method              N        Mean     Std Dev     Std Err     Minimum     Maximum

A                               201      1.1891      0.6512      0.0459           0      2.0000
B                               136      1.2206      0.8919      0.0765           0      2.0000
Diff (1-2)    Pooled                    -0.0315      0.7574      0.0841
Diff (1-2)    Satterthwaite             -0.0315                  0.0892

Edit: Needless to say, you can write these statistics to a dataset (e.g. WORK.STATS) for further processing or simply to see more decimals. Just insert an ODS OUTPUT statement into or before the PROC TTEST step:

ods output statistics=stats;
proc print data=stats noobs label;
where method=:'P';
format _numeric_ best17.;
var mean stddev;
label mean='Difference in mean age (A - B)';
run;

Result:

    Difference in
 mean age (A - B)              Std Dev

-0.03153350892596     0.75744715265964

 

vuhandrew
Calcite | Level 5

Thank you for your help! I really appreciate it.

ballardw
Super User

If you need simple summary statistics for each level of group variable(s) you can use one of the procedures such as Means or Summary with the count as Freq variable.

 

proc means data=have;
   class group;
   var age;
   freq frequency;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 3259 views
  • 1 like
  • 3 in conversation