-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathInformation_value_with_Weight.sas
551 lines (450 loc) · 16.1 KB
/
Information_value_with_Weight.sas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
options mprint mlogic;
/*******************************************************************************************************************/
/******************* INFO VAL MACRO ********************************************************************************/
/*******************************************************************************************************************/
/*Please note GBI_Tag is dependent variable in this code */
/*Dataset name has to be DEVBASE and dependent variable name has to be GBI_TAG. Please drop indeterminate if any. */
/*remove the variable which is totally missing. then run the below code to replace the missing with -999 */
/*******************************************************************************************************************/
/**************** User Input **************************************************************************************/;
%let out = C:\Users\Lenovo\Downloads\fwdproject\test;
libname local "&out.";
data Dev;
set local.ttd_dev;
run;
%let indata = Dev; /* input data name with libname */
%let libname = work;
%let dep_var = GBI_Tag; /* mention name of dependent variable. without indeterminate */
%let weight = wgt; /* weight variable. if there is no weight then put 1 */
%let output = &out.; /* Output report file with path */
%let delete_all_file = 0; /* 1= delete all files in work, 0=do not delete */
/* Numeric variables list */
%let BinCVarList = ;
%put &BinCVarList;
/* Character variables list */
%let BinDVarList = ;
%put &BinDVarList;
/*******************************************************************************************************************/
/*******************************************************************************************************************/
/**************** Do not change below ******************************************************************************/
/*******************************************************************************************************************/
%Macro Info_val_report();
data DEVBASE /*DEVBASE_Missing*/ ;
set &indata. ;
wgt = &weight.;
GBI_tag = &dep_var.;
/* keep only required variables */
keep GBI_tag wgt &BinCVarList. &BinDVarList.;
run;
/* Missing value treatment: Assign -999 for missing value */
/*PROC STDIZE DATA= DEVBASE_Missing OUT=DEVBASE REPONLY MISSING = -999; VAR _NUMERIC_; RUN;*/
/****************************************************************************************/
proc freq data = DEVBASE noprint;
tables GBI_tag / out = BadsCount;
weight wgt ;
run;
proc sql noprint; select count into:NoofBads from BadsCount where GBI_TAG=1;quit;
proc sql noprint; select count into:NoofGoods from BadsCount where GBI_TAG=0;quit;
%put &NoofBads;
%put &NoofGoods;
%let TotalPop = %SYSEVALF(&NoofGoods + &NoofBads);
%put &TotalPop.;
%let badrate = %SYSEVALF(&NoofBads./&TotalPop. );
%put &badrate.;
/*Info val for Numeric variable:
1. Using proc rank to find maximum 20 bins for each continuous variable
2. Find the infoval of that variable
*/
%macro MakeBinsC(Varname);
/* Raw binning of variable by using proc rank */
proc rank data = DevBase(keep = &varname GBI_tag wgt) out = CoarseRanked groups = 10 ;
var &Varname;
ranks RankVar;
run;
data CoarseRanked;
set CoarseRanked;
_tot_=1;
if RankVar = . then RankVar= -1 ;
run;
/* binwise Total, Min, Max */
proc summary SUM data = CoarseRanked nway;
class RankVar;
weight wgt;
var &Varname GBI_tag _tot_ ;
output out = &varname (Drop = _FREQ_ _TYPE_ )
MIN(&Varname) = Low
MAX(&Varname) = High
SUM(GBI_tag) = BandNoofBads
sum(_tot_) = TotalinBand;
run;
data &varname (drop = Low High rename=(tempLow = Low tempHigh=High));
set &varname;
length tempLow $32.;
length tempHigh $32.;
tempLow = Low||"";
tempHigh = High||"";
run;
data &varname ;
set &varname ;
length variable $32;
Variable = "&varname";
TP = TotalinBand/&TotalPop ;
BandNoofGoods = TotalinBand - BandNoofBads ;
GP = BandNoofGoods/&NoofGoods ;
BP = BandNoofBads/&NoofBads ;
if GP*BP ne 0 then WoE = log((BandNoofGoods/&NoofGoods)/(BandNoofBads/&NoofBads));
else if GP = 0 then WoE = 0;
else WoE = 0;
InfovalBand = (GP - BP)*WoE;
WoeRound = floor(WoE*10);
retain cumm_infoval cum_pct_bad cum_pct_good 0;
cumm_Infoval = cumm_Infoval + InfovalBand;
BadRate = BandNoofBads/TotalinBand;
cum_pct_bad = cum_pct_bad + BP ;
cum_pct_good = cum_pct_good+ GP ;
format KS percent10.4 ;
KS = abs(cum_pct_good - cum_pct_bad);
Length Histogram_WOE $20;
len_woe = abs(round(7*WoE,1))-1;
if len_woe > 14 then len_woe=14;
if woe >0 then do;
if len_woe <0 then Histogram_WOE = "+";
else Histogram_WOE = "+"||repeat("*",len_woe);
end;
else do;
if len_woe < 0 then Histogram_WOE = "+";
else Histogram_WOE = repeat("*",len_woe)||"+";
end;
Length Histogram_BadRate $120;
if &badrate. > 0.3 then do;
len_BadRate = abs(round(10*BadRate,1));
end;
else do;
len_BadRate = abs(round(100*BadRate,1));
end;
if len_BadRate <= 0 then Histogram_BadRate = "+";
else Histogram_BadRate = repeat("*",len_BadRate)||"+";
format Type $4.;
Type ="num";
drop cum_pct_bad cum_pct_good len_woe len_BadRate;
/*BadRateRound = floor(BadRate/5);*/
run;
proc means data = &varname;
var InfovalBand;
output out = inf&varname SUM(InfovalBand) = Infoval;
run;
data inf&varname (drop = _type_ _freq_);
set inf&varname;
length variable $32;
Variable = "&varname";
run;
%mend MakeBinsC;
/*
Info val for char variable:
1. Form bins for each discrete variable (no. of bins = no. of distinct values
2. Find the infoval of that variable
*/
%macro MakeBinsD(Varname);
%global VarType;
proc sort data = DevBase(keep = &varname GBI_tag wgt) out = CoarseRanked;
by &varname;
run;
data CoarseRanked (drop = Pr&varname);
set CoarseRanked;
_tot_=1;
if _n_ = 1 then RankVar = 0;
else if &varname ne Pr&varname then RankVar = RankVar + 1;
else RankVar = RankVar;
Pr&varname = &varname;
retain Pr&varname RankVar;
run;
PROC SQL;
CREATE TABLE ToFindType AS
SELECT
NAME,
TYPE
FROM DICTIONARY.COLUMNS
WHERE LIBNAME=%UPCASE("Work") AND MEMNAME=%UPCASE("CoarseRanked");
QUIT;
data TempToFindType;
set ToFindType;
if upcase(NAME) = "&varname" then call symput('VarType',type);
run;
%put &VarType;
%if &VarType = "num" %then %do;
proc summary SUM data = CoarseRanked nway;
class RankVar;
weight wgt;
var &Varname GBI_tag _tot_ ;
output out = &varname (Drop = _FREQ_ _TYPE_)
MIN(&Varname) = Low
MAX(&Varname) = High
SUM(GBI_tag) = BandNoofBads
SUM(_tot_) = TotalinBand;
run;
data &varname (drop = Low High rename=(tempLow = Low tempHigh=High));
set &varname;
length tempLow $32.;
length tempHigh $32.;
tempLow = Low||"";
tempHigh = High||"";
run;
%end;
%else %do;
data TempCoarseRanked(keep = Low High RankVar );
set CoarseRanked;
by RankVar;
length Low $32.;
length High $32.;
if first.RankVar;
Low = &varname;
High = &varname;
run;
proc summary SUM data = CoarseRanked nway;
class RankVar;
var GBI_tag _tot_ ;
weight wgt;
output out = &varname ( Drop = _FREQ_ _TYPE_ )
SUM(GBI_tag) = BandNoofBads
SUM(_tot_) = TotalinBand;
run;
data &varname;
merge &varname TempCoarseRanked;
by RankVar;
run;
%end;
data &varname ;
set &varname ;
length variable $32;
Variable = "&varname";
TP = TotalinBand/&TotalPop;
BandNoofGoods = TotalinBand - BandNoofBads;
GP = BandNoofGoods/&NoofGoods;
BP = BandNoofBads/&NoofBads;
if GP*BP ne 0 then WoE = log((BandNoofGoods/&NoofGoods)/(BandNoofBads/&NoofBads));
else if GP = 0 then WoE = 0;
else WoE = 0;
InfovalBand = (GP - BP)*WoE;
WoeRound = floor(WoE*10);
retain cumm_infoval cum_pct_bad cum_pct_good 0;
cumm_Infoval + InfovalBand;
BadRate = BandNoofBads/TotalinBand;
cum_pct_bad = cum_pct_bad + BP ;
cum_pct_good = cum_pct_good+ GP ;
format KS percent10.4 ;
KS = abs(cum_pct_good - cum_pct_bad);
Length Histogram_WOE $20;
len_woe = abs(round(7*WoE,1))-1;
if len_woe > 14 then len_woe=14;
if len_woe > 14 then len_woe=14;
if woe >0 then do;
if len_woe <0 then Histogram_WOE = "+";
else Histogram_WOE = "+"||repeat("*",len_woe);
end;
else do;
if len_woe < 0 then Histogram_WOE = "+";
else Histogram_WOE = repeat("*",len_woe)||"+";
end;
Length Histogram_BadRate $120;
if &badrate. > 0.3 then do;
len_BadRate = abs(round(10*BadRate,1));
end;
else do;
len_BadRate = abs(round(100*BadRate,1));
end;
if len_BadRate <= 0 then Histogram_BadRate = "+";
else Histogram_BadRate = repeat("*",len_BadRate)||"+";
format Type $4.;
Type ="char";
drop cum_pct_bad cum_pct_good len_woe len_BadRate;
/* BadRateRound = floor(BadRate/5);*/
run;
proc means data = &varname;
var InfovalBand;
output out = inf&varname SUM(InfovalBand) = Infoval;
run;
data inf&varname(drop = _type_ _freq_);
set inf&varname;
length variable $32;
Variable = "&varname";
run;
%mend MakeBinsD;
%macro CallMakeBinsWithList;
%let j = 1;
%do %while(%scan(&BinCVarList, &j) ne %str());
%let var = %scan(&BinCVarList, &j);
%MakeBinsC(&var);
%put &var;
%let j = %eval(&j+1);
%end;
%put &j;
%let j = 1;
%do %while(%scan(&BinDVarList, &j) ne %str());
%let var = %scan(&BinDVarList, &j);
%MakeBinsD(&var);
%put &var;
%let j = %eval(&j+1);
%end;
%put &j;
%mend CallMakeBinsWithList;
%CallMakeBinsWithList;
%let BinVarList = &BinCVarList &BinDVarList;
/*
Append the datasets created for each variable which have GP,BP,bandinfoval etc.
Also append the datasets created for each variable that have the variable name and its infoval
*/
%macro ForAppending;
%let i = 1;
%do %while(%scan(&BinVarList, &i) ne %str());
%let var = %scan(&BinVarList, &i);
&var
%let i = %eval(&i+1);
%end;
%put &i;
%mend ForAppending;
%macro ForAppendingInfoval;
%let i = 1;
%do %while(%scan(&BinVarList, &i) ne %str());
%let var = %scan(&BinVarList, &i);
inf&var
%let i = %eval(&i+1);
%end;
%put &i;
%mend ForAppendingInfoval;
/*****************************************************************************************************************/;
/*****************************************************************************************************************/;
/* Information value at bin level */
data appended;
set %ForAppending;
run;
proc sql;
create table cont as
select
name,
label
/* type*/
from dictionary.columns
where upcase(libname)=upcase("&libname.") and upcase(memname)=upcase("&indata.");
quit;
proc sql;
create table Bin_Infoval as
select
a.RankVar as Rank ,
a.variable as variable ,
a.Low as Low ,
a.High as High ,
a.TotalinBand as Total ,
a.BandNoofGoods as Good ,
a.BandNoofBads as Bad ,
a.BadRate as Bad_rate format percent10.3 ,
a.WoE as WoE format 20.8 ,
a.BP as Pct_Bad format percent10.4 ,
a.TP as Pct_Total format percent10.4 ,
a.GP as Pct_Good format percent10.4 ,
a.InfovalBand as Bin_Info_Val format percent10.8 ,
a.KS as KS ,
a.Histogram_WOE as Histogram_WOE ,
a.Histogram_BadRate as Histogram_BadRate ,
b.label as Label ,
a.type as Type
from appended as a
left join cont as b on upcase(a.variable) = upcase(b.name)
order by variable, RankVar ;
quit;
proc sql; create table mx_ks as select distinct variable, max(ks) as KS from Bin_Infoval group by variable; quit;
/* SAS code*/
filename woe "&output.\woe_code.sas";
data _null_;
set Bin_Infoval;
file woe;
variablew=trim(variable)||"W";
if _n_=1 then do ;
put '/************--------------- WEIGHT-OF-EVIDENCE CODES FOR ------------------*************************/' ;
put ' ';
put '/**Chech the SAS code for scientific no.(Minimum/Maximum). update the number from excel output**/' ;
put '/****************************************************************************************************/';
put ' ';
end ;
If type = "num" then do;
put @2 " IF " @10 Low @20 " <= " @30 variable @65 " <= " @75 High @90 " THEN " @100 variablew @130 " = " @135 WoE @155 " ; " ;
end;
run;
data _null_;
set Bin_Infoval;
file woe mod;
variablew =trim(variable)||"W";
Highw ="'"||trim(High)||"'";
If type = "char" then do;
put @2 " IF " @10 variable @45 " in ( " @53 Highw @90 " ) THEN " @100 variablew @130 " = " @135 WoE @155 " ; " ;
end;
run;
filename ca "&output.\ca_code.sas";
data _null_;
set Bin_Infoval;
file ca;
variablew =trim(variable)||"B";
if _n_=1 then do ;
put '/*************----------------- Characteristic Analysis CODES FOR ------------***********************/' ;
put ' ';
put '/**Chech the SAS code for scientific no.(Minimum/Maximum). update the number from excel output**/' ;
put '/****************************************************************************************************/';
put ' ' ;
end ;
If type = "num" then do;
put @2 " IF " @10 Low @20 " <= " @30 variable @65 " <= " @75 High @90 " THEN " @100 variablew @130 " = " @135 Rank @155 " ; " ;
end;
run;
data _null_;
set Bin_Infoval;
file ca mod;
variablew =trim(variable)||"b";
Highw ="'"||trim(High)||"'";
If type = "char" then do;
put @2 " IF " @10 variable @45 " in ( " @53 Highw @90 " ) THEN " @100 variablew @130 " = " @135 Rank @155 " ; " ;
end;
run;
/* Information value at overall level */
data AllInfoVals;
set %ForAppendingInfoval;
run;
proc sql;
create table overall_infoval as
select
a.variable,
a.Infoval as Overall_Info_Val format percent10.4,
b.KS as KS format percent10.4
from AllInfoVals as a
left join mx_ks as b on upcase(a.variable) = upcase(b.variable)
order by Infoval desc;
quit;
/*****************************************************************************************************************/;
/****Export appended and Allinfovals to excel and study trend ********/
/*
ods tagsets.excelxp file="&output." style=statistical options(sheet_name='overall_infoval' sheet_interval='Proc');
*/
ods html file = "&output.\overall_infoval.xls";
proc print data = overall_infoval; run;
ods html close;
/*
ods tagsets.excelxp style=statistical options(sheet_name='Bin_Infoval' sheet_interval='Proc');
*/
ods html file = "&output.\Bin_Infoval.xls";
proc print data = Bin_Infoval; run;
ods html close;
/*ods _all_ close;*/
/*****************************************************************************************************************/;
/******************* Delete all file from work library ***********************************************************/;
%macro del_lib();
%if &delete_all_file. = 1 %then %do;
proc datasets library=work kill; run; quit;
%end;
%mend del_lib;
%del_lib();
/*****************************************************************************************************************/;
/*****************************************************************************************************************/;
%Mend Info_val_report;
/*****************************************************************************************************************/;
/***************** Call Info Val macro ***************************************************************************/;
%Info_val_report;
/*********** END OF INFO VAL MACRO ******************************************************************************/;
/*****************************************************************************************************************/;