SAS编程数据挖掘学习笔记(原创) 下载本文

内容发布更新时间 : 2024/6/26 20:06:44星期一 下面是文章的全部内容请认真阅读。

1. 数据集

永久(可以自己建立逻辑库,也可以放sasuser逻辑库中)在和临时(work逻辑库中); 2.建立数据集

(1)建立永久数据集,必须先用LIBNAME语句定义一个逻辑库名。 e.g. libname mylib 'D:\\软件新\\SAS2015-2014.3\\SASLX';

data mylib.test; input x y; datalines; 1 2 2 4 3 6 4 8 ;

Proc print;

Run;

(2)从外部文本数据文件中读入数据 A.从建议的数据集中读入数据:set; e.g. data ex3;

set mylib.test; a=x+y; proc print; run;

B.proc import 过程实现外部数据的导入(txt和xls都可以)

proc import datafile='D:\\软件新\\SAS2015-2014.3\\SASLX\\data1.txt' out=sasuser.test2 replace; getname=yes; run;

proc print data=sasuser.test2; run;

proc import datafile='D:\\软件新\\SAS2015-2014.3\\SASLX\\fit.xlsx' out=work.fit1t (where=(Sex='M')) replace; *getname=yes;可以省略 run;

proc print data=work.fit1t; run;

proc import datafile='D:\\软件新\\SAS2015-2014.3\\SASLX\\fit.xlsx' out=work.fit3t replace; sheet=\; *getname=yes; run;

proc print data=work.fit3t; run;

3.keep语句保留变量

只有Keep语句中出现的变量被写入新数据集中。 4.条件语句,select语句

data C;

infile 'D:\\软件新\\SAS2015-2014.3\\SASLX\\test2.txt'; length num $ 4 name $ 10;

input num $ name $ score1 score2 score3 sex $; select (sex);

when (\) put \性别:男\; when (\) put \性别:女\; end; run;

proc print data=work.C; run;

data example1; set sasuser.mytest; select (Sex);

when (\) put name \男\; when (\) put name \女\; end; run;

proc print data=work.example1; run;

data ex1; set work.score; select (sex);

when (\) put name \女\; when (\) put name \男\; end; run;

5.where语句

data class; set work.score;

where sex=\ and math >=90; run;

proc print data=work.class; run;

6.delete语句

data cla; set work.score;

if sex=\ and math >=90 then do;

put name; delete; end; run;

proc print data=work.cla; run;

7.循环语句do while语句 8.数组

9.数据排序 proc sort过程

data class2; set work.score; run;

proc sort data=work.class2 out=CS1; by num descending math; run;

proc print data=work.CS1; run;

10.数据集连接:set

11.描述性统计分析(第七章) (1)proc mean

proc import datafile= 'D:\\软件新\\SAS2015-2014.3\\SASLX\\fit.xlsx' out=mylib.score1; getname=yes; run;

proc means data=mylib.score1 maxdec=3; var math phys english; run;

(2)proc freq data abc; do a = 1 to 2; do b=1 to 2; input f @@; output; end; end; cards; 52 19 39 3 ;

proc freq; weight f;

tables a*b/chisq expected nopercent nocol; run;

(3)proc univariate

12.假设检验 Proc ttest

(1)单样本t检验 data time; input time @@; datalines;

43 90 84 87 116 95 86 99 93 92 121 71 66 98 79 102 60 112 105 98 ; run;

proc ttest h0=80 sides=u alpha=0.05; var time; run;

(2)成组实验的t检验

data ex823; input x @@;

if _n_>11 then a=2; else a=1; datalines;

2.60 3.24 3.73 3.73 4.32 4.73 5.18 5.58 5.78 6.40 6.53

1.67 1.98 1.98 2.33 2.34 2.50 3.60 3.73 4.14 4.17 4.57 4.82 5.78 ;

proc ttest; class a; var x; run;

(3)配对比较t检验:

data pressure; input a1 a2 @@; datalines;

120 128 124 131 130 131 118 127 140 132 128 125 140 141 135 137 126 118 130 132 126 129 127 135 ; run;

proc ttest; paired a1*a2; run;

(4)t检验属于参数的假设检验,前提条件是总体的分布为正太分布。但在许多实际问题中,总体的分布形式很难确定,或者总体的分布为偏态。在这种情况下,t检验就不适用了,一般可选用非参数统计方法。

Npar1way过程专门用来处理单因素的非参数检验。

data na; input x @@; a=1;

if _n_>5 then a=2; if _n_>14 then a=3; datalines;

13 6.8 7.8 15.5 11.4

14.1 7.9 6 6.7 5.2 10.7 3.8 9.4 13.9 9.2 11.6 4.2 ; run;

proc npar1way wilcoxon;秩和检验 class a; var x; run;

13.方差分析

方差分析是检验两个或多个样本均数间差异是否具有统计意义的一种方法。(和假设检验中的两个样本的均值检验的区别?)

多组均数间的多重比较:研究因素单元对因变量影响之间是否存在显著性差异。 (1)完全随机设计资料的方差分析(单因素方差分析) Proc anova过程

data anova1; do i=1 to 5; do a=1 to 4; input x @@;

output;(循环输入数据时必不可少!!!)