楼主: funwin
5321 26

请教 如何补入缺失值? [推广有奖]

11
sushe1527 发表于 2009-12-14 22:20:32
data x;
input gongsi chanpin date : yymmn6. jiage;
format date yymmn6.;
z=year(date)*12+month(date);
cards;
10001 105 199009 5.36
10001 106 199009 6.3
10001 109 199009 5.17
10001 105 199012 3.6
10001 285 199012 5.3
10002 365 199102 6.3
10002 250 199102 4.2
10002 251 199105 2.5
10002 352 199105 8.1
;run;
proc sort data=x;by z;run;
data g;set x;by z;
if last.z then output;
run;
data y;
if last then do;
   set g point=lastrec;
   output;
end;
set g (firstobs=2 rename=(date=end)) end=last nobs=lastrec;
set g (rename=(date=start));
do date=start to end-1;
  output;
end;
drop end start;
run;
data y1;set y;daten=put(date,yymmn6.);run;
proc sort data=y1;by daten jiage;run;
data y2;set y1;by daten jiage;if last.daten then output;run;
proc sort data=y2;by jiage;run;
data y2;set y2;by  jiage;if first.jiage then delete;run;
data final;set x y2;drop z daten;
run;proc sort data=final;by date;run;
proc sql;drop table g, x, y, y1, y2;quit;
proc print data=final;run;

12
jingju11 发表于 2009-12-14 23:40:30
3# funwin


  1. proc sort data = funwin out =fw1; by company product date;
  2. run;
  3. data fw3;
  4. set fw1;
  5. lagCompany = lag(Company); lagDate = lag(Date); lagProduct = lag(product);
  6. lagQuant = lag(quant);
  7. year = substrn(left(put(date,8.)), 1, 4); mon = substrn(left(put(date,8.)), 5);
  8. lagYear = lag(year); lagMon = lag(mon);
  9. output;
  10. if company = lagCompany then do;
  11.   if product ^= lagProduct then
  12.    do i = lagMon+1 to 12;
  13.     date1 = input(cats(lagYear, put(i, z2.)), 8.); quant1 = lagQuant; product1 = lagProduct;
  14.     output;
  15.    end;
  16.   
  17.   else if product = lagProduct then do;
  18.    if year = lagYear then
  19.     do i = lagMon+1 to mon-1;
  20.      date1 = input(cats(lagYear, put(i, z2.)), 8.); quant1 = lagQuant;
  21.      output;
  22.     end;
  23.    
  24.    else if year ^= lagYear then
  25.     do i = lagMon+1 to 12;
  26.      date1 = input(cats(lagYear, put(i, z2.)), 8.); quant1 = lagQuant;
  27.      output;
  28.     end;
  29.    
  30.   end;
  31. end;
  32. run;
  33. data fw4;
  34. set fw3;
  35. date2 = coalesce(date1, date);
  36. quant2 = coalesce(quant1,quant);
  37. Product2 = coalescec(Product1, Product);
  38. keep company product2 date2 quant2;
  39. run;
  40. proc sort data = fw4 out =fw5; by company product2 date2;
  41. run;
复制代码


Rule: For the same company and same product, impute the missing from the start month to 12 except for those exist

13
funwin 发表于 2009-12-15 04:39:21
11# sushe1527

试过了 但是还是不能实现我所需要的!

14
funwin 发表于 2009-12-15 04:45:06
12# jingju11

谢谢你 这个code几乎解决了我的问题, 但唯一的遗憾就在于没有解决 夸年度问题:
比如有的公司 产品 给出的日期是199012 ,199103... 所以 中间缺失的199101 199102 也需要补上,让他们的值 和199012的一样。
您给的code能够解决 在同一年内的问题 ,但没有解决 夸年度的问题,是否需要在fw1 到 fw3的过程中再补充些code?

非常感谢你的帮忙!

15
jingju11 发表于 2009-12-15 07:41:46
14# funwin


  1. proc sort data = funwin out =fw1; by company product date;
  2. run;
  3. data fw2;
  4. set fw1;
  5. lagCompany = lag(Company); lagProduct = lag(product);
  6. lagQuant = lag(quant);
  7. date_=input(put(date, 6.), yymmn6.); year = year(date_); mon = month(date_);
  8. lagDate_ = lag(date_);
  9. lagYear = lag(year); lagMon = lag(mon);
  10. output;
  11. if company = lagCompany then do;
  12.   if product ^= lagProduct then
  13.    do i = lagMon+1 to 12;
  14.     date1 = mdy(i, 1, lagYear);
  15.     quant1 = lagQuant; product1 = lagProduct;
  16.     output;
  17.    end;  
  18.    else if product = lagProduct then
  19.     do i = lagMon+1 to lagMon+intck('month', lagDate_, date_)-1;
  20.       quant1 = lagQuant;
  21.       q = ifn(^mod(i, 12), int(i/12)-1, int(i/12));
  22.       date1 = mdy(i-12*q, 1, lagYear+q);
  23.      output;
  24.     end;
  25. end;
  26. run;
  27. data fw3;
  28. set fw2;
  29. date2 = coalesce(date1, date_);
  30. quant2 = coalesce(quant1,quant);
  31. Product2 = coalescec(Product1, Product);
  32. keep company product2 date2 quant2;
  33. format date2 yymmn6.;
  34. run;
  35. proc sort data = fw3 out =fw4; by company product2 date2;
  36. run;
复制代码

note: in the same company and for the same product, there are no discontinuous (by month) records.

16
funwin 发表于 2009-12-15 11:30:40
15# jingju11

谢谢你再次提供code,虽然不上了丢失的1月 2月的数据,但用了你的code,我发现在部分的1 ,2月补充上的观测值少于上一年12月的。比如 company 100001 199112月有10个观察值,但199201 199202月却只有7个值, 没有完全补充上
是否code还有漏洞?

能否再帮忙看看!?多谢!

17
jingju11 发表于 2009-12-15 21:45:58
16# funwin

Not understood your requirement. I list the part of the results.

Obs    company    Product2     date2      quant2
  1     100001    00282410    199009     5478200
  2     100001    00282410    199010     5478200
  3     100001    00282410    199011     5478200
  4     100001    00282410    199012     6300000
  5     100001    00282410    199101     6300000
  6     100001    00282410    199102     6300000
  7     100001    00282410    199103     7680000
  8     100001    00282410    199104     7680000
  9     100001    00282410    199105     7680000
10     100001    00282410    199106     8280000
11     100001    00282410    199107     8280000
12     100001    00282410    199108     8280000
13     100001    00282410    199109     9159600
14     100001    00282410    199110     9159600
15     100001    00282410    199111     9159600
16     100001    00282410    199112    12398400
17     100001    00282410    199201    12398400
18     100001    00282410    199202    12398400
19     100001    00282410    199203    12200000
20     100001    00282410    199204    12200000
21     100001    00282410    199205    12200000
22     100001    00282410    199206    13090000
23     100001    00282410    199207    13090000
24     100001    00282410    199208    13090000
25     100001    00282410    199209    14690000
26     100001    00282410    199210    14690000
27     100001    00282410    199211    14690000
28     100001    00282410    199212    18228000
29     100001    02581610    199009      633900
30     100001    02581610    199010      633900
31     100001    02581610    199011      633900
32     100001    02581610    199012      618900
33     100001    02581610    199101      618900
34     100001    02581610    199102      618900
35     100001    02581610    199103      866400
36     100001    02581610    199104      866400
37     100001    02581610    199105      866400
38     100001    02581610    199106      675000
39     100001    02581610    199107      675000
40     100001    02581610    199108      675000
41     100001    02581610    199109      675000
42     100001    02581610    199110      675000
43     100001    02581610    199111      675000
44     100001    02581610    199112      675000
45     100001    02660910    199009     5640000
46     100001    02660910    199010     5640000
47     100001    02660910    199011     5640000
48     100001    02660910    199012     6315600
49     100001    02660910    199101     6315600
50     100001    02660910    199102     6315600
51     100001    02660910    199103     7020000
52     100001    02660910    199104     7020000
53     100001    02660910    199105     7020000
54     100001    02660910    199106     7751900
55     100001    02660910    199107     7751900
56     100001    02660910    199108     7751900
57     100001    02660910    199109    10219500
58     100001    02660910    199110    10219500
59     100001    02660910    199111    10219500
60     100001    02660910    199112    12694500
61     100001    02660910    199201    12694500
62     100001    02660910    199202    12694500
63     100001    02660910    199203    12899600
64     100001    02660910    199204    12899600
65     100001    02660910    199205    12899600
66     100001    02660910    199206    17875000
67     100001    02660910    199207    17875000
68     100001    02660910    199208    17875000
69     100001    02660910    199209    19430000
70     100001    02660910    199210    19430000
71     100001    02660910    199211    19430000
72     100001    02660910    199212    21600000
73     100001    02680410    199009     1220000
74     100001    02680410    199010     1220000
75     100001    02680410    199011     1220000
76     100001    02680410    199012     2002500
77     100001    02680410    199101     2002500
78     100001    02680410    199102     2002500
79     100001    02680410    199103     3350000
80     100001    02680410    199104     3350000
81     100001    02680410    199105     3350000
82     100001    02680410    199106     3350000
83     100001    02680410    199107     3350000
84     100001    02680410    199108     3350000
85     100001    02680410    199109     3350000
86     100001    02680410    199110     3350000
87     100001    02680410    199111     3350000
88     100001    02680410    199112     3350000
89     100001    03095410    199106     3012500
90     100001    03095410    199107     3012500
91     100001    03095410    199108     3012500
92     100001    03095410    199109     3075000
93     100001    03095410    199110     3075000
94     100001    03095410    199111     3075000
95     100001    03095410    199112     3810000
96     100001    03095410    199201     3810000
97     100001    03095410    199202     3810000
98     100001    03095410    199203     3390000
99     100001    03095410    199204     3390000
100     100001    03095410    199205     3390000
101     100001    03095410    199206     7545600
102     100001    03095410    199207     7545600
103     100001    03095410    199208     7545600
104     100001    03095410    199209     9590000
105     100001    03095410    199210     9590000
106     100001    03095410    199211     9590000
107     100001    03095410    199212    14250000
108     100001    03189710    199009      402500
...
191     100001    04882510    199212    20655000
192     100001    06805510    199009      536400
193     100001    06805510    199010      536400
194     100001    06805510    199011      536400
195     100001    06805510    199012      566400
196     100001    06805510    199101      566400
197     100001    06805510    199102      566400
198     100001    06805510    199103      690000
199     100001    06805510    199104      690000
200     100001    06805510    199105      690000
201     100001    06805510    199106      802500
202     100001    06805510    199107      802500
203     100001    06805510    199108      802500
204     100001    06805510    199109      802500
205     100001    06805510    199110      802500
206     100001    06805510    199111      802500
207     100001    06805510    199112      802500
208     100001    09959910    199009     2240000
209     100001    09959910    199010     2240000
210     100001    09959910    199011     2240000
211     100001    09959910    199012     2988000
212     100001    09959910    199101     2988000
213     100001    09959910    199102     2988000
214     100001    09959910    199103     3538000
215     100001    09959910    199104     3538000
216     100001    09959910    199105     3538000
217     100001    09959910    199106     3488000
218     100001    09959910    199107     3488000
219     100001    09959910    199108     3488000
220     100001    09959910    199109     3188000
221     100001    09959910    199110     3188000
222     100001    09959910    199111     3188000
223     100001    09959910    199112     3263000
224     100001    09959910    199201     3263000
225     100001    09959910    199202     3263000
226     100001    09959910    199203     3238000
227     100001    09959910    199204     3238000
228     100001    09959910    199205     3238000
229     100001    09959910    199206     3050000
230     100001    09959910    199207     3050000
231     100001    09959910    199208     3050000
232     100001    09959910    199209     2788000
233     100001    09959910    199210     2788000
234     100001    09959910    199211     2788000
235     100001    09959910    199212     2863000
236     100004    00176510    199103     1743900
...

18
funwin 发表于 2009-12-15 22:40:52
17# jingju11

最后一步我按这个排列,公司 日期 产品来排:
proc sort data = fw3 out =fw4;
by company date2 product2;
run;
会发现原来100001公司的199112月原来只有7个观测值(7个产品),但等插入缺失处理后,199112月有10个观测值了。怎么会多了三个出来?后面补充进来的199201 199202月份的又是正确的,只有7个观测值 (和原来的199112月 相同)

19
funwin 发表于 2009-12-15 22:53:07
也许 我没有讲清楚我的要求,比如某公司100001给出了199003 199006 199009 199012 199103..等月份的数据,我希望补入199004 199005 和199003一样, 199007 199008 和199006一样 ......(缺失的月份和上一个月份一样)。 但原来的月份199003 199006 199009 199012 199103 ...数据不需要变。

20
jingju11 发表于 2009-12-15 23:04:47
18# funwin

原因是一些产品没有十二月的数据,因此根据最近的一个月做补充。比如说产品0258610, 只有到6月的数据, 而7-12月便根据六月的数据做了补充

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

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