**************************************
learning.sas - example program for lab 1
For online SAS help go to
http://support.sas.com/documentation/cdl/en/statug/63962/HTML/default/viewer.htm#titlepage.htm
**************************************;
* Comments in SAS programs can be made in either of two ways:
1) by starting them with a slash-star and ending them with a star-slash
or
2) by starting them with a * and ending them with a semi-colon.
Illustrations: ;
/* a comment
of type 1 */
* a comment
of type 2 ;
options nodate pageno=1 formdlim='*';
/*the formdlim='*' option above separates output
pages by *'s rather than hard page breaks.
This conserves paper when printing*/
* ods stands for "output delivery system". ods statements allow the user to control
what kind of output SAS generates and where that output is sent. E.g., graphical output
can be turned on (which is the purpose of the first statement below this comment) or off,
output can be sent to the output window in the SAS GUI, it can be sent to html files,
pdf files, etc. In addition, there are ods statements that can be used within a given
procedure to exclude certain output that would have been included by default or to
send certain output to a SAS data set for additional analysis.;
ods graphics on;
ods listing gpath="c:\temp"; * send output to the output window;
* specify the gpath to a directory in which you want graphics files stored.
If this is unspecified, the files may or may not go to a directory to which
you have write access. If not, then they will not be written correctly and
also will not appear in your pdf or html output file. The choice c:\temp
will work, but you can specify a directory on your flash drive, say, instead;
* but, in addition, you can also send it to a pdf file by uncommenting the
following line and modifying it by specifying the path and filename that you want
to use for that output;
* ods pdf file="mypath\myfile.pdf";
data tests;
input id pretest posttest;
diff=posttest-pretest; * compute the d_i's ;
cards;
1 80 82
2 60 69
3 65 75
4 98 99
5 40 60
6 73 71
7 88 100
8 37 60
9 52 65
10 79 86
11 70 95
12 84 71
13 91 95
14 78 83
15 59 62
;
run;
title 'Learning Data';
proc print data=tests;
run;
title 'Paired t-test done using PROC MEANS';
proc means data=tests mean n stderr t prt; /* prt gives the two-sided p-value */
var diff;
run;
* Everything in this program below this point is instructive (please read it!) but
not essential to the problem. ;
/* For illustration, here is another way to get the p-value */
data junk;
*The function probt called as probt(tvalue,df) gives the cumulative probability
that a random variable that has a t distribution on df degrees of freedom will be less than
or equal to tvalue. I.e., it gives the area under the probability density function of the t(df)
curve to he left of tvalue. Similarly, there are functions such as probf, probnorm, etc
that give cumulative probabilities for the F distribution, standard normal distribution, etc.;
p1=(1-probt(3.0937057,14)); /* p1 will be the one-sided p-value */
p2=2*p1; /* p2 will be the two-sided p-value */
* Sometimes we want critical values rather than p-values. That is, instead of wanting to know the
probabiliy under the curve to the left of a given value, we may want to know the value such that
the probability under the curve is a certain given value (.05 or .025 say). That is, we may want
to find a certain percentile of the t, F or other distribution. The functions tinv, finv, etc.
do these calculations. E.g., tinv(prob,df) returns the 100*prob percentile of the t distribution
on df degrees of freedom. ;
t1=tinv(1-p1,14); *should give 3.0937057 ;
F2=finv(1-p2,1,14); *should give 3.097057 squared;
run;
title 'One and two-sided p-values and t and F critical values';
title2 'Computed in a data step using probt, tinv, finv functions';
proc print data=junk;
run;
title2;
*As an alternative to doing to paired t-test in PROC MEANS, SAS has a procedure called
PROC TTEST that will do paired and unpaired t tests. We illustrate its use below to give the
same answer as obtained above with PROC MEANS;
title 'Paired t-test using PROC TTEST';
proc ttest data=tests;
paired posttest*pretest; *gives paired t-test;
run;
/* If this was an independent samples problem (which it is not) a two-sample ttest
could be obtained via the following code:
data tests2;
input group response;
cards;
1 80
1 60
1 65
.
.
.
2 82
2 69
2 75
.
.
.
;
run;
proc ttest data=tests2;
class group;
var response;
run;
*/
* if you sent the output to a pdf file then you need to close it via the following
statement (modify the path and filename as necessary and uncomment);
*ods pdf close;