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      频数      百分比     累积频数 累积百分比

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

          111     46.84          111     46.84

          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

 

    <90         74     31.2236

    90-110      78     32.9114

    >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

 

      112       26     10.9705

       84       20      8.4388

       81            2.9536

       85            2.9536

       92            2.9536

       94            2.9536

       95            2.9536

      105            2.9536

      108            2.9536

 10      114            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

 

         >110        53     22.3629

         90-110      35     14.7679

         <90         38     16.0338

         >110        32     13.5021

         90-110      43     18.1435

         <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

数据分析,数据科学及AI算法是当前最热门的职业。这些职业有着共同的特点:面向数字的,针对编程的以及采取分析手段的。 这些当代热点特性使得在就业市场上对以上职位需求激增也就不足为奇了。但是,市场上提供这方面的大型综合的培训课程是有限,如果说有,大多是知识范围狭窄且非综合性的,而且大多培训都缺乏方法论与实务结合。一般的情况是讲师讲述某种语言的一堆代码,学生听完后甚至连使用方法及代码的前提都不清楚,更别提实际应用场景了。这里,掌握一门数据分析软件本身没错,但仅通过单一的编程培训很难获得聘用为数据分析师或数据科学家所需的技能。那我的解决方案是什么呢?首先,我把所有数据分析中的典型问题都归类总结出来,再结合相应的实际问题,数据以及案例,同时采用世界上最流行的两种数据分析软件:PYTHON 和 SAS去解决这些问题,并将这些解决方法传授给学生。学生在完成培训后更重要的收获是知道每一问题从产生直至解决的前因后果和应用场景,这是因为我在每一课程章节最前都会交代方法论,知识要点及应用场合。SAS和PYTHON可以一起学吗?当然可以。因为我就是这样做到的。具体步骤是,我在课程当中安排了一系列主题,然后使用两种编程语言解决同样的问题。我总结出这样做的好处是边学习边比较,最后在不知不觉当中掌握了两门语言的精华和数据分析的通用方法或模式。过程虽有点长,但十分有趣。最后,为了巩固已学的知识和技能,我还专门安排了针对PYTHON 和 SAS的中小型项目及详细代码讲解。另外,课程当中使用的全部编程代码及数据文件都将免费地提供给注册的学生。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值