楼主: funwin
1819 9

请教一个 数据清理的问题 [推广有奖]

  • 0关注
  • 2粉丝

硕士生

85%

还不是VIP/贵宾

-

威望
0
论坛币
177 个
通用积分
0
学术水平
0 点
热心指数
4 点
信用等级
0 点
经验
2256 点
帖子
192
精华
0
在线时间
131 小时
注册时间
2007-11-7
最后登录
2016-1-22

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
本人有一个数据库,但数据不是很干净,需要整理一下,原数据大致如下:
companydatetype
1001200101.
1001200102.
10012001033
1001200104.
1001200105.
10012001063
1001200107.
…….
1002200101.
1002200102.
1002200103.
1002200104.
1002200105.
1002200106.
1002200107.
…….
1003200101.
1003200102.
10032001032
1003200104.
1003200105.
10032001063
1003200107.
…….

第一步 对于那些缺失type值的公司,予以去除 (只要该公司所有时间段内,type都是'.',就去除掉,如1002)
第二步 对于那些在某个月有type值的, 就让其他缺失的月份的type值 也和该月一致。(比如1001 有一个月是3,就让其他月分也都是3);
第三步  对于那些在某个时间段变更type值的, 就让type值在变更前,重复第二步工作。如果变更了,从变更之日起后面的缺失的type值也随着变为该值,而且希望把公司名也变更一下;

希望得到如下:
companydatetype
10012001013
10012001023
10012001033
10012001043
10012001053
10012001063
10012001073
…….
10032001012
10032001022
10032001032
10032001042
10032001052
1003_12001063
1003_12001073
…….


由于数据过多,一一更改太过繁琐,是否有高人可以解答一下!
多谢了!
二维码

扫码加我 拉你入群

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

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

关键词:数据清理 company compan type comp 数据 请教

回帖推荐

sushe1527 发表于5楼  查看完整内容

下面的会产生7倍的数据集 极不推荐 等候高人出现~~~~ data a; input a1 a2 a3@@; z=_n_; cards; 1001 200101 . 1001 200102 . 1001 200103 3 1001 200104 . 1001 200105 . 1001 200106 3 1001 200107 . 1002 200101 . 1002 200102 . 1002 200103 . 1002 200104 . 1002 200105 . 1002 200106 . 1002 200107 . 1003 200101 . 1003 200102 . 1003 200103 2 1003 200104 . 1003 200105 . ...

pobel 发表于8楼  查看完整内容

data a; input company $ date type; cards; 1001 200101 . 1001 200102 . 1001 200103 3 1001 200104 . 1001 200105 . 1001 200106 3 1001 200107 . 1002 200101 . 1002 200102 . 1002 200103 . 1002 200104 . 1002 200105 . 1002 200106 . 1002 200107 . 1003 200101 . 1003 200102 . 1003 200103 2 1003 200104 . 1003 200105 . 1003 200106 3 1003 200107 . ;run; ** delete co ...

本帖被以下文库推荐

沙发
wensubin 发表于 2009-9-1 20:26:25 |只看作者 |坛友微信交流群
用EXCEL做,简单。

使用道具

藤椅
Fisher2001 发表于 2009-9-1 20:28:24 |只看作者 |坛友微信交流群
把数据导入EXCEL可以实现。

你提到的第一步,用率选可以实现。
第二步,用条件公式貌似可以实现,但我估计你的数据是动态的。编一段VBA代码可以解决。

使用道具

板凳
funwin 发表于 2009-9-1 20:50:46 |只看作者 |坛友微信交流群
有几百万行,能全导入excel吗?excel好像只能有4万多行
而且用肉眼确实太繁琐了!!!

使用道具

报纸
sushe1527 发表于 2009-9-2 04:53:47 |只看作者 |坛友微信交流群
下面的会产生7倍的数据集 极不推荐 等候高人出现~~~~

data a;
input a1 a2 a3@@;
z=_n_;
cards;
1001 200101 .
1001 200102 .
1001 200103 3
1001 200104 .
1001 200105 .
1001 200106 3
1001 200107 .
1002 200101 .
1002 200102 .
1002 200103 .
1002 200104 .
1002 200105 .
1002 200106 .
1002 200107 .
1003 200101 .
1003 200102 .
1003 200103 2
1003 200104 .
1003 200105 .
1003 200106 3
1003 200107 .
;run;
proc sort data=a;by descending z a1;run;
data b;set a;by descending a1;if last.a1 then z2=0;run;
proc sort data=b;by z;run;
data c;set b;retain type ;
if a3^=. then type=a3;
if z2=0 then type=a3;run;
data d;set c;by z;if last.a1 then z2=0;run;
proc sort data=d;by descending z;run;
data e;set d;retain type1 ;
if type^=. then type1=type;;run;
data f;set a;if a3=. then  delete;
proc sql;create table final as
select a1,a2,type1 as a3 from e where  exists
(select * from f where e.a1=f.a1) order by a1,a2;quit;
已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 3  论坛币 + 3   查看全部评分

使用道具

地板
funwin 发表于 2009-9-2 05:26:19 |只看作者 |坛友微信交流群
5# sushe1527

果然是高手,虽然复杂,但能解决问题。非常感谢!!!

使用道具

7
jingju11 发表于 2009-9-2 06:39:15 |只看作者 |坛友微信交流群
  1. data a;
  2. input company date type@@;
  3. cards;
  4. 1001 200101 .
  5. 1001 200102 .
  6. 1001 200103 3
  7. 1001 200104 .
  8. 1001 200105 .
  9. 1001 200106 3
  10. 1001 200107 .
  11. 1002 200101 .
  12. 1002 200102 .
  13. 1002 200103 .
  14. 1002 200104 .
  15. 1002 200105 .
  16. 1002 200106 .
  17. 1002 200107 .
  18. 1003 200101 .
  19. 1003 200102 .
  20. 1003 200103 2
  21. 1003 200104 .
  22. 1003 200105 .
  23. 1003 200106 3
  24. 1003 200107 .
  25. 1003 200108 4
  26. 1003 200109 .
  27. ;
  28. run;
  29. proc sort data=a; by company; run;

  30. data b;
  31. n+1;
  32. set a;
  33. retain x;
  34. by company;
  35. if first.company then x=type;
  36. if ^missing(type) then x=type;
  37. type1=x;
  38. drop x;
  39. run;
  40. proc sort data=b; by descending n; run;
  41. data c;
  42. set b;
  43. by descending company;
  44. retain y;
  45. if first.company then y=type1;
  46. if ^missing(type1) then y=type1;
  47. type2=y;
  48. if ^missing(type2);
  49. drop y;
  50. run;
  51. proc sort data=c; by n; run;
  52. data d;
  53. length company1 $10.;
  54. set c;
  55. by company;
  56. retain z;
  57. lagCompany=lag(company);
  58. lagType2=lag(type2);
  59. if first.company then z=0;
  60. if company=lagCompany then if lagType2 ^= type2 then z+1;
  61. if z then company1=compress(put(company, 4.)||'_'||put(z, 2.) );
  62. else company1=put(company, 4.);
  63. keep company company1 date type type2;
  64. run;
  65. proc print data=d; run;
复制代码
6# funwin
参考sushe的,增加对company的标识

使用道具

8
pobel 在职认证  发表于 2009-9-2 09:08:57 |只看作者 |坛友微信交流群
data a;
input company $ date type;
cards;
1001 200101 .
1001 200102 .
1001 200103 3
1001 200104 .
1001 200105 .
1001 200106 3
1001 200107 .
1002 200101 .
1002 200102 .
1002 200103 .
1002 200104 .
1002 200105 .
1002 200106 .
1002 200107 .
1003 200101 .
1003 200102 .
1003 200103 2
1003 200104 .
1003 200105 .
1003 200106 3
1003 200107 .
;run;

** delete company with no nonmissing type;
proc sql;
     create table a1 as
          select * from a
          group by company
          having max(type) ne .
          order by company, date;
quit;
     
data a2;
    set a1;
        by company date;
        retain pretype;
        retain chgflag;
        if first.company then do;
            pretype=.;
                        chgflag=0;
        end;
        if ^missing(type) then do;
            if missing(pretype) then pretype=type;
                else if type ne pretype then do;
                                       chgflag+1;
                                                   pretype=type;
                                        end;
        end;
        if missing(type) and ^missing(pretype) then type=pretype;
        if chgflag ne 0 then company=catx('_',company,chgflag);
        drop pretype chgflag;
run;

proc sort data=a2;
     by company descending date;
run;

data a3;
     set a2;
         by company descending date;
         retain nextype;
         if ^missing(type) then nextype=type;
         if missing(type) then type=nextype;
         drop nextype;
run;

proc sort data=a3;
     by company date;
run;
已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 3  论坛币 + 3   查看全部评分

使用道具

9
funwin 发表于 2009-9-2 19:59:10 |只看作者 |坛友微信交流群
多谢7,8楼 让小弟学了不少。

使用道具

10
nernernini 发表于 2009-9-4 02:52:08 |只看作者 |坛友微信交流群
thanks for good codes. learning.

使用道具

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

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

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

GMT+8, 2024-5-15 07:19