天天看點

SAS:Freq過程介紹

Freq過程介紹

原文位址:http://www2.sas.com/proceedings/sugi31/252-31.pdf

轉載請注明出處: http://blog.sina.com.cn/s/blog_5d3b177c0100b68k.html

原文沒有提供資料,是以就在網上随便找了個資料進行測試,位址如下:http://www.sasenterpriseminer.com/data/htwt.xls

該資料包含4個變量(性别sex,年齡age,身高height,體重weight),共237個觀測。

1 Freq 文法

proc freq <options> ; 

by variables ; 

exact statistic-options < / computation-options> ; 

output <OUT= dataset> options ; 

tables requests < /options> ; 

test options ;   

weight variable ; 

2 如果直接運作freq過程步,程式如下,它将會對所有的變量進行操作。

proc freq data=Htwt;

run;

部分結果:

FREQ 過程

sex

sex      頻數      百分比     累積頻數 累積百分比

------------------------------------------------------------------------------

f           111     46.84          111     46.84

m           126     53.16          237    100.00

3 tables:得到給定變量的頻數統計,或多變量的交叉表頻數統計。

proc freq data=Htwt;

tables sex;

run;

結果如上。

4 format:對連續數值變量做Freq時,系統會對每個數值進行頻數統計,這個結果一般不是我們所需要的。我們一般會将連續變量轉換為離散變量,這個可以通過Format過程步來實作。

proc format;

value height_ctg    0-50   = '<50'

   50-60 = '50-60'

   60-high   = '>60';

value weight_ctg     0-90    = '<90'

   90-110 = '90-110'

   110-high     = '>110';

 run;

proc freq data=Htwt;

tables weight*height;

format weight weight_ctg.;

format height height_ctg.;

run;

結果:

FREQ 過程

weight * height 表

weight(weight)     height(height)

頻數    |

百分比  |

行百分比|

列百分比|50-60   |>60     |   合計

--------+--------+--------+

<90     |     61 |     13 |     74

        |  25.74 |   5.49 |  31.22

   |  82.43 |  17.57 |

        |  67.78 |   8.84 |

--------+--------+--------+

90-110  |     24 |     54 |     78

        |  10.13 |  22.78 |  32.91

        |  30.77 |  69.23 |

        |  26.67 |  36.73 |

--------+--------+--------+

>110    |      5 |     80 |     85

        |   2.11 |  33.76 |  35.86

       |   5.88 |  94.12 |

    |   5.56 |  54.42 |

--------+--------+--------+

合計          90      147      237

37.97    62.03   100.00

5 norow nocol nopercent:有時我們隻需要頻數,不需要各行各列的百分比,我們就可以在tables後面加上這些參數。

proc freq data=Htwt;

tables weight*height/norow nocol nopercent;

format weight weight_ctg.;

format height height_ctg.;

run;

結果:

FREQ 過程

weight * height 表

weight(weight)     height(height)

頻數    |50-60   |>60     |   合計

--------+--------+--------+

<90     |     61 |     13 |     74

--------+--------+--------+

90-110  |     24 |     54 |     78

--------+--------+--------+

>110    |      5 |     80 |     85

合計          90      147      237

Norow:不要行的百分比

Nocol:不要列的百分比

Nopercent:不要頻數的百分比

Nocum:單變量時不要累積頻數和累積百分比

Nofreq:不要頻數

Noprint:不列印

Nowarn:不輸出警告資訊

Missing:将缺失值也進行統計

6 對變量加label辨別,使輸出更直覺

proc freq data=Htwt;

tables weight*height/norow nocol nopercent;

format weight weight_ctg.;

format height height_ctg.;

label weight = '高度';

label height = '重量';

run;

結果:

FREQ 過程

weight * height 表

weight(高度)     height(重量)

頻數    |50-60   |>60     |   合計

--------+--------+--------+

<90     |     61 |     13 |     74

--------+--------+--------+

90-110  |     24 |     54 |     78

--------+--------+--------+

>110    |      5 |     80 |     85

合計          90      147      237

7 By:對這個變量的值進行分頁顯示

proc freq data=Htwt;

tables weight/norow nocol nopercent;

format weight weight_ctg.;

by sex;

run;

結果(以第一頁為例)

----------------- sex=m ------------

 FREQ 過程

 weight

 weight      頻數        累積頻數

 ------------------------------

 <90             38          38

 90-110          35          73

 >110            53         126

8 out:輸出資料集

proc freq data=Htwt;

tables weight/ out=htwtfreq;

format weight weight_ctg.;

run;

proc print data= htwtfreq;

run;

結果:

Obs    weight    COUNT    PERCENT

1     <90         74     31.2236

2     90-110      78     32.9114

3     >110        85     35.8650

9 order選項:使輸出按指定的order方式排序。

Order=data :按輸入資料集的順序排序

Order=formatted :按其formatted value排序

Order=freq :按計算的頻數的降序排序

Order=internal :按其unformatted value排序

data htwttmp;

set htwt;

weight=round(weight);

run;

proc freq data=Htwttmp order=freq;

tables weight/ out=htwtfreq ;

run;

proc print data= htwtfreq(obs=10);

run;

結果:

Obs    weight    COUNT    PERCENT

 1      112       26     10.9705

 2       84       20      8.4388

 3       81        7      2.9536

 4       85        7      2.9536

 5       92        7      2.9536

 6       94        7      2.9536

 7       95        7      2.9536

 8      105        7      2.9536

 9      108        7      2.9536

 10      114        7      2.9536

10 list當對多個變量進行交叉頻率操作,我們隻需要頻數和百分比時可以用到。

proc freq data=Htwttmp order=freq;

tables sex*weight/list out=htwtfreq ;

format weight weight_ctg.;

run;

proc print data= htwtfreq(obs=10);

run;

結果:

Obs    sex    weight    COUNT    PERCENT

 1      m     >110        53     22.3629

 2      m     90-110      35     14.7679

 3      m     <90         38     16.0338

 4      f     >110        32     13.5021

 5      f     90-110      43     18.1435

 6      f     <90         36     15.1899

11 對缺失值和非缺失值進行頻數統計

data Htwtmissing;

set Htwttmp;

if weight<100 then weight=.;

run;

proc format;

   value misscnt .= 'Missing'

               other ='Nonmissing';

run;

proc freq data = Htwtmissing;

   tables _numeric_ / missing nocum nopercent;

   format _numeric_ misscnt.;

run;

結果:

age

 age      頻數

 ----------------------

 Nonmissing         237

 height

 height      頻數

 ----------------------

 Nonmissing         237

 weight

 weight      頻數

 ----------------------

 Missing            115

 Nonmissing         122

繼續閱讀