用于计算IV的SAS代码

时间:2016-07-28 15:15:12

标签: sas

我从obseveupdate websit找到了一些代码。它们用于IV计算。当我运行代码时,它会通过,但所有的IV和Woe都是零。我更改了另一个数据集以尝试,也为所有变量获取零。你能帮我找出原因吗?

data inputdata;
length Region $ 20 age $ 20 Gender $ 20; 
infile datalines dsd dlm= ':' truncover;
input Region $ age $ Gender $ target ;
datalines;
Scotland:18-25:Male:1
Scotland:18-25:Female:0
Scotland:26-35:Male:0
Wales:26-35:Male:1
Wales:36-45:Female:0
Wales:26-35:Male:1
London:36-45:Male:1
London:26-35:Male:0
London:18-25:Unknown:1
London:36-45:Male:0
Northern Ireland:36-45:Female:0
Northern Ireland:26-35:Male:1
Northern Ireland:36-45:Male:0
Engand (Not London):45+:Female:0
Engand (Not London):18-25:Male:1
Engand (Not London):26-35:Female:0
Engand (Not London):45+:Female:0
Engand (Not London):36-45:Female:1
Engand (Not London):45+:Female:1
; 

data _tempdata;
set inputdata;;
n=_n_;
run;


proc sort data=_tempdata;
by target n;
run;



proc transpose data=_tempdata out = _tempdata;
by target n;
var _character_ _numeric_;
run; 


proc sort data=_tempdata out=_tempdata;
by _name_ target;
run; 


proc freq data=_tempdata;
by _name_ target;
tables col1 /out=_tempdata;
run; 

proc sort data=_tempdata;
by _name_ col1;
run;


proc transpose data=_tempdata out=_tempdata;
by _name_ col1;
id target;
var percent;
run; 

data IV_Table(keep=variable IV) WOE_Table(keep=variable attribute woe);
set _tempdata;
by _name_; 

rename col1=attribute _name_=variable;
_0=sum(_0,0)/100; *Convert to percent and convert null to zero;
_1=sum(_1,0)/100; *Convert to percent and convert null to zero; 

woe=log(_0/_1)*100;output WOE_Table;*Output WOE;

if _1 ne 0 and _0 ne 0 then do; 
raw=(_0-_1)*log(_0/_1); 
end;
else raw=0; 

IV+sum(raw,0);*Culmulativly add to IV, set null to zero;

if last._name_ then do; *only _tempdata the last final row;
output IV_table;
IV=0;
end; 

where upcase(_name_) ^='TARGET' and upcase(_name_) ^= 'N';run;


proc sort data=IV_table;by descending IV;run;
title1 "IV Listing";proc print data=IV_table;run;
proc sort data=woe_table;
by variable WOE;
run;

title1 "WOE Listing";
proc print data=WOE_Table;run; 

0 个答案:

没有答案