熱線電話:13121318867

登錄
首頁精彩閱讀sas信用評分之評分卡的生成
sas信用評分之評分卡的生成
2017-05-12
收藏

sas信用評分之評分卡的生成

今天介紹的“信用風險評分卡研究”中的生成評分卡的代碼,哪一章生成評分卡我琢磨了好久,所以我覺得要是有疑惑的可以看下我寫的這篇文章。至于理論的東西我就不說,基本要學評分卡的,這本書都是人手一本啦。
這個代碼分為三部分:1、準備數據集。2、代碼執行 3、生成sas代碼。我今天也會按照這個順序介紹下。
1  準備數據集。
(1)準備一個邏輯庫,最好數據不要太多,可以是本地的也可以是數據庫上的。
(2)生成變量碼表:
    <1>、數值的碼表是長這樣子的:

 
      其中-1000和-999代表的是空值。Bin是分組,ll是下限,ul是上限。一定要這么      命名,不要問為什么      
     <2>、字符的碼表長這樣子:

   全部的變量碼表的命名都是“new_q_lcc_six_map”,就是后面有個 “_map”的后綴。
 (3)準備變量的woe表。在這里強調一下,每個變量都是一張碼表還有一張對應的woe表,woe表長下面這樣子:


生成表的規則是,第一列是變量碼表中的分組,名字是該變量名加上后綴“_b”,第二列就是該分組對應的woe值,命名也是叫woe。Woe值是等下要生成評分的。Woe的命名規則是,該變量名后面加后綴”_woe”。像上面這個數據集,他的數據集名稱就叫“new_q_lsix_cnt_woe”。
準備數據集中的工作就這樣子啦,強調一遍,這些數據集都必須全部放在我剛才說的那個你準備的邏輯庫里面。
2 代碼執行
%macro SCScale(BasePoints, BaseOdds, PDO, M_alpha, M_beta);

%local bb;

%let bb=%sysevalf(&PDO / %sysfunc(log(2)));

%let &M_Beta = &bb;

%let &M_alpha= %sysevalf(&BasePoints - &bb * %sysfunc(log(&BaseOdds)));

%mend;

%macro GenSCDS(ParamDS, Lib, DVName, BasePoints, BaseOdds, PDO, SCDS);


%local alpha beta;

%let alpha=;

%let beta=;

%SCScale(&BasePoints, &BaseOdds, &PDO, alpha, beta);

proc transpose data =&ParamDS out=temp_mpt;

run;

%local Intercept;


data temp_mptc;

 set temp_mpt;

length VarName $32.;

length MapDS  $32.;

length WOEDS $32.;

if _Name_ eq 'Intercept' then do;

  call symput('Intercept', compress(&DVName));

  delete;

  end;

  ix=find(upcase(_Name_),'_WOE')-1;

  if ix >0 then VarName=substr(_Name_,1,ix);

  MapDS=compress(VarName)||'_MAP';

  BinName=compress(VarName)||'_b';

  WOEDS=_Name_;

  Parameter=&DVName;


  if _Name_ ne '_LNLIKE_' and &DVName ne . ;

  keep VarName BinName MapDS WOEDS Parameter;

run;

  %local SCBase;

  %let SCBase = %sysfunc(int(&alpha + &beta * &Intercept));

%local i N;

data _null_;

 set temp_mptc;

  call symput('N',compress(_N_));

run;

%do i=1 %to &N;

 %local V_&i P_&i WOE_&i Map_&i;

%end;

data _null_;

 set temp_mptc;

  call symput('V_'||left(_N_),compress(VarName));

  call symput('B_'||left(_N_),compress(BinName));

  call symput('P_'||left(_N_),compress(Parameter));

  call symput('WOE_'||left(_N_),"&Lib.."||compress(WOEDS));

  call symput('Map_'||left(_N_),"&lib.."||compress(MapDS));

run;

%put &&Map_&i.;

proc sql noprint;

 create table &SCDS (VarName char(80), UL num, LL num,  Points num);

 insert into &SCDS values('_BasePoints_' , 0    , 0     ,  &SCBase);

run; quit;

%do i=1 %to &N;


   data temp1;

     set &&WOE_&i;

         bin=&&B_&i;

         VarName="&&V_&i";

         ModelParameter=&&P_&i;

   run;


   proc sort data=temp1;

    by bin;

   run;

      proc contents data=&&Map_&i out=temp_cont nodetails noprint;

      run;

      %local MapType;

      proc sql noprint;

       select Type into :MapType from temp_cont where upcase(Name)='CATEGORY';

      run; quit;

      %if &MapType =1 %then %do;

       Data &&Map_&i;

        set &&Map_&i;

         N_category=Category;

         drop category;

       run;

      %end;


   proc sort data=&&Map_&i;

    by bin;

   run;


    data temp_v;

     merge temp1 &&Map_&i;

        by bin;

      run;


      proc sort data=temp_v;

       by VarName;

      run;


    proc sort data=&SCDS;

       by VarName;

    run;


    data temp_all;

       merge &&SCDS temp_v;

        by VarName;

      run;


    Data &SCDS;

        set temp_all;

        drop &&B_&i;

      run;


%end;

data &SCDS;

  set &SCDS;

   if VarName = '_BasePoints_' then VarType=0;

   else do;

       Points=-WOE*ModelParameter * &beta ;

        if UL ne . and LL ne . then VarType=1;

        else if N_Category eq . then VarType=2;

        else VarType=3;

      end;


   drop WOE bin ModelParameter;

run;

proc sort data=&SCDS;

 by VarType VarName;

run;

/*proc datasets library=work nodetails;*/

/*delete temp1 temp_all temp_cont temp_mpt temp_mptc temp_v;*/

/*run; quit;*/

%mend;

%GenSCDS(ParamDS=raw.bb, Lib=raw, DVName=appl_status_1, BasePoints=540, BaseOdds=7.5, PDO=20, SCDS=cc);

代碼使用:

%GenSCDS(ParamDS=raw.bb, Lib=raw, DVName=appl_status_1, BasePoints=540, BaseOdds=7.5, PDO=20, SCDS=cc);

ParamDS= 這個數據集就是你執行以下這個過程中outest=產出的數據集,其實就是各個變量的系數。這個說一下(event="1")這個參數,就是假設你的好客戶是1的話,那使用這個outest=產出的數據集計算的評分,就是越高分越是好人。

Ods Output ParameterEstimates=aa ;

proc logistic data=test.RONG_total12 outest=bb ;

model APPL_STATUS_1(event="1")=

*****

/selection=s sle=0.05 sls=0.05 include=12;

output out=pp

    p=pred_status lower=pi_l upper=pi_u;

run;


Lib=填入你第一步哪里我說的準備的邏輯庫。

BasePoints=基礎分,具體的基礎分是什么,書里有詳細的解釋。

BaseOdds=7.5  違約比正常,7.5那就是違約率大概是11%。具體看樣本的壞樣本量。

PDO=20 你想設置的刻度,這個你也自己看書,我不是很能表達這個參數的意義

SCDS=cc 輸出數據集。

輸出的數據集長的是這樣子的:

就是每個變量的區間對應的分數,方便生成sql或者是sas代碼去對初始變量執行。

3 生成sas代碼

%macro SCSasCode(SCDS,BasePoints, BaseOdds, PDO, IntOpt,FileName);


proc sort data=&SCDS;

by VarType VarName;

run;


data _null_;

set &SCDS nobs=nx;

by VarType VarName;

file "&FileName";

length cond $300.;

length value $300.;


if _N_ =1 then do;

put '/*********************************************/' ;

put '/*********************************************/';

put '/***** Automatically Generated Scorecard *****/';

put '/*********************************************/';

      put '/************    SAS CODE             ********/';

      put;

      put '/* Scorecard Scale : */';

put "/*  Odds of [ 1 : &BaseOdds ] at  [ &BasePoints ] Points ";

 put "     with PDO of [ &PDO ] */";

      put;

put '/*********************************************/';

put '/*********************************************/';

      put ;

put '/********** START OF SCORING DATA STEP *******/';

put '/*********************************************/';

put '/*********************************************/';

      put;

put 'DATA SCORING;/********** Modify ************/';

put ' SET ScoringDataset; /********** Modify ************/';

      put;

put '/*********************************************/';

put '/*********************************************/';

end;


/* print the dataset RulesDS */


%if &IntOpt=1 %then xPoints=int(Points);

%else xPoints=Points; ;


if VarName="_BasePoints_" then do;

put '/*********************************************/';

      put "/* Base Points   */";

put '/*********************************************/';

put "Points=" xPoints ";";

                            end;

 else do;

   if first.VarName then do;

put '/*********************************************/';

put "/* Variable : " VarName "    *****/";

put '/*********************************************/';

                      end;

    value= "  THEN  Points=Points +("||compress(xPoints)||");";


    /* The rule */

    if VarType=1 then  do;/* continuous */

      if first.VarName then  cond='IF '||compress(VarName)||' LE ('||compress(UL) || ') ';

      else if last.VarName then cond='IF '||compress(VarName)||' GT ('|| compress(LL)||')';

    else cond='IF '||compress(VarName)||' GT ('|| compress(LL)||') AND '||compress(VarName)||' LE ('||compress(UL) || ') ';

                       end;

    else if VarType=2 then /* nominal string */

      cond = 'IF '||compress(VarName)||' = '|| quote(compress(Category)) ;


      else /* nominal numeric */

      cond='IF '||compress(VarName)||' = ('|| compress(N_Category)||') ';

     

      put "      " cond value;


 end;


 if _N_=Nx then do;

      put 'RUN;';

      put;

put '/*************END OF SCORING DATA STEP *******/';

put '/*********************************************/';

end;

run;

%mend;

%SCSasCode(SCDS=cc,BasePoints=540, BaseOdds=7.5, PDO=20,IntOpt=1,FileName=D:\工作\簡版征信\scorecard.sas);

SCDS=填入你上個代碼生成評分卡的那個輸出數據集。

BasePoints=如上代碼

BaseOdds=如上代碼

PDO=如上代碼

IntOpt=1的意思就是使所有的分值都四舍五入為整數。

FileName 你想把這個代碼放在那里的路徑。

數據分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數據分析師資訊
更多

OK
客服在線
立即咨詢
日韩人妻系列无码专区视频,先锋高清无码,无码免费视欧非,国精产品一区一区三区无码
客服在線
立即咨詢