Sas tutorial 1018
-
Upload
wensheng-chang -
Category
Documents
-
view
799 -
download
2
Transcript of Sas tutorial 1018
SAS Tutorial 10/18
Data processing: if-then, do, Proc freq
Fig 2.9SEX GRADE1 361 621 702 632 652 66…
• Sex -> Gender• Collapse grades into 3 groups
SEX GRADE1 361 621 702 632 652 80…
GENDER GRADE GROUP
male 36 not good
male 62 good
male 70 good
female 63 good
female 65 good
female 80 excellent
…
Codedata f2_9;
input sex grade;
if sex=1 then gender='male';
else gender='female';
if grade < 60 then group='not good';
else if 60 <= grade <80 then group='good';
else group='excellent';
datalines;
1 36
1 62
1 70
…
;
If-else statement
Structure of if-else statement:
if conditional then descriptions;else descriptions;
Chapter 2, p.54
• Create these histograms
Code
data q2_16;
input pre post;
y1= post-pre;
y2= (post-pre)/pre;
label y1='post-pre';
label y2='(post-pre)/pre';
datalines;
80.5 82.2
84.9 85.6
…
;
proc print label;
proc univariate;
var post y1 y2;
histogram / normal;
run;
How to get this output report?
Let’s cheat
Codedata q2_16;
input pre post;
y1= post-pre;
label y1='post-pre';
datalines;
80.5 82.2
84.9 85.6
…
;
data q2_16a;
set q2_16;
if y1>0 then
do;
pret=pre;
postt=post;
yt=y1;
end;
else delete;
drop pre post y1;
proc print data=q2_16a;
proc univariat;
var yt;
histogram/normal;
run;
3 descriptions
If-else statement
位置檢定 : Mu0=0
檢定 統計值 p 值Student's t t 4.134876 Pr > |t| 0.0007
符號 M 9 Pr >= |M| <.0001
符號秩 S 85.5 Pr >= |S| <.0001
A better way: set categories
• Set a categorical variable• Analyze data by this variable separately
Code
data q2_16;
input pre post;
y1= post-pre;
if y1>0 then z=1;
else z=0;
label y1='post-pre';
label z='">0 or not"';
datalines;
80.5 82.2
84.9 85.6
…
;
proc print label;
proc sort;
by z;
proc freq;
tables z;
proc univariate;
var y1;
by z;
run;
Sort in ascending first
">0 or not"
z 次數 百分比 累計次數
累計百分比
0 11 37.93 11 37.93
1 18 62.07 29 100.00
• Homework: use proc means to produce the report on the above
Exercise 2.1
• Convert the table in Q2.1 into “10, 11, 12, 15, 15, 15,…”.
• Construct the same table.
Code
data q2_1;
input score freq @@;
i=0;
do while(i < freq);
output ;
i=i+1;
end;
drop i freq;
datalines;
10 1 11 1 12 1 15 3 16 4 17 6 18 10 19 7 20 7 21 3 22 2 23
2 24 1 31 1 40 1
;
proc print;
proc freq;
run;
Counter
Loop
Structure of do-while loop:
do while(conditional);descriptions;…;end;
Homework( exercise 2.10)
• Go to http://www.uvm.edu/~dhowell/methods8/DataFiles/DataSets.html
• Download Ex2-10.dat
1. Convert 1 as “male” and 2 as “female” in variable SEX.2. Create a frequency table using proc freq.3. Draw histograms with normal fit for male and female
grades separately.4. Compare the distribution of male and female grades.** Draw boxplot as ex 2.10, p. 57.