You can use the following basic syntax to calculate the quartiles for a dataset in SAS:
/*calculate quartile values for variable called var1*/
proc univariate data=original_data;
var var1;
output out=quartile_data
pctlpts = 25 50 75
pctlpre = Q_;
run;
Note: The pctlpts statement specifies which quartiles to calculate and the pctlpre statement specifies the prefix to use for the quartiles in the output.
The following example shows how to use this syntax in practice.
Example: How to Calculate Quartiles in SAS
Suppose we have the following dataset in SAS that contains two variables:
/*create dataset*/
data original_data;
input team $ points;
datalines;
A 12
A 15
A 16
A 21
A 22
A 25
A 29
A 31
B 16
B 22
B 25
B 29
B 30
B 31
B 33
B 38
;
run;
/*view dataset*/
proc print data=original_data;
The following code shows how to calculate the quartiles for the points variable in the dataset
/*calculate quartile values for points*/
proc univariate data=original_data;
var points;
output out=quartile_data
pctlpts = 25 50 75
pctlpre = Q_;
run;
/*view quartiles for points*/
proc print data=quartile_data;
Here’s how to interpret the output:
- The value of the first quartile is 18.5.
- The value of the second quartile is 25.
- The value of the third quartile is 30.5.
To calculate the quartile values grouped by the team variable, simply add by team in the proc univariate statement:
/*calculate quartile values for points*/
proc univariate data=original_data;
var points;
by team;
output out=quartile_data
pctlpts = 25 50 75
pctlpre = Q_;
run;
/*view quartiles for points*/
proc print data=quartile_data;
The output table shows the quartile values for the points variable for both teams A and B.
Additional Resources
The following tutorials explain how to perform other common tasks in SAS:
How to Calculate Percentiles in SAS
How to Use Proc Summary in SAS
How to Create Frequency Tables in SAS