数据清理

  • 格式:docx
  • 大小:26.50 KB
  • 文档页数:16

数据清理数据清理data Cleaning技术大全及SAS实现转载请注明出处:/s/blog_5d3b177c0100esmx.html1 简介数据清理是数据准备一个很重要的环节,什么是数据清理呢?数据清理Is for techies 技术人员的事Is just coding 只是写代码Is boring 很无聊Consumes up to 80 % of the project要花掉项目80%的时间Was not in the focus of data mining literature so far在数据挖掘中数据清理相关的文章不是很多Is something that SAS can excellently do SAS可以很好地搞定Is vital to the quality of the project 是项目质量的一个重要步骤首先说明一下,由于没搞到本书的数据,所以就用其它的书《Predictive Modeling Using Logistic Regressio》的数据进行程序调试。

2 字符型数据清理2.1 观察数据集2.1.1 首先可以观察一下数据集中,所有字符型变量的数据情况:procfreq data=pmlr.Develop(drop=branch);tables _character_ / nocumnopercent;run;关键词_character_表示所有的字会型变量,其它的_numeric_和_all_已经讲解过。

Drop和keep 选项可以剔除或保留选择的变量。

2.1.2 观测了变量的值的情况后,我们就可能会发现一些错误值,这时可以将这些异常值输出出来(输出到日志中,或输出到数据集中,或外部文件):data _null_;setpmlr.Develop;file print; ***send output to the output window;***check Res;if Res not in ('R' 'S' 'U') then put Res= ;***check Branch;if verify(trim(Branch),'B0123456789') and not missing(Branch) and substr(Branch,1,1) netrim('B')then put Branch= ;run;这里,如果RES变量的值不是R、S、U,则为错误的值。

2.1.3 通过format来查看错误值个数这里我们假设Res中的U是错误的值,我们可以用以下操作来实现错误值个数的观测procformat;value $Res 'R','S' = 'Valid'other = 'Miscoded';run;procfreq data=pmlr.Develop;format Res $Res.;tables Res/ nocumnopercent missing;run;2.2 几个重要的函数Verify:SAS的verify函数在数据处理和data clean的过程中十分有用,verify函数的第一个参数是源字符串,后续参数都是待查找字符,如果源字符串中包含的都是待查找字符,verify就返回0,否则,返回不包含字符在源字符串中的位置。

由此可见,我们可以利用verify 函数对字符串作非正常字符的检测,从而达到clean data的目的。

verify的语法:verify(source, chkstring1[, chkstring2, … , chkstringn]) (参考文献:/u/6776/showart_1073354.html)TRIM(s) 返回去掉字符串s的尾随空格的结果。

MISSING:可用于字符型和数字型变量,当变量为空时,返回1,当变量不为空时,返回0。

这个前面的博文已有讲解。

NOTDIGIT(character_value) 在字符串中搜索非数字字符,若找到则返回其第一次出现的位置,在这里,notdigit(character_value)和verify(character_value,'0123456789')实现的功能相同。

3数值型数据清理3.1 极值异常值3.1.1 三个重要的过程步:means过程步:得到缺失值和非缺失值个数、最大值、最小值等统计值。

procmeans data=pmlr.Develop n nmiss min max maxdec=3;varDDABalDepAmt;run;tabulate过程步:得到缺失值和非缺失值个数、均值、最大值、最小值等统计值proctabulate data=pmlr.Develop format=7.3;varDDABalDepAmt;tablesDDABalDepAmt,n*f=7.0nmiss*f=7.0 mean min max / rtspace=18;keylabeln = 'Number'nmiss = 'Missing'mean = 'Mean'min = 'Lowest'max = 'Highest';run;univariate过程步:UNIVARIATE过程除了可以提供MEANS和SUMMARY所提供了基本统计数外,还提供位置特征数(如Med中位数,Mode众数)和偏度系数(Skewness)、峰度系数(Kurtosis)这些变异数。

此外它还可通过FREQ选项统计变量次数及频率,通过PLOT 选项给出茎叶图(Stem Leaf)和正态概率密度图(Normal Probability Plot),通过NORMAL选项进行变数正态性检验(给出W:Normal值)。

procunivariate data=pmlr.Develop plot;varDDABalDepAmt;run;除了上述过程步外,还有很多过程步也可以完成相似的功能,大家可以去查阅相关文献。

3.2 最大(小)N条数据前面的方法可以得到最大最小值,但是当我们要取最大的N条数据,或最小的N条数据时,可以用下面的选项:univariate过程步:用nextrobs或nextrvals选项,可以得到最大的和最小的N条数据procunivariate data=pmlr.Developnextrobs=10; ** nextrvals=10;varDDABal;run;3.3 得到分位数数据3.3.1 用univariate过程步得到前N%的数据:procunivariate data=pmlr.Developnoprint;varDDABal;output out=tmppctlpts=1090pctlpre = L_; **得到10%和90%分位数;run;data hilo;setpmlr.Develop(keep=DDABal);if _n_ = 1 then set tmp; **将得到10%和90%分位数数据集与原数据集合并,生成新数据集hilo;ifDDABal le L_10 and not missing(DDABal) then do;Range = 'Low '; **如果小于10%分位数的值,则标记为low并输出;output;end;else if DDABalge L_90 then do;Range = 'High'; **如果大于90%分位数的值,则标记为high并输出;output;end;run;将上面的代码参数化一下,就可以得到一个宏,功能为输出前N%的数据%macro hilowper(Dsn=,Var=,Percent=,Idvar= );%let Up_per = %(100 - &Percent);procunivariate data=&Dsnnoprint;var&Var;id&Idvar;output out=tmppctlpts=&Percent &Up_perpctlpre = L_; run;datahilo;set&Dsn(keep=&Idvar&Var);if _n_ = 1 then set tmp;if&Var le L_&percent and not missing(&Var) then do; range = 'Low';output;end;else if &Varge L_&Up_per then do;range = 'High';output;end;run;proc sort data=hilo;by&Var;run;title "Low and High Values for Variables";proc print data=hilo;id&Idvar;var Range &Var;run;proc datasets library=work nolist;deletetmphilo;run;quit;%mend hilowper;3.3.2 用rank过程步得到前N%的数据:Rank过程步详见(/s/blog_5d6632e70100ddqe.html),下面介绍一下宏,实现上例的功能:%macro top_bottom_nPercent(Dsn=,Var=,Percent=,Idvar=);%let Bottom = %(&Percent - 1);%let Top = %(100 - &Percent);proc format;valuernk0 - &Bottom = 'Low'&Top - 99 = 'High';run;proc rank data=&Dsn(keep=&Var&Idvar)out=new(where=(&Var is not missing))groups=100;var&Var;ranks Range;run;proc sort data=new(where=(Range le &Bottom orRange ge&Top));by&Var;run;***Produce the report;proc print data=new;title "Upper and Lower &Percent.% Values for %upcase(&Var)";id&Idvar;var Range &Var;format Range rnk.;run;proc datasets library=work nolist;delete new;run;quit;%mend top_bottom_nPercent;3.4 得到最大或最小N个数据实现方法:最小N个数据:排序后直接用obs=N即可得到最大N个数据:排序,然后得到样本总数Num,减去N-1,再用firstobs=(Num-N+1)得到procsort data=pmlr.Develop(keep=DDABalDepAmt) out=tmp;byDepAmt;run;data _null_;settmp nobs=Num_obs;callsymputx('Num',Num_obs);run;%let High = %(&Num - 9);title "Ten Highest and Ten Lowest Values for HR"; data _null_;settmp(obs=10)tmp(firstobs=&High) ;file print;if _n_ le 10 then do;if _n_ = 1 then put / "Ten Lowest Values" ;put "Patno = " DepAmt @15 "Value = " DDABal; end;else if _n_ ge11 then do;if _n_ = 11 then put / "Ten Highest Values" ;put "Patno = " DepAmt @15 "Value = " DDABal; end;run;用宏实现:%macro highlow(Dsn=,Idvar=,n= );proc sort data=&Dsn(keep=&Idvar&Var where=(&Var is not missing)) out=tmp;by&Var;run;data _null_;settmp nobs=Num_obs;callsymput('Num',Num_obs);stop;run;%let High = %(&Num - &n + 1);title "&n Highest and Lowest Values for &Var"; data _null_;settmp(obs=&n)tmp(firstobs=&High) ;file print;if _n_ le &n then do;if _n_ = 1 then put / "&n Lowest Values" ;put "&Idvar = " &Idvar @15 "Value = " &Var;end;else if _n_ ge %(&n + 1) then do;if _n_ = %(&n + 1) then put / "&n Highest Values" ;put "&Idvar = " &Idvar @15 "Value = " &Var;end;run;proc datasets library=work nolist;deletetmp;run;quit;%mend highlow;3.5 异常值错误值重复值缺失值对于错误值,处理字符型变量用到的方法大多也可以用到数值型变量上,如上面的if then ,以及format,这里就不作多的讲解。