楼主: funwin
4130 10

[原创博文] 求助 如何能让每家公司的ID都统一为 该公司第一个出现的non-missing ID? [推广有奖]

11
shenliang_111 发表于 2011-9-12 16:54:39
  1. try this
  2. data a;
  3. input company date:yymmn6. id;
  4. format date yymmn6.;
  5. datalines;
  6. 1001 200509 2
  7. 1001 200510 6
  8. 1001 200511 2
  9. 1001 200512 6
  10. 1119 200112 .
  11. 1119 200201 .
  12. 1119 200202 6
  13. 1119 200203 8
  14. ;
  15. run;
  16. /*code 1*/
  17. data bb;
  18. temp=0;
  19. ccc=0;
  20. do _n_=1 by 1 until(last.company);
  21. set a;
  22. by company;
  23. temp=ifn(^missing(id),temp+1,temp);
  24. ccc=ifn(^missing(id) and temp=1,id,ccc);
  25. put temp ccc;
  26. end;
  27. do _n_=1 to _n_;
  28. set a;
  29. if ^missing(id) then id=ccc;
  30. output;
  31. end;
  32. run;
  33. /*code 2*/
  34. data aa;
  35. input company date:yymmn6. id;
  36. format date yymmn6.;
  37. datalines;
  38. 1001 200509 2
  39. 1001 200510 6
  40. 1001 200511 .
  41. 1001 200512 6
  42. 1119 200112 .
  43. 1119 200201 .
  44. 1119 200202 6
  45. 1119 200203 8
  46. ;
  47. run;
  48. data bbb;
  49. temp=0;
  50. ccc=0;
  51. ddd=0;
  52. do _n_=1 by 1 until(last.company);
  53. set a;
  54. by company;
  55. temp=ifn(^missing(id),temp+1,temp);
  56. ccc=ifn(^missing(id) and temp=1,id,ccc);
  57. ddd=ifn(^missing(id) and temp=1,_n_,ddd);
  58. put temp ccc ddd;
  59. end;
  60. do _n_=1 to _n_;
  61. set a;
  62. if ddd eq 1 then do;
  63. id=ccc;
  64. output;
  65. end;
  66. else do;
  67. if _n_ ge ddd  then id=ccc;
  68. else call missing(id);
  69. output;
  70. end;
  71. end;
  72. run;
复制代码

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-28 22:20