• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / SAS / Using SAS to Find Mean by Group with PROC MEANS

Using SAS to Find Mean by Group with PROC MEANS

September 29, 2022 Leave a Comment

To find the mean by group in SAS, you can use PROC MEANS and specify by group in the CLASS statement. You can find the mean of multiple variables and group by multiple groups with PROC MEANS.

data example;
input group $ value;
datalines;
A 1
A 2
A 3
B 4
B 5
B 6
C 7
C 8
;
run;

proc means data=example mean;
    class group;
    variable value;
run;

mean by group sas

You can also output this dataset with the OUTPUT statement.

data example;
input group $ value;
datalines;
A 1
A 2
A 3
B 4
B 5
B 6
C 7
C 8
;
run;

proc means data=example mean;
    class group;
    variable value;
    output out=mean_by_group mean(value)=;
run;

/* Output */
group _TYPE_ _FREQ_ value
           0      8   4.5
A          1      3     2
B          1      3     5
C          1      2   7.5

Use the NWAY option if you only want to create a dataset with the means by group (instead of the entire dataset, for example).

data example;
input group $ value;
datalines;
A 1
A 2
A 3
B 4
B 5
B 6
C 7
C 8
;
run;

proc means data=example mean nway;
    class group;
    variable value;
    output out=mean_by_group mean(value)=;
run;

/* Output */
group _TYPE_ _FREQ_ value
A          1      3     2
B          1      3     5
C          1      2   7.5

When working with data, the ability to summarize and aggregate your data in different ways is very valuable.

One such case is if you want to find the mean of certain variables of your data.

In SAS, PROC MEANS is a procedure which allows you to create summaries of your data and allows you to calculate things like the mean, mean, min, max, etc. of a variable.

You can use PROC MEANS to find the mean of variables by group using the CLASS statement.

Below is a simple example of how you can use PROC MEANS to find means by group in SAS.

data example;
input group $ value;
datalines;
A 1
A 2
A 3
B 4
B 5
B 6
C 7
C 8
;
run;

proc means data=example mean;
    class group;
    variable value;
run;

mean by group sas

How to Find Mean of Variable By Multiple Groups in SAS with PROC MEANS

If you want to find the mean of a variable by multiple groups in SAS, you can add variables to the PROC MEANS CLASS statement.

Below shows you a simple example of calculating the mean across multiple groups in SAS with PROC MEANS.

data example;
input group1 $ group2 $ value;
datalines;
A D 1
A D 2
A E 3
B E 4
B E 5
B F 6
C F 7
C F 8
;
run;

proc means data=example mean;
    class group1 group2;
    variable value;
run;

mean by multiple group sas

How to Find Mean of Multiple Variables by Group in SAS with PROC MEANS

If you want to get the mean of multiple values by group in SAS, you can add variables to the PROC MEANS VARIABLE statement.

Below shows you a simple example of finding the mean of multiple variables by group in SAS with PROC MEANS.

data example;
input group $ value1 value2;
datalines;
A 1 2
A 2 3
A 3 4
B 4 5
B 5 6
B 6 7
C 7 8
C 8 9
;
run;

proc means data=example mean;
    class group;
    variable value1 value2;
run;

mean multiple variables by group

Hopefully this article has been useful for you to learn how to find the mean of variables by groups using PROC MEANS in SAS.

Other Articles You'll Also Like:

  • 1.  SAS Less Than or Equal to with LE or <=
  • 2.  SAS min() Function – Find Minimum Value Across Columns in Data Step
  • 3.  SAS Dollar Format – Formatting Numbers as Dollars in SAS Dataset
  • 4.  SAS call symput – Create Macro Variables in Data Step
  • 5.  SAS Not Equal – Check if a Variable is Not Equal to Another in Data Step
  • 6.  =">SAS Greater Than or Equal to with GE or >=
  • 7.  Concatenating Strings in a SAS Data Step
  • 8.  SAS sum() Function – Find Sum Across Columns in Data Step
  • 9.  SAS Proper Case – Capitalize First Letter of Word with propcase Function
  • 10.  SAS _n_ – How to Use the Automatic Variable _n_ in a Data Step

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x