Command reference

correlate — measure linear association on a common sample

Calculate Pearson correlations between numeric variables using the same complete observations for every coefficient. Use covariance when the original measurement units matter.

Syntax

correlate [varlist] [if condition] [in range] [, covariance]

Provide at least two numeric variables, or omit varlist to use all numeric variables. if and in first restrict the sample. The command then excludes any observation missing any listed numeric variable, so every matrix entry uses the same complete-case sample. corr is an alias.

Options

covariance
Display sample covariances instead of correlations, using the same complete-case sample and an N − 1 denominator. The diagonal contains sample variances; each off-diagonal covariance is expressed in the product of the two variables' units.

Example

The example correlates math, reading, and writing scores in 24 synthetic student records. It then correlates math and reading again, explicitly excluding the record with a missing writing score.

clear
import delimited "data/student_scores.csv"
correlate math read write
correlate math read if !missing(write)
Open example in browser

Sample data included. Opens in a new tab; click Run.

What to look for

Both commands use the same 23 records, so the math–reading coefficient is the same in both results, about 0.9805. It is positive and close to 1 because higher scores in one subject tend to occur alongside higher scores in the other in this constructed dataset. The second command makes the first command's complete-case selection explicit.