楼主: intheangel
3907 4

[程序分享] 计算字符型变量的信息熵——macro [推广有奖]

  • 3关注
  • 89粉丝

VIP

副教授

25%

还不是VIP/贵宾

-

威望
1
论坛币
49842 个
通用积分
6.1883
学术水平
152 点
热心指数
168 点
信用等级
122 点
经验
12931 点
帖子
480
精华
0
在线时间
475 小时
注册时间
2013-11-22
最后登录
2023-6-28

初级热心勋章

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
有空写了一个宏,可以直接计算字符型变量的信息熵;
raw_data是原始数据集,destination是分类变量;
关于数值型变量的信息熵算法有空继续;

  1. %macro entropy(row_data,destination);

  2. proc contents data=&row_data(drop=&destination) noprint out=var_list;run;

  3. data var_cha_list;
  4. set var_list;
  5. where type=2;
  6. keep name;
  7. run;

  8. /*first we need to get the character variable which we can calculate the entropy*/

  9. %if %sysfunc(exist(var_cha_list)) ne 0 %then %do;
  10. proc datasets lib=work nolist;
  11. delete entropy_variable;
  12. quit;

  13. data entropy_variable;
  14. length variable $40 entropy 8;
  15. stop;
  16. run;
  17. %end;

  18. /*create the table entropy_variable to store the results*/

  19.         %let dsid = %sysfunc(open(var_cha_list));
  20.         %if &dsid gt 0 %then %do;
  21.         %let nobs = %sysfunc(attrn(&dsid,nobs));
  22.         %do i=1 %to &nobs;/*start resolve this question, get the answers*/
  23.                 %let rc = %sysfunc(fetch(&dsid,&i));
  24.                 %let varnume = %sysfunc(varnum(&dsid,name));
  25.                 %let variable = %sysfunc(getvarc(&dsid,&varnume));
  26. ods html close;
  27. ods output
  28. CrossTabFreqs=pro;
  29. proc freq data=&row_data;
  30. table &variable * &destination;
  31. run;
  32. ods output close;
  33. ods html;

  34. /*use the freq proc to summary the data we need*/

  35. data pro_temp(keep=&variable percent);
  36. set pro(keep=&variable percent &destination);
  37. where &destination='' and &variable^='';
  38. run;

  39. data pro_temp_2(keep=&variable rowpercent);
  40. set pro(keep=&variable rowpercent &destination);
  41. where &destination^='' and &variable^='';
  42. run;

  43. proc sql;
  44. create table final as
  45. select * from pro_temp as a
  46. inner join pro_temp_2 as b
  47. on a.&variable.=b.&variable.;
  48. quit;

  49. /*organize the data so we can use it directly*/

  50. data final;
  51. set final;
  52. retain entropy 0;
  53. if rowpercent^=0 and rowpercent^=100 then
  54. entropy=entropy+rowpercent/100*log2(rowpercent/100)*percent/100;
  55. else entropy=entropy;
  56. run;

  57. /*calculate the entropy*/

  58. data entropy_&variable(keep=variable entropy);
  59. retain variable;
  60. set final end=eof;
  61. variable="&variable";
  62. entropy=-entropy;
  63. if eof=1;
  64. run;

  65. proc append base=entropy_variable data=entropy_&variable force;run; /*get all the answer together*/
  66.         %end;
  67.         %let dsid = %sysfunc(close(&dsid));
  68.         %end;

  69. /*Worte by Albert.feng , if you use this code please note.*/
  70. %mend entropy;

  71. %entropy(c,play_golf);
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:字符型变量 Macro Mac CRO acr 信息

我是一只瘦瘦的小猪~~~
╭︿︿︿╮
{/-◎◎-/}
( (oo) )
  ︶︶︶
沙发
intheangel 学生认证  发表于 2014-6-24 21:22:30 |只看作者 |坛友微信交流群
%entropy(c,play_golf);
是我自己的测试,sorry,不该放在上面的

使用道具

藤椅
ynaktank 发表于 2014-6-25 07:50:32 |只看作者 |坛友微信交流群
楼主好人,向你学习啊。

使用道具

板凳
420948492 发表于 2014-6-26 22:52:25 |只看作者 |坛友微信交流群
恩,不错

使用道具

报纸
weifeng9011 发表于 2017-7-27 07:28:43 |只看作者 |坛友微信交流群
谢谢楼主,学习一下

使用道具

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

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-20 12:18