SAS Code

advertisement
D.G. Bonett (1/2016)
SAS Code for Homework Problems
SAS for Windows and SAS University Edition are both available to UCSC students for
free. The University Edition uses SAS Studio which works with Windows, OS X, or Linux.
SAS for Windows installs on your computer while SAS Studio runs SAS from a server
using your web browser. Contact Paul Sosbee (paul@ucsc.edu) for assistance in
installing SAS for Windows. SAS Studio can be downloaded from:
http://www.sas.com/en_ca/software/university-edition/download-software.html
Homework 1-1ef
Step 1. Open the SAS program. When you start SAS for Windows, you will see the following
split screen. The “Log” screen is on the top and the “Editor” screen is on the bottom. You will
enter your SAS program in the Editor screen. All of the SAS programs for this course have been
written for you. All you need to do is copy and paste the programs in the Editor screen and then
run the program. When you run the program, the Log screen displays actions taken for each
command and reports any warnings or errors in the SAS program. [In SAS Studio, the Log and
Editor screens are replaced by a single Code Entry screen].
Step 2. Enter (or just copy and paste) the following SAS program in the Editor screen. This is
code for analyzing a design with a single quantitative factor. The data file has 32 rows (one row
per participant) and two columns. The first column contains the values of the quantitative factor
(drug dosage), and the second column contains the response variable scores.
D.G. Bonett (1/2016)
data;
input Dose Score;
datalines;
5 47
5 34
5 38
5 40
5 41
5 37
5 30
5 46
10 39
10 44
10 41
10 30
10 29
10 46
10 39
10 28
20 38
20 27
20 39
20 42
20 33
20 35
20 36
20 28
40 36
40 26
40 35
40 23
40 28
40 29
40 34
40 29
;
proc reg;
model Score = Dose/clb alpha = .05;
run;
Instructor note: The input statement indicates that two variables, Dose and Score will be read (“Dose”
and “Score” are user specified names). The datalines statement indicates that data to be analyzed will
follow. The proc plot statement instructs SAS to print a scatterplot. The model command in the proc glm
statement indicates that Score is the response variable and Dose is the predictor variable. Each
command must end with a semicolon.
Step 3. Click on the small “running person” icon at the top of the screen to run the program. If
there are no errors in the code, the output will be displayed in the top half of the screen. If no
output appears, check the Log file (click on the Log button at the bottom of the screen) to see
the error messages. Go to the Editor screen and correct the errors then run again. The most
common types of errors are misspellings (procedure names, options codes, variable names)
and omitting required semicolons. The relevant parts of the output are shown below.
D.G. Bonett (1/2016)
Dependent Variable: Score
Parameter
Estimate Standard Error t Value Pr > |t|
95% Confidence Limits
Intercept 39.93478261
1.68298133
23.73
<.0001 36.49767619 43.37188902
-0.25152174
0.07301798
-3.44
0.0017 -0.40064435 -0.10239913
Dose
Dependent Variable: Score
Source
DF Sum of Squares Mean Square F Value Pr > F
Model
1
363.763315
363.763315
Error
30
919.705435
30.656848
Corrected Total 31
1283.468750
11.87 0.0017
Instructor note: Instead of entering data after a datalines statement, the data could be saved in a text file
and read into SAS using the infile command. Suppose the data file was named “HW1-1.txt” and stored
on drive E. The following code would read the data from the file. SAS can also read SPSS and Excel
data files.
data;
infile “E:HW1-1.txt”;
input Dose Score;
proc reg;
model Score = Dose/clb alpha = .05;
run;
D.G. Bonett (1/2016)
Homework 1-1h
[run Program 10]
Homework 1-2efg
data;
proc import datafile = "E:\HW1-2.sav" out = mydata dbms = sav replace;
run;
proc reg data = mydata;
model child = parent/clb alpha = .05;
proc corr data = mydata fisher (alpha = .05 biasadj = no);
var child parent;
run;
Instructor note: This SAS code reads an SPSS data file from drive E which contains the data as well as
variable names.
Homework 1-3b
[run Program 6]
proc IML;
cor1 = .886;
/* sample correlation for group 1
LL1 = .852;
/* correlation lower limit for group 1
UL1 = .912;
/* correlation upper limit for group 1
cor2 = .802;
/* sample correlation for group 2
LL2 = .747;
/* correlation lower limit for group 2
UL2 = .846;
/* correlation upper limit for group 2
/* ============================================================
reset noname printadv = 0;
options nodate nonumber nocenter;
L = cor1 - cor2 - sqrt((cor1 - LL1)**2 + (UL2 - cor2)**2);
U = cor1 - cor2 + sqrt((UL1 - cor1)**2 + (cor2 - LL2)**2);
print "CONFIDENCE INTERVAL FOR DIFFERENCE IN TWO CORRELATIONS";
print "COMPUTED FROM TWO INDEPENDENT SAMPLES";
print , "Lower limit:
" L [format = 6.4];
print "Upper limit:
" U[format = 6.4];
quit;
Homework 1-4a
[run Program 9]
*/
*/
*/
*/
*/
*/
*/
D.G. Bonett (1/2016)
Homework 2-1c
data;
input readscore TV IQ;
datalines;
20 15 82
36 12 96
72 8 112
40 10 90
95 5 130
71 8 121
65 8 115
48 10 98
55 10 105
85 7 120
92 4 128
45 12 95
;
proc corr fisher (alpha = .05 biasadj = no);
var readscore TV;
partial IQ;
run;
Homework 2-2cd
data;
input sonaggr hours fatheraggr;
datalines;
50 5 62
62 6 54
62 2 73
50 5 39
49 3 51
60 6 57
40 4 30
36 2 45
79 7 64
39 4 33
36 0 45
55 4 46
52 2 52
42 0 35
59 4 51
50 4 42
57 5 38
60 7 65
58 4 47
67 3 76
;
ods graphics off; ods html close; ods listing;
proc reg;
model sonaggr = hours fatheraggr/clb alpha =.05;
proc cancorr smc spcorr short;
var sonaggr;
with hours fatheraggr;
run;
Instructor note: The optional ods graphics off; ods html close; ods listing; commands will print output in a
simple text format which takes less space and is easier to copy and paste into other documents.
D.G. Bonett (1/2016)
Homework 2-2e
[run Program 2]
Homework 2-3c
data;
input spect diff hr @@;
spect = spect - 1.3333;
diff = diff - 5;
inter = spect*diff;
datalines;
0 1 67 0 1 64 0 1 68 0 1 70
1 1 73 1 1 71 1 1 69 1 1 79
3 1 75 3 1 74 3 1 72 3 1 82
0 5 70 0 5 66 0 5 71 0 5 74
1 5 78 1 5 74 1 5 75 1 5 81
3 5 80 3 5 83 3 5 75 3 5 90
0 9 74 0 9 78 0 9 75 0 9 65
1 9 83 1 9 80 1 9 75 1 9 80
3 9 87 3 9 84 3 9 91 3 9 85
;
proc reg;
model hr = diff spect inter/clb covb alpha = .05;
run;
Instructor note: The @@ command in the input statement for HW 2-3c above instructs SAS to treat the
three numbers 0 1 67 as the first case, the next three numbers 0 1 64 as the second case, and so on.
This input format is used here only to save space. The traditional one line per case format is usually
much easier to enter and edit.
Homework 2-4c
data;
input group score gpa;
datalines;
1 10 3.6
1 8 3.1
1 9 3.5
1 12 3.7
1 10 3.5
1 8 3.0
1 9 3.0
1 10 3.1
1 13 3.8
1 11 3.4
0 7 3.4
0 8 3.6
0 11 3.7
0 6 3.1
0 5 3.0
0 8 3.5
0 9 3.5
0 13 3.8
0 7 3.1
0 5 3.0
;
proc glm;
class group;
model score = group gpa/solution clparm alpha = .05;
run;
D.G. Bonett (1/2016)
Homework 3-1c
data;
proc import datafile = "E:\HW3-1.sav" out = mydata dbms = sav replace;
run;
ods graphics off; ods html close; ods listing;
proc calis data = mydata;
path
SS1 -> CLS1,
SS1 -> CLS2,
SS2 -> CLS2;
pcov
CLS1 CLS2;
run;
Homework 3-2cde
data;
proc import datafile = "E:\HW3-2.sav" out = mydata dbms = sav replace;
run;
ods graphics off; ods html close; ods listing;
proc calis data = mydata;
path
motherED -> AchMot,
motherOC -> AchMot,
motherED -> EDgoal,
AchMot -> EDgoal;
effpart
EdGoal <- motherOC motherED;
run;
Download