很多数据集里都包含了缺失数据,且不论什么原因导致了数据的缺失,就缺失数据问题我们应作何处理??
1.检查是否存在缺失
缺失数据形成的原因很多,可能是遗落,也可能是字符型数据与数值型数据混淆。阅读SAS log通常可以得到原因。
2.计算缺失值个数
对于数值型数据,计算缺失值个数可以通过proc means实现,而字符型数据则可以利用proc freq实现。
例如:
数值型数据
proc means data=yuyu n nmiss; /*n nmiss用于读取缺失与未缺失数个数*/
run;
字符型数据
proc format;
value $misscnt ' ' = 'missing' other = 'nonmissing';
run;
proc freq data=yuyu;
tables _character_ / nocum missing; /*_character_提示字符型数值类型*/
format _character_ $misscnt.;
run;
3.识别缺失值
知道缺失个数还不够,因为有些数据可以缺失,有些数据是不能够缺失的。如果要更进一步的了解缺失数据的情况,我们可以考虑用put语句。
title "listing of missing values";
data _null_;
file print;
set yuyu
if missing(Visit) then
put "Missing or invalid visit data for ID" Patno;
if missing(HR) then put "Missing or invalid HR or ID" Patno;
if missing(AE) then put "Missing value for ID" Patno;
run;
倘若所有观测都是有标号的,那么你可能还想知道到底是哪个标号数据缺失了,即找到缺失值的ID。
title "Listing of missing patient numbers";
data _null_;
set yuyu;
file print;
Prev_id = lag(Patno);
Prev2_id = lag2(Patno);
if missing(Patno) then put "Missing patient ID. Two previous ID's are:"
Prev2_id "and " Prev_id / @5 "Missing record is number " _n_;
else if notdigit(trim(Patno)) then
put "Invalid patient ID:" patno +(-1)". Two previous ID's are:"
Prev2_id "and " Prev_id / @5 "Missing record is number " _n_;
run;
附:数据yuyu结构类型
参考《cody's data cleaning techniques using sas》
数据,代码出自该书,代码作局部修改