楼主: playmore
1558 4

[SAS] SAS基础宏之8:GetMissNum [推广有奖]

已卖:1645份资源

学科带头人

2%

还不是VIP/贵宾

-

TA的文库  其他...

R相关

经济学相关

金融工程

威望
1
论坛币
16356 个
通用积分
8.6697
学术水平
372 点
热心指数
394 点
信用等级
341 点
经验
15297 点
帖子
1194
精华
1
在线时间
1332 小时
注册时间
2007-1-11
最后登录
2025-12-1

初级学术勋章 初级热心勋章 中级热心勋章

楼主
playmore 发表于 2015-2-13 15:41:40 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币

宏解释+源代码


1、宏解释






      这个宏用来统计数据集中指定变量的缺失值数量,支持数值变量和字符变量
      最后都汇到一张结果表格中

      之前我用的是把表格转置后,用nmiss和cmiss求横向的数组的缺失值
      这样代码比较简单,但是后来发现proc transpose实在是太慢了,而且耗资源
      这下改了下,直接用sql对列进行计算了,不需要转置了


2、源代码




  1. %macro GetMissNum(SourceTable,TargetTable,InputVar);
  2. /**********************************************************************/
  3. /* 此宏的作用是统计原表中不同变量的缺失值数量。其中SourceTable是原始 */
  4. /* 表格,SourceTable是结果表格;TargetTable是结果表格;InputVar是原始 */
  5. /* 表格中的变量,可设多个变量,用空格分隔,也可如下设置:=_Numeric_表 */
  6. /* 示统计全部数值型变量,=_Character_表示统计全部字符型变量,=_All_表 */
  7. /* 示统计全部变量。 */
  8. /* */
  9. /* 最终结果表格中包含所有指定变量的名称、类型和相应的缺失值数量。 */
  10. /* */
  11. /* Created on 2013.5.8 */
  12. /* Modified on 2013.5.8 */
  13. /**********************************************************************/

  14. /* 检查TargetTable的存在性 */
  15. %if &TargetTable EQ %STR() %then %do;
  16. %put ERROR: The TargetTable should not be blank, please check it again.;
  17. %goto exit;
  18. %end;

  19. /* 检查InputVar的合法性 */
  20. %if %UPCASE(&InputVar) NE _NUMERIC_ AND %UPCASE(&InputVar) NE _CHARACTER_ AND %UPCASE(&InputVar) NE _ALL_ %then %do;
  21. %ChkVar(SourceTable=&SourceTable,InputVar=&InputVar,FlagVarExists=GMN_FlagVarExists);

  22. %if %SYSFUNC(FIND(&GMN_FlagVarExists,0)) NE 0 %then %do;
  23. %put ERROR: The InputVar should be _Numeric_, _Character_, _All_ or any variable name in SourceTable, case insensitive and without quotes.;
  24. %put ERROR: The InputVar "%SCAN(&InputVar,%SYSFUNC(FIND(&GMN_FlagVarExists,0)))" does not exist in SourceTable, please check it again.;
  25. %goto exit;
  26. %end;
  27. %end;

  28. %if %UPCASE(&InputVar) EQ _NUMERIC_ %then %do;
  29. proc contents data=&SourceTable position out=GMN_VarList(keep=name type varnum) noprint;
  30. run;

  31. /* 检查数值型变量是否存在 */
  32. %ChkValue(SourceTable=GMN_VarList,
  33. InputVar=type,
  34. Value=1,
  35. FlagValueExists=GMN_FlagNumVarExists);

  36. %if &GMN_FlagNumVarExists GT 0 %then %do;
  37. proc sql noprint;
  38. select NAME into :GMN_NumVarList separated by ' '
  39. from GMN_VarList
  40. where TYPE EQ 1;
  41. quit;

  42. /* 化简原始表格 */
  43. data &TargetTable(keep=&GMN_NumVarList drop=GMN_i);
  44. set &SourceTable;
  45. array VarList &GMN_NumVarList;
  46. do GMN_i=1 to dim(VarList);
  47. if VarList{GMN_i} NE . then VarList{GMN_i}=0;
  48. else VarList{GMN_i}=1;
  49. end;
  50. run;

  51. %GetStatsForTable(SourceTable=&TargetTable,
  52. TargetTable=&TargetTable,
  53. ByFactors=,
  54. InputVar=&GMN_NumVarList,
  55. InputVarType=,
  56. OutputVarType=,
  57. Weight=,
  58. Statistic=SUM);

  59. proc transpose data=&TargetTable out=&TargetTable;
  60. var &GMN_NumVarList;
  61. run;

  62. data &TargetTable(rename=(_NAME_=VarName _LABEL_=VarLabel COL1=MissNum));
  63. retain _NAME_ _LABEL_ VarType COL1;
  64. set &TargetTable;
  65. VarType='N';
  66. run;
  67. %end;
  68. %else %do;
  69. %put ERROR: There is no numeric variable existed in SourceTable, please check it again.;
  70. %goto exit;
  71. %end;
  72. %end;
  73. %else %if %UPCASE(&InputVar) EQ _CHARACTER_ %then %do;
  74. proc contents data=&SourceTable position out=GMN_VarList(keep=name type varnum) noprint;
  75. run;

  76. /* 检查字符型变量是否存在 */
  77. %ChkValue(SourceTable=GMN_VarList,
  78. InputVar=type,
  79. Value=2,
  80. FlagValueExists=GMN_FlagCharVarExists);

  81. %if &GMN_FlagCharVarExists GT 0 %then %do;
  82. proc sql noprint;
  83. select NAME,STRIP(NAME)||'_Temp' into :GMN_CharVarList separated by ' ',:GMN_CharVarList_Temp separated by ' '
  84. from GMN_VarList
  85. where TYPE EQ 2;
  86. quit;

  87. /* 化简原始表格 */
  88. data &TargetTable(keep=&GMN_CharVarList_Temp drop=GMN_j);
  89. set &SourceTable;
  90. array VarList &GMN_CharVarList;
  91. array VarList_Temp &GMN_CharVarList_Temp;
  92. do GMN_j=1 to dim(VarList);
  93. if VarList{GMN_j} NE "" then VarList_Temp{GMN_j}=0;
  94. else VarList_Temp{GMN_j}=1;
  95. end;
  96. run;

  97. %GetStatsForTable(SourceTable=&TargetTable,
  98. TargetTable=&TargetTable,
  99. ByFactors=,
  100. InputVar=&GMN_CharVarList_Temp,
  101. InputVarType=,
  102. OutputVarType=,
  103. Weight=,
  104. Statistic=SUM);

  105. proc transpose data=&TargetTable out=&TargetTable;
  106. var &GMN_CharVarList_Temp;
  107. run;

  108. data &TargetTable(rename=(_NAME_=VarName _LABEL_=VarLabel COL1=MissNum));
  109. retain _NAME_ _LABEL_ VarType COL1;
  110. set &TargetTable;
  111. VarType='C';
  112. _NAME_=SUBSTR(_NAME_,1,LENGTH(_NAME_)-5);
  113. run;
  114. %end;
  115. %else %do;
  116. %put ERROR: There is no character variable existed in SourceTable, please check it again.;
  117. %goto exit;
  118. %end;
  119. %end;
  120. %else %do;
  121. proc contents data=&SourceTable position out=GMN_VarList(keep=name type varnum) noprint;
  122. run;

  123. /* 筛选指定的变量 */
  124. %if %UPCASE(&InputVar) NE _ALL_ %then %do;
  125. %let InputVar_Comma=%PrxChange(InputString=&InputVar,PrxString=s/(\w+)/'$1'/); /* 给InputVar中的代码加引号 */
  126. %let InputVar_Comma=%SYSFUNC(TRANSLATE(&InputVar_Comma,%STR(,),%STR( ))); /* 替换InputVar中的空格为逗号 */

  127. proc sql noprint;
  128. create table GMN_VarList as
  129. select * from GMN_VarList
  130. where Name in (&InputVar_Comma)
  131. order by Name;
  132. quit;
  133. %end;

  134. /* 检查数值型变量是否存在 */
  135. %ChkValue(SourceTable=GMN_VarList,
  136. InputVar=type,
  137. Value=1,
  138. FlagValueExists=GMN_FlagNumVarExists);

  139. /* 检查字符型变量是否存在 */
  140. %ChkValue(SourceTable=GMN_VarList,
  141. InputVar=type,
  142. Value=2,
  143. FlagValueExists=GMN_FlagCharVarExists);

  144. %if &GMN_FlagNumVarExists GT 0 %then %do;
  145. proc sql noprint;
  146. select NAME into :GMN_NumVarList separated by ' '
  147. from GMN_VarList
  148. where TYPE EQ 1;
  149. quit;

  150. /* 化简原始表格 */
  151. data GMN_NumMiss(keep=&GMN_NumVarList drop=GMN_k);
  152. set &SourceTable;
  153. array VarList &GMN_NumVarList;
  154. do GMN_k=1 to dim(VarList);
  155. if VarList{GMN_k} NE . then VarList{GMN_k}=0;
  156. else VarList{GMN_k}=1;
  157. end;
  158. run;

  159. %GetStatsForTable(SourceTable=GMN_NumMiss,
  160. TargetTable=GMN_NumMiss,
  161. ByFactors=,
  162. InputVar=&GMN_NumVarList,
  163. InputVarType=,
  164. OutputVarType=,
  165. Weight=,
  166. Statistic=SUM);

  167. proc transpose data=GMN_NumMiss out=GMN_NumMiss;
  168. var &GMN_NumVarList;
  169. run;

  170. data GMN_NumMiss(rename=(_NAME_=VarName _LABEL_=VarLabel COL1=MissNum));
  171. retain _NAME_ _LABEL_ VarType COL1;
  172. set GMN_NumMiss;
  173. VarType='N';
  174. run;
  175. %end;
  176. %if &GMN_FlagCharVarExists GT 0 %then %do;
  177. proc sql noprint;
  178. select NAME,STRIP(NAME)||'_Temp' into :GMN_CharVarList separated by ' ',:GMN_CharVarList_Temp separated by ' '
  179. from GMN_VarList
  180. where TYPE EQ 2;
  181. quit;

  182. /* 化简原始表格 */
  183. data GMN_CharMiss(keep=&GMN_CharVarList_Temp drop=GMN_l);
  184. set &SourceTable;
  185. array VarList &GMN_CharVarList;
  186. array VarList_Temp &GMN_CharVarList_Temp;
  187. do GMN_l=1 to dim(VarList);
  188. if VarList{GMN_l} NE "" then VarList_Temp{GMN_l}=0;
  189. else VarList_Temp{GMN_l}=1;
  190. end;
  191. run;

  192. %GetStatsForTable(SourceTable=GMN_CharMiss,
  193. TargetTable=GMN_CharMiss,
  194. ByFactors=,
  195. InputVar=&GMN_CharVarList_Temp,
  196. InputVarType=,
  197. OutputVarType=,
  198. Weight=,
  199. Statistic=SUM);

  200. proc transpose data=GMN_CharMiss out=GMN_CharMiss;
  201. var &GMN_CharVarList_Temp;
  202. run;

  203. data GMN_CharMiss(rename=(_NAME_=VarName _LABEL_=VarLabel COL1=MissNum));
  204. retain _NAME_ _LABEL_ VarType COL1;
  205. set GMN_CharMiss;
  206. VarType='C';
  207. _NAME_=SUBSTR(_NAME_,1,LENGTH(_NAME_)-5);
  208. run;
  209. %end;

  210. data &TargetTable;
  211. set
  212. %if &GMN_FlagNumVarExists GT 0 %then %do;
  213. GMN_NumMiss
  214. %end;
  215. %if &GMN_FlagCharVarExists GT 0 %then %do;
  216. GMN_CharMiss
  217. %end;
  218. ;
  219. run;
  220. %end;

  221. /* 删除不必要的表格 */
  222. proc datasets lib=work nolist;
  223. delete GMN_:;
  224. quit;

  225. %exit:
  226. %mend;


  227. %macro Demo();

  228. %let SourceTable=Cars;
  229. %let TargetTable=MissNum;
  230. %let InputVar=Cylinders; /* =_Numeric_表示统计全部数值型变量,=_Character_表示统计全部字符型变量,=_All_表示统计全部变量,大小写不敏感 */
  231. %GetMissNum(&SourceTable,&TargetTable,&InputVar);

  232. %mend;
复制代码

二维码

扫码加我 拉你入群

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

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

关键词:sas基础 Miss Get Iss MIS 基础 基础

已有 4 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
niuniuyiwan + 60 + 60 + 5 + 5 + 5 精彩帖子
fantuanxiaot + 5 + 1 + 1 + 1 精彩帖子
mycpcw + 5 + 3 + 3 + 3 精彩帖子
我的素质低 + 20 精彩帖子

总评分: 经验 + 65  论坛币 + 85  学术水平 + 9  热心指数 + 9  信用等级 + 9   查看全部评分

playmore邀请您访问ChinaTeX论坛!!!进入ChinaTeX论坛

沙发
mycpcw 发表于 2015-2-13 15:51:54
学习了

藤椅
narutohokage 发表于 2015-6-14 15:18:30
楼主,请问日期时间型变量用这个宏可以识别出来嘛?

板凳
narutohokage 发表于 2015-6-14 16:10:45
为什么我运行之后总是报这个错误?
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: &GSFT_StatGroup1 EQ 0 AND &GSFT_StatGroup2a EQ 0 AND
       &GSFT_StatGroup2b EQ 0 AND &GSFT_StatGroup3 EQ 0 AND &GSFT_StatGroup4 EQ 0

报纸
bitcoin 发表于 2016-1-30 15:41:32
243、246行貌似有错误

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

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-31 01:14