Sas tutorial 1018

17
SAS Tutorial 10/18 Data processing: if-then, do, Proc freq

Transcript of Sas tutorial 1018

Page 1: Sas tutorial 1018

SAS Tutorial 10/18

Data processing: if-then, do, Proc freq

Page 2: Sas tutorial 1018

Fig 2.9SEX GRADE1 361 621 702 632 652 66…

Page 3: Sas tutorial 1018

• 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

Page 4: Sas tutorial 1018

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;

Page 5: Sas tutorial 1018

Chapter 2, p.54

• Create these histograms

Page 6: Sas tutorial 1018

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;

Page 7: Sas tutorial 1018
Page 8: Sas tutorial 1018

How to get this output report?

Page 9: Sas tutorial 1018

Let’s cheat

Page 10: Sas tutorial 1018

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

Page 11: Sas tutorial 1018

位置檢定 : Mu0=0

檢定 統計值 p 值Student's t t 4.134876 Pr > |t| 0.0007

符號 M 9 Pr >= |M| <.0001

符號秩 S 85.5 Pr >= |S| <.0001

Page 12: Sas tutorial 1018

A better way: set categories

• Set a categorical variable• Analyze data by this variable separately

Page 13: Sas tutorial 1018

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

Page 14: Sas tutorial 1018

• Homework: use proc means to produce the report on the above

Page 15: Sas tutorial 1018

Exercise 2.1

• Convert the table in Q2.1 into “10, 11, 12, 15, 15, 15,…”.

• Construct the same table.

Page 16: Sas tutorial 1018

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;

Page 17: Sas tutorial 1018

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.