# How to Perform T-Test in SAS (with Examples)

In this tutorial, we will show how to perform t-test in SAS, along with examples.

SAS Code: One Sample T-Test
```proc ttest data=mydata
h0=65 /* Null hypothesis mean of 65 */
alpha=0.05;  /* Significance level of 0.05 */
var variable1; /* Specify the variable "variable1" for t-test */
run;
```
SAS Code: Two Sample T-Test
```proc ttest data=mydata alpha=0.05;
class grouping_variable; /* Grouping Variable */
var variable1;
run;
```
SAS Code: Paired T-Test
```proc ttest data=mydata;
paired before_variable * after_variable;
run;
```

## How to Perform One Sample T-Test in SAS

The one sample t-test is used to test whether the mean of the variable "height" is significantly different from a specified value of 65 inches. The null and alternative hypotheses for this t-test can be written as follows:

Null Hypothesis (H0): μ = 65

Alternative Hypothesis (H1): μ ≠ 65

The following SAS code performs a one-sample t-test on the variable "height" in the "sashelp.class" dataset. The significance level (alpha) is set to 0.05, which means the test will be conducted at a 5% significance level.

```proc ttest data=sashelp.class
h0=65 /* Null hypothesis mean of 65 */
alpha=0.05;  /* Significance level of 0.05 */
var height; /* Specify the variable to test */
run;
```

Conclusion: The p-value indicates the level of significance and whether the null hypothesis can be rejected or not. Since the p-value (0.0362) is less than the significance level (0.05 in this case), we reject the null hypothesis and conclude that there is a significant difference between the sample mean of "height" and the null hypothesis value of 65.

## How to Perform Two Sample T-Test in SAS

Here we are performing an independent two sample t-test to compare the mean height between males and females. The null and alternative hypotheses for this t-test can be written as follows:

Null Hypothesis (H0): μ_male = μ_female

Alternative Hypothesis (H1): μ_male ≠ μ_female

The following SAS code performs an independent sample t-test to compare the heights of two groups (males and females) represented by the "sex" variable in the "sashelp.class" dataset. The significance level (alpha) is set to 0.05, which means the test will be conducted at a 5% significance level.

```proc ttest data=sashelp.class alpha=0.05;
class sex /* Grouping Variable */;
var height;
run;
```

Assumption: The independent two-sample t-test assumes variance of the data in both groups should be approximately equal. It means that the spread of data points around the mean should be similar for both groups.

Equality of Variances table - Since the p-value of the Equality of Variances test (0.9527) is greater than the significance level (e.g., 0.05), then we assume that the variances are equal. In this case we will use Pooled method. If the p-value had been less than 0.05, we would have used the Satterthwaite method.

Conclusion: The p-value indicates the level of significance and whether the null hypothesis can be rejected or not. Since the p-value (0.1645) is greater than the significance level (0.05 in this case), we do not reject the null hypothesis and conclude that there is no significant difference between the sample mean of "height" between males and females.

## How to Perform Paired T-Test in SAS

A paired t-test is used to compare the means of two related groups or conditions, where each subject is measured twice, once under each condition. The purpose of the paired t-test is to determine whether there is a significant difference between the means of the two conditions. In simple words, a paired t-test is used to compare the measurements between the "before" and "after" conditions.

The null and alternative hypotheses for the paired t-test can be written as follows:

Null Hypothesis (H0): μ_before = μ_after

Alternative Hypothesis (H1): μ_before ≠ μ_after

The following SAS code performs paired t-test to compare the "before" and "after" measurements. The `paired` statement in PROC TTEST specifies that the observations are paired. The "before_measure" and "after_measure" variables are paired together for the analysis.

```data PairedData;
input subject_id before_measure after_measure;
datalines;
1 12 15
2 14 16
3 10 11
4 15 18
5 18 20
6 20 22
7 11 12
8 13 14
9 16 17
10 9 13
;
run;

proc ttest data=PairedData;
paired before_measure * after_measure;
run;
```

Conclusion: Since the p-value (0.0002) is less than 0.05, we can reject the null hypothesis, indicating that there is a significant difference between the "before" and "after" measurements.

Related Posts
Share 