数据清洗技术_第1页
数据清洗技术_第2页
数据清洗技术_第3页
数据清洗技术_第4页
数据清洗技术_第5页
已阅读5页,还剩30页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、数据清理技术数据清理技术冯国双冯国双 中国疾控中心卫生统计室中国疾控中心卫生统计室数据清洗数据清洗p从数据收集结束,到统计分析之前,需要对数据从数据收集结束,到统计分析之前,需要对数据做的清理工作做的清理工作数据清洗数据清洗p数据双录入对比数据双录入对比p数据合并数据合并p查找重复值查找重复值p查找缺失值查找缺失值p查找异常值查找异常值双录入对比双录入对比pExcelExcel用到的函数:用到的函数:pIfIf函数函数pIf(If(判断条件判断条件, ,条件满足返回值条件满足返回值, ,条件不满足返回值条件不满足返回值) )pexactexact函数函数比较两个文本是否相同比较两个文本是否相同

2、pexact(exact(比较文本比较文本1,1,比较文本比较文本2 2) )poffsetoffset函数函数返回给定偏移量的新区域返回给定偏移量的新区域poffset(offset(参照区域参照区域, ,行行, ,列列) )双录入对比双录入对比psmall(small(数据区域数据区域, ,第几小第几小 ) ) 计算单元格范围的第几小的计算单元格范围的第几小的数值数值pColumn()Column()p返回单元格所在返回单元格所在号号pRow()Row()p返回单元格所在行返回单元格所在行号号双录入对比双录入对比pSASSAS命令命令:pprocproc compare base= com

3、pare ; ;pb by y 变量变量1 1 变量变量2 2 ; ;prun;run;双录入对比双录入对比pprocproc compare compare语句调用数据比较过程,选项语句调用数据比较过程,选项basebase和和comparecompare分别指定两个比较和被比较的数据集;分别指定两个比较和被比较的数据集;nosummarynosummary的作用是不显示一些概括性的结果。的作用是不显示一些概括性的结果。pbyby语句指定的变量有点类似于索引的作用,通常语句指定的变量有点类似于索引的作用,通常指定指定idid号。如果两个数据集的观测数不同,利用号。如果两个数据集的观测数不同,

4、利用byby语句可以保证它们比较的仍然是同一个语句可以保证它们比较的仍然是同一个idid号,号,而不会出现错位比较的情况。而不会出现错位比较的情况。双录入对比双录入对比pdata a1;data a1;pinput input id g gender age marriage height weight nationid g gender age marriage height weight nation; ;pcardscards; ;p(数据(数据)p; ;pdata data a2;a2;pinput id g gender age marriage input id g gender

5、age marriage height weight height weight nation;nation;pcards;cards;p(数据(数据)p; ;pproc compare base=a1 compare=a2 nosummary;proc compare base=a1 compare=a2 nosummary;prunrun; ;双录入对比双录入对比双录入对比双录入对比查找缺失值查找缺失值pExcelExcel函数:函数:pIfIf函数函数pIf(If(判断条件判断条件, ,条件满足返回值条件满足返回值, ,条件不满足返回值条件不满足返回值) )pexactexact函数函数

6、比较两个文本是否相同比较两个文本是否相同pexact(exact(比较文本比较文本1,1,比较文本比较文本2)2)poffsetoffset函数函数返回给定偏移量的新区域返回给定偏移量的新区域poffset(offset(参照区域参照区域, ,行行, ,列列) )查找缺失值查找缺失值psmall(small(数据区域数据区域, ,第几小第几小 ) ) 计算单元格范围的第几小的数值计算单元格范围的第几小的数值pColumn()Column()p返回单元格所在号返回单元格所在号pRow()Row()p返回单元格所在行号返回单元格所在行号查找缺失值查找缺失值pSASSAS可用可用missingmis

7、sing函数实现函数实现p如果结合数组和自动变量,可以一次性实现所有如果结合数组和自动变量,可以一次性实现所有变量缺失值的输出变量缺失值的输出查找缺失值查找缺失值pdata a1;data a1;pinput input id g gender age marriage height weight nationid g gender age marriage height weight nation; ;pmiss_gmiss_g=missing(g);=missing(g);pmiss_gendermiss_gender=missing(gender);=missing(gender);pm

8、iss_agemiss_age=missing(age);=missing(age);pmiss_marriagemiss_marriage=missing(marriage);=missing(marriage);pmiss_heightmiss_height=missing(height);=missing(height);pmiss_weightmiss_weight=missing(weight);=missing(weight);pmiss_nationmiss_nation=missing(nation);=missing(nation);pcardscards; ;p(数据)(数

9、据)p;pprocproc print; print;prun;run;查找缺失值查找缺失值p部分结果部分结果查找缺失值查找缺失值p利用数组和自动变量利用数组和自动变量pdata data missing;missing;pset a;set a;parray chaarray cha* * _character_ ; _character_ ;pdo i=1 to dim(cha); do i=1 to dim(cha); pif missing(chai) then output;if missing(chai) then output;pend;end;parray array numn

10、um * * _numeric_ ; _numeric_ ;pdo i=1 to dim(do i=1 to dim(numnum); ); pif missing(if missing(numnumi) then output;i) then output;pend;end;pdrop i;drop i;pprocproc print; print;prun;run;查找缺失值查找缺失值p缺失值结果缺失值结果查找重复值查找重复值pExcelExcel函数:函数:pcountifcountif( (计数区域计数区域, ,条件条件 ) ) 根据指定条件,在计数区域内计数根据指定条件,在计数区域内

11、计数查找重复值查找重复值pSASSAS命令:命令:pprocproc sort out= sort ;pby by 变量变量1 1 变量变量2 2 ; ;prunrun; ;查找重复值查找重复值pprocproc sort sort语句调用排序过程。选项语句调用排序过程。选项out=out=数据集指定排序数据集指定排序后的数据集名。因为排序后数据发生了变化,因此可指定后的数据集名。因为排序后数据发生了变化,因此可指定该选项将排序后的数据存放到一个新的数据集中。如果不该选项将排序后的数据存放到一个新的数据集中。如果不加该选项,排序后的数据集将覆盖原有数据集,这样你就加该选项,排序后的数据集将覆盖

12、原有数据集,这样你就找不回原有的未排序的数据了。找不回原有的未排序的数据了。p选项选项nodupkeynodupkey表示如果表示如果byby语句指定的排序变量有重复值,语句指定的排序变量有重复值,则删除重复值。如按则删除重复值。如按idid排序,如果排序,如果idid有重复值,则只保留有重复值,则只保留重复值中的第一个值,删除其它值。重复值中的第一个值,删除其它值。p选项选项nouniquekeynouniquekey的作用跟的作用跟nodupkeynodupkey正好相反,如果正好相反,如果byby语语句指定的排序变量都是唯一值,则将其删除。如按句指定的排序变量都是唯一值,则将其删除。如按

13、idid排序排序,如果,如果idid没有有重复值,则全部删除没有有重复值,则全部删除。查找重复值查找重复值pbyby语句指定排序的变量,可以指定多个。选项语句指定排序的变量,可以指定多个。选项descendingdescending表示按降序排序,如果不加该选项,表示按降序排序,如果不加该选项,默认的是按升序排序默认的是按升序排序。p当需要查找重复值时,当需要查找重复值时,byby语句指定的变量就是需语句指定的变量就是需要查找的重复值变量。要查找的重复值变量。查找重复值查找重复值pdata a1;data a1;pinput id g gender age marriage height we

14、ight nation;input id g gender age marriage height weight nation;pcardscards; ;p(数据)(数据)p;pprocproc sort sort nouniquekeynouniquekey out=bb; out=bb;pby genderby gender age marriage height weightage marriage height weight; ;prun;run;pprocproc print data=bb; print data=bb;prun;run;查找重复值查找重复值p5 5个变量均重复的

15、观测个变量均重复的观测查找异常值查找异常值pExcelExcel函数:函数:pIfIf函数函数pIf(If(判断条件判断条件, ,条件满足返回值条件满足返回值, ,条件不满足返回值条件不满足返回值) )p结合各种算术运算符、比较运算符、逻辑运算符等结合各种算术运算符、比较运算符、逻辑运算符等查找异常值查找异常值p常见运算符常见运算符符号说明示例+和-加减运算=3+2-4=1*和/乘除运算=3*2/4=1.5乘幂=32=9,=16(1/2)=4=、=、=等于、不等于大于、小于大于等于、小于等于=(A2=9),判断A2是否等于9=(A29),判断A2是否不等于9=(A2=9),判断A2是否大于等于

16、9and多个表达式同时成立 and(A19,B19,B1 (大于)、(大于)、 =(大于等于)、(大于等于)、 = =(小于等(小于等于)于)inin,表示其中之一,表示其中之一,如如grade in(2,4,6grade in(2,4,6) )表示表示只要是只要是gradegrade为为2 2、4 4、6 6中的中的其中一个就算符合条件其中一个就算符合条件;deptdept not in(“A”, “Bnot in(“A”, “B”)”)表示表示只要只要deptdept不是不是“A A”或或“B B”就算成立就算成立& &或或andand(表示(表示2 2个表达式同个表达式同时成立时成立)

17、| | 或或oror(表(表示两个表达式示两个表达式至少一个成立)至少一个成立)查找异常值查找异常值pdata a1;data a1;pinput id g gender age marriage height weight nation;input id g gender age marriage height weight nation;pcards;cards;p(数据)(数据)p;pdata b1;data b1;pset a1;set a1;pif if (gender not in(1,2) (gender not in(1,2) | | (age=60) (age=60) | |

18、 (height=200(height=200) ) | | (weight=100) (weight=100) p| | (marriage (marriage not in (1,0not in (1,0) ) | | (nation (nation not in (not in (1,2)1,2); ;pprocproc print; print;prun;run;查找异常值查找异常值查找异常值查找异常值pdata b2;data b2;pset a1;set a1;pif (gender not in(1,2,.) if (gender not in(1,2,.) | |p( (age

19、=. and (age=60) | age=. and (age=60) | p( (height=. and(height=150 or height=. and(height=200=200) | |p( (weight=. and (weight=40 or weight=. and (weight=100=100)| )| pmarriage marriage not in (1,0,.) | not in (1,0,.) | pnation nation not in (1,2,.);not in (1,2,.);pprocproc print; print;prun;run;查找异

20、常值查找异常值查找异常值查找异常值pdata data gendergender(where=(gender not in(1,2,.) (where=(gender not in(1,2,.) pageage(where=(not missing(age) and(age=50) (where=(not missing(age) and(age=50) pmarriagemarriage(where=(marriage not in(1,0,.) (where=(marriage not in(1,0,.) pheightheight(where=(not missing(height) and(height=150 or (where=(not missing(height) and(height=200)height=200)pweightweight(where=(not missing(weight) and(weight=40 or (where=(not missing(weight) and(weight=100) weight=100) pnationnation(where=(nation not in(1,2

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论