Count the number of times a particular value occurred in a variable of a dataset


This post is part of 'SAS | Proc freq' series

When working with data, we frequently want to see how many number of times a particular value occurred in a variable across observations in the dataset.

We need programming features to achieve this. There are multiple ways in which you can get this result in SAS.

In this post, we will see how to get the result using frequency procedure (proc freq).


Let us assume that we have the dataset which has information for 19 students of a class. 

Name

Sex

Age

Height

Weight

Alfred

M

14

69

112.5

Alice

F

13

56.5

84

Barbara

F

13

65.3

98

Carol

F

14

62.8

102.5

Henry

M

14

63.5

102.5

James

M

12

57.3

83

Jane

F

12

59.8

84.5

Janet

F

15

62.5

112.5

Jeffrey

M

13

62.5

84

John

M

12

59

99.5

Joyce

F

11

51.3

50.5

Judy

F

14

64.3

90

Louise

F

12

56.3

77

Mary

F

15

66.5

112

Philip

M

16

72

150

Robert

M

12

64.8

128

Ronald

M

15

67

133

Thomas

M

11

57.5

85

William

M

15

66.5

112


Let us assume that we want to count the number of males and females in this data. The male/female information is stored in a variable named sex in this data.

To get the result, we can use the below sas code.


proc freq data=class;

   tables sex;

run;

  • We are instructing SAS to run frequency procedure
  • We are specifying that proc freq has to use a dataset named class by using data= option on proc freq statement
  • With the tables statement, we are instructing proc freq to produce a frequency table for the sex variable

When the code is executed, we get the output below.

  • The output is produced in the results window or output window
  • We get one row for each unique value present in the sex variable
  • Frequency column contains the number of rows with the value displayed in the sex column on that row
  • Additionally, we get the percent, cumulative frequency and cumulative percent in the output

Most of the time, we need to store this output in a dataset so that we can reuse it in downstream processing. We can achieve this by using the code below.

proc freq data=class;

   tables sex/out=counts;

run;

  • out= option of tables statement is used to specify the name of the output dataset




Filter a category
AllSASnRSDTMSASDomainADaMStatistics

List of other posts


Domain


General
What are CDISC standards?
What is a clinical development plan?
What is a clinical study report?
What is a clinical trial registry?
What is the importance of baseline characteristics in a clinical trial?
Drug discovery
Drug Development
Preclinical Research
The Investigational New Drug Process
What is ICH?
Efficacy data vs Safety data
What is a clinical trial?
Why are clinical trials conducted?
What is a clinical trial protocol?
Who conducts clinical trials?
Inclusion/Exclusion Criteria in a clinical trial
What are the phases of clinical trials?
What happens after a clinical trial is completed?
FDA Drug Review
Key steps in developing new drugs and biologicals

Trial design aspects
What is a crossover clinical trial?

Terminology
What is a cohort?

SAS


Definitions
What is a computer?
What happens when we execute a SAS program?
What is software?
What is SAS?
What is data?
What is data entry?
What is data retrieval?
What is data management?
What is "Report"?
What is statistics?
What is Statistical Analysis?
How do we use SAS?
What kind of questions can SAS help us answer?
How do we provide instructions to SAS?
What is a SAS program?
What does a SAS program contain?

General
Attributes of a SAS dataset
Rules for SAS dataset names
Rules for SAS variable names
Rules for SAS library names
Rules for character SAS format names
Reserved SAS dataset names
Rules for numeric SAS format names
What can SAS dataset options do?
Attributes of a SAS variable
Automatic conversion of data types in SAS
How does SAS expect our data to be organized?
Introduction to SAS interface
By groups in SAS

Informats
Rules for character SAS informat names

Proc freq
Count the number of times a particular value occurred in a variable of a dataset

Proc contents
Check the list of variables in a SAS dataset

Proc datasets
Delete all sas datasets from a library
Delete specific sas datasets from a library
Save specific sas datasets (and delete others) of a library
Rename SAS datasets using proc datasets change statement

Log issues
WARNING: No matching members in directory.

One-line definitions
What is a SAS library?
What is a libref?
What is an input statement?
What is infile statement?
What is set statement?
What is length statement?

SDTM


General
How to derive baseline flag in SDTM
How to create SEQ variable in SDTM
New domains in SDTM IG 3.3
What is a codelist?
How to derive study day variable in SDTM
What is SDTM?
Why do we need SDTM?
How to convert original results to standard results using conversion factors

Demographics
What information does SDTM.DM (Demographics) contain?

Adverse Events
What is causality assessment?
What information does SDTM.AE (Adverse Events) contain?

Disposition
What information does SDTM.DS (Disposition) contain?

Concomitant Medications
What information does SDTM.CM (Concomitant/Prior Medications) contain?

Procedures
What information does SDTM.PR (Procedures) contain?

ADaM


General
What is ADaM?

ADSL
What is ADSL as per ADaM standard?

BDS
What is BDS as per ADaM standard?

Statistics


General
Alpha and beta errors
What is correlation?

SASnR


Introduction
What is R?
What is an R package?
What is tidyverse?
What are the core packages of tidyverse?
What is haven package of tidyverse?
How to install tidyverse?
How to load core tidyverse packages into the R session?

Reading data
Import/Read SAS dataset into R

Creating sample data
How to create some sample data in SAS and R tidyverse

Subset variables (columns)
How to select only required variables/columns in SAS and R tidyverse?
How to drop unwanted variables/columns in SAS and R tidyverse?

Subset observations (rows)
How to select/subset required rows in SAS and R tidyverse

Appending data
Append two datasets in SAS and R tidyverse

Merging/joining data
Merge/full join two datasets in SAS and R tidyverse
Merge/inner join two datasets in SAS and R tidyverse
Merge/left join two datasets in SAS and R tidyverse

Sort (order) observations
Sort/order observations based on the values in a single variable in SAS and R tidyverse

Transpose/Restructure data
Restructure/transpose long data to wide data
Restructure/transpose wide data to long data

Obtain frequencies
Obtain frequencies/counts based on one variable - one-way frequencies in SAS and R tidyverse
Obtain frequencies/counts based on two variables - two-way frequencies in SAS and R tidyverse

Descriptive statistics
Descriptive statistics for a numeric variable using SAS and R tidyverse