SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 1104|回复: 0
打印 上一主题 下一主题

A scorecard for probability of default with sparkline

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2011-6-27 14:45:10 | 只看该作者

A scorecard for probability of default with sparkline

From Dapangmao's blog on sas-analysis

<div class="separator" style="clear: both; text-align: center;"><a href="http://3.bp.blogspot.com/-hH2OSzvV_JY/TeVKG9SsM1I/AAAAAAAAAlk/NTpQA4qJikY/s1600/plot1.jpg" imageanchor="1" style="margin-left:1em; margin-right:1em"><img border="0" height="300" width="400" src="http://3.bp.blogspot.com/-hH2OSzvV_JY/TeVKG9SsM1I/AAAAAAAAAlk/NTpQA4qJikY/s400/plot1.jpg" /></a></div><br />
In the 1st chapter of their must-read credit risk modeling book, Gunter and Peter used the ratios of working capital(WC), retained earnings(RE), earnings before interest and taxes(EBIT),  sales(S) and market value of equity(ME)  over either total liabilities (TL) or total assets(TA), to build a logit default risk model for 4000 records by 791 firms through 10 years [Ref.1].  The authors implemented VBA’s user-defined functions in Excel to realize the modeling and scoring procedures. In SAS, Proc Logistic does the same job. <br />
<br />
Macros in SAS and Excel can be used together. Excel2010’ new Sparkline functions visualize the fluctuations of values in many rows. For those firms which eventually didn’t default in Gunter and Peter’s example, Sparkline can help observe the changing pattern of default risk for specific firms, such as which year the firms’ maximum default risk occur. Collin and Eli disclosed some definitions to translate a SAS dataset to an Excel table [Ref. 2]. Thus, by using their method, a SAS macro that generates VBA code will automate this process for a default risk scorecard with Sparkline. <br />
<br />
References:<br />
1. Gunter Löeffler and Peter Posch. ‘Credit Risk Modeling using Excel and VBA’. The 2nd edition. Wiley. <br />
2. Collin Elliot and Eli Morris. ‘Excel lent SAS Formulas:  The Creation and Export of Excel Formulas Using SAS’ . SAS Global 2009. <br />
<br />
<pre style="background-color: #ebebeb; border: 1px dashed rgb(153, 153, 153); color: #000001; font-size: 14px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>/*******************READ ME*********************************************
* - A scorecard for probability of default(PD) with sparkline -
*
* SAS VERSION:    9.1.3
* EXCEL VERSION:  2010
* DATE:           31may2011
* AUTHOR:         <!-- e --><a href="mailto:hchao8@gmail.com">hchao8@gmail.com</a><!-- e -->
*
****************END OF READ ME******************************************/

%macro splscd(data = , path = , filename = );
   /*****************************************************************
   *  MACRO:      splscd()
   *  GOAL:       create xls and vba for a scorecard with sparkline
   *  PARAMETERS: data     = dataset for modeling and scoring
   *              path     = output path
   *              filename = name for scorecard
   *****************************************************************/
   options mprint mlogic;
   data _excelCol(where = (start le 256));  
      length label $2;  
      do c1 = -1 to 25;  
         do c2 = 0 to 25;  
            alpha1 = byte(rank('A')+ c1);  
            alpha2 = byte(rank('A')+ c2);  
            label = compress(alpha1||alpha2, " @");  
            start + 1;  
            fmtName = "column";  
            output;  
         end;  
      end;  
   run;

   proc format cntlin = _excelCol;
   run;

   proc logistic data = &data;
      model default = WCoverTA REoverTA EBIToverTA MEoverTL SoverTA;
      score data = &data out = _scored;
   run;

   data _tmp01;
      set _scored;
      where default = 0;
      keep id year p_1;
   run;

   proc sort data = _tmp01;
      by id year;
   run;

   proc transpose data = _tmp01 out = _tmp02(keep = id year:) prefix = year;
      by id;
      id year;
      var p_1;
   run;

   ods listing close;
   ods output variables = _vartab;
   proc contents data = _tmp02;
   run;

   proc sql;
      select variable into: varlist separated by ' '
      from _vartab
      order by substr(variable, 5, 4)
      ;
      select count(*) format = column. into: col_num
      from _vartab
      ;
      select count(*)+1 format = column. into: spl_col
      from _vartab
      ;
   quit;

   data _tmp03;
      retain &varlist;
      set _tmp02 nobs = nobs;
      sparkline =.;
      call symput('nobs_plus', nobs + 1);
   run;

   ods html file = "&path\&filename..xls" style = minimal;
   option missing = '';
   title; footnote;
   proc print data = _tmp03 noobs;
   run;
   ods html close;

   proc sql;
      create table _tmp04 (string char(200));
      insert into _tmp04
      values("Sub sas2vba()")
      values("''''''''CREATE SPARKLINE'''''''''")
      values('Columns("spl_col:spl_col").ColumnWidth=30')
      values("Dim mySG As SparklineGroup")
      values('Set mySG = _ ')
      values('Range("$spl_col$2:$spl_col$nobs_plus").SparklineGroups.Add(Type:=xlSparkColumn, SourceData:="B2:col_numnobs_plus")')
      values('mySG.SeriesColor.ThemeColor=6')
      values("mySG.Points.Highpoint.Visible=True")
      values("''''''''FORMAT THE TABLE'''''''''")
      values('ActiveSheet.ListObjects.Add(xlSrcRange,Range("$A$1:$spl_col$nobs_plus"), , xlYes).Name="myTab"')
      values('Range("myTab[#All]").Select')
      values('ActiveSheet.ListObjects("myTab").TableStyle="TableStyleMedium1"')
      values("''''''''SAVE AS EXCEL2010 FORMAT'''''''''")
      values('ChDir "sas_path"')
      values('ActiveWorkbook.SaveAs Filename:="sas_path\excel_file.xlsx",FileFormat:=xlOpenXMLWorkbook,CreateBackup:=False')
      values("End Sub")
      ;
   quit;

   data _tmp05;
      set _tmp04;
      string = tranwrd(string, "spl_col", "&spl_col");
      string = tranwrd(string, "nobs_plus", "&nobs_plus");
      string = tranwrd(string, "col_num", "&col_num");
      string = tranwrd(string, "sas_path", "&path");
      string = tranwrd(string, "excel_file", "&filename");
      if _n_ in (3, 6, 10) then string = compress(string, ' ');
   run;

   data _null_;
      set _tmp05;
      file "&path\&filename..bas";
      put string;
   run;

   proc datasets nolist;
      delete _:;
   quit;
%mend splscd;

%splscd(data = test1, path = c:\tmp, filename = scorecard);</code></pre><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3256159328630041416-332482454846286199?l=www.sasanalysis.com' alt='' /></div><img src="http://feeds.feedburner.com/~r/SasAnalysis/~4/1FHbRSzfbhM" height="1" width="1"/>
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|SAS中文论坛  

GMT+8, 2025-6-10 03:25 , Processed in 0.068144 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表