Table of Contents
SAS, also known as Statistical Analysis System, is a powerful tool for data analysis and management. One of its useful features is the ability to count the number of observations in a dataset by group. This can be achieved by using the PROC FREQ procedure, which allows users to specify the variables and groups they want to count. By utilizing this feature, users can efficiently analyze and understand their data by obtaining a count of observations for each group, providing valuable insights for decision-making and further analysis. With its user-friendly interface and extensive documentation, SAS makes it easy for users to perform this task and gain a better understanding of their data.
Count Observations by Group in SAS
You can use the following methods to count the total observations by group in SAS:
Method 1: Count Observations by One Group
proc sql;
select var1, count(*) as total_count
from my_data
group by var1;
quit;
Method 2: Count Observations by Multiple Groups
proc sql;
select var1, var2, count(*) as total_count
from my_data
group by var1, var2;
quit;The following examples show how to use each method with the following dataset in SAS:
/*create dataset*/
data my_data;
input team $ position $ points;
datalines;
A Guard 15
A Guard 12
A Guard 29
A Forward 13
A Forward 9
A Forward 16
B Guard 25
B Guard 20
C Guard 34
C Forward 19
C Forward 3
C Forward 8
;
run;
/*view dataset*/
proc printdata=my_data;
Example 1: Count Observations by One Group
The following code shows how to count the total number of observations by team:
/*count observations by team*/
proc sql;
select team, count(*) as total_count
from my_data
group by team;
quit;

From the output we can see that team A contains 6 observations, team B contains 2 observations, and team C contains 4 observations.
Example 2: Count Observations by Multiple Groups
The following code shows how to count the total number of observations, grouped by team and position:
/*count observations by team and position*/
proc sql;
select team, position, count(*) as total_count
from my_data
group by team, position;
quit;
- A total of 3 players belong on team A and have a position of Forward.
- A total of 3 players belong on team A and have a position of Guard.
- A total of 2 players belong on team B and have a position of Guard.
- A total of 3 players belong on team C and have a position of Forward.
- A total of 1 player belongs on team A and has a position of Guard.
Additional Resources
The following tutorials explain how to perform other common tasks in SAS:
Cite this article
stats writer (2024). “How can I use SAS to count the observations by group in my dataset?”. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-sas-to-count-the-observations-by-group-in-my-dataset/
stats writer. "“How can I use SAS to count the observations by group in my dataset?”." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-sas-to-count-the-observations-by-group-in-my-dataset/.
stats writer. "“How can I use SAS to count the observations by group in my dataset?”." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-sas-to-count-the-observations-by-group-in-my-dataset/.
stats writer (2024) '“How can I use SAS to count the observations by group in my dataset?”', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-sas-to-count-the-observations-by-group-in-my-dataset/.
[1] stats writer, "“How can I use SAS to count the observations by group in my dataset?”," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.
stats writer. “How can I use SAS to count the observations by group in my dataset?”. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
