楼主: funwin
3103 11

[原创博文] 如何将有缺失的月份的下一个月份的数据也变为缺失值? [推广有奖]

  • 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 论坛币
公司001200103月没有销售数据,所以为缺失值,200104月份却有了两个月的加总数据(所以不准确),想把200104月的数据也变为缺失值,以便于后面删除。002公司也一样,想把200309月的数据也变为缺失值。总的想法就是:如何将有缺失的月份的下一个月份的数据也变为缺失值?

company
month
sales
001
200101
700
001
200102
650
001
200103
.
001
200104
1500
001
200105
850
……..
…….
……
002
200305
.
002
200306
.
002
200307
.
002
200308
.
002
200309
3600
002
200310
1200
……..
…….
……

变为:
company
month
sales
001
200101
700
001
200102
650
001
200103
.
001
200104
.
001
200105
850
……..
…….
……
002
200305
.
002
200306
.
002
200307
.
002
200308
.
002
200309
.
002
200310
1200
……..
…….
……




company month sales 001 200101 700 001 200102 650 001 200103 . 001 200104 1500 001 200105 850 …….. ……. …… 002 200305 . 002 200306 . 002 200307 . 002 200308 . 002 200309 3600 002 200310 1200 …….. ……. …… 变为: company month sales 001 200101 700 001 200102 650 001 200103 . 001 200104 . 001 200105 850 …….. ……. …… 002 200305 . 002 200306 . 002 200307 . 002 200308 . 002 200309 . 002 200310 1200 …….. ……. ……
二维码

扫码加我 拉你入群

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

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

关键词:缺失值 company compan sales month 如何

回帖推荐

gzjb 发表于2楼  查看完整内容

**I haven't dealt with month. 200101--you mean January, 2001 ***; **** Anyway it doesn't affect your goal ***; data company; input company$3. month sales; datalines; 001 200101 700 001 200102 650 001 200103 . 001 200104 1500 001 200105 850 002 200305 . 002 200306 . 002 200307 . 002 200308 . 002 200309 3600 002 200310 1200 ; run; data companynew(drop=flag); set company; ...

本帖被以下文库推荐

沙发
gzjb 发表于 2010-3-21 09:14:55 |只看作者 |坛友微信交流群
**I haven't dealt with month. 200101--you mean January, 2001 ***;
**** Anyway it doesn't affect your goal ***;
data company;
  input company$3. month sales;
  datalines;
001 200101 700
001 200102 650
001 200103 .
001 200104 1500
001 200105 850
002 200305 .
002 200306 .
002 200307 .
002 200308 .
002 200309 3600
002 200310 1200
;
run;

data companynew(drop=flag);
  set company;
  retain flag;
  if sales=. then  flag=1;
  else if flag=1 then do;
     sales=.;
          flag=0;
        end;
run;

proc print; id company; run;

***** SAS output ****;
                                     company     month    sales

                                       001      200101      700
                                       001      200102      650
                                       001      200103        .
                                       001      200104        .
                                       001      200105      850
                                       002      200305        .
                                       002      200306        .
                                       002      200307        .
                                       002      200308        .
                                       002      200309        .
                                       002      200310     1200
已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

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

使用道具

藤椅
funwin 发表于 2010-3-21 10:14:01 |只看作者 |坛友微信交流群
gzjb 发表于 2010-3-21 09:14
**I haven't dealt with month. 200101--you mean January, 2001 ***;
**** Anyway it doesn't affect your goal ***;
data company;
  input company$3. month sales;
  datalines;
001 200101 700
001 200102 650
001 200103 .
001 200104 1500
001 200105 850
002 200305 .
002 200306 .
002 200307 .
002 200308 .
002 200309 3600
002 200310 1200
;
run;

data companynew(drop=flag);
  set company;
  retain flag;
  if sales=. then  flag=1;
  else if flag=1 then do;
     sales=.;
          flag=0;
        end;
run;

proc print; id company; run;

***** SAS output ****;
                                     company     month    sales

                                       001      200101      700
                                       001      200102      650
                                       001      200103        .
                                       001      200104        .
                                       001      200105      850
                                       002      200305        .
                                       002      200306        .
                                       002      200307        .
                                       002      200308        .
                                       002      200309        .
                                       002      200310     1200
受益匪浅
非常感谢

使用道具

板凳
funwin 发表于 2010-3-21 10:22:19 |只看作者 |坛友微信交流群
如果情况更复杂些:
有的公司可能第一个月的数据就是缺失值,那么第二的月的数据不应该被认为是错误值,所以不应该被设为缺失值。也就是:每家公司,第一个月如果是缺失值,那么第二个月不需要被设为缺失值;或者从一开始都是缺失值,那么第一个有数据的月份也不应该设为缺失值。而其他那些在中间出现的缺失值情况,则按之前的处理方法。
如何能在上述codes上处理这种情况?再次请教!
如下表
company  month     sales
001      200101      .
001      200102      650  /*不变,因为前一个月是第一个缺失值, 不能说明这个月的数据重复计算了,所以是正确值*/
001      200103        .
001      200104      1500  /*这个才需要设为” .”*/
001      200105      850
002      200305        .
002      200306        .
002      200307        .
002      200308        .
002      200309     1600  /*也不变,因为从一开始都是缺失值, 也不能说明这个月的数据是错误的,所以是正确值*/
002      200310     1200
002      200311      .
002      200312      .
002      200401      .
002      200402     1300   /*这个才需要设为” .”*/
002      200403     1400

使用道具

报纸
funwin 发表于 2010-3-21 10:32:07 |只看作者 |坛友微信交流群
可以以这个为例:
data company;
  input company$3. month sales;
  datalines;
001 200101 .
001 200102 650
001 200103 .
001 200104 1500
001 200105 850
002 200305 .
002 200306 .
002 200307 .
002 200308 .
002 200309 1600
002 200310 1200
003 200305 80
003 200306 .
003 200307 190
003 200308 100
;
run;

希望得到:
company   date   sales
001      200101   .
001      200102   650
001      200103   .
001      200104   .
001      200105   850
002      200305    .
002      200306    .
002      200307    .
002      200308    .
002      200309    1600
002      200310    1200
003      200305    80
003      200306    .
003      200307    .
003      200308    100

使用道具

地板
funwin 发表于 2010-3-21 21:39:56 |只看作者 |坛友微信交流群
自己顶!
希望哪位帮忙解决这个问题!

使用道具

7
funwin 发表于 2010-3-21 23:32:00 |只看作者 |坛友微信交流群
gzjb 发表于 2010-3-21 09:14
**I haven't dealt with month. 200101--you mean January, 2001 ***;
**** Anyway it doesn't affect your goal ***;
data company;
  input company$3. month sales;
  datalines;
001 200101 700
001 200102 650
001 200103 .
001 200104 1500
001 200105 850
002 200305 .
002 200306 .
002 200307 .
002 200308 .
002 200309 3600
002 200310 1200
;
run;

data companynew(drop=flag);
  set company;
  retain flag;
  if sales=. then  flag=1;
  else if flag=1 then do;
     sales=.;
          flag=0;
        end;
run;

proc print; id company; run;

***** SAS output ****;
                                     company     month    sales

                                       001      200101      700
                                       001      200102      650
                                       001      200103        .
                                       001      200104        .
                                       001      200105      850
                                       002      200305        .
                                       002      200306        .
                                       002      200307        .
                                       002      200308        .
                                       002      200309        .
                                       002      200310     1200
这个Code还是有点问题:假如第一家公司最后一个值是缺失值. 而第二家公司的第一值不是缺失值,那么用这个code,会使这第二家公司的第一个值,也变成缺失值。
在code里,应该按每家公司来考虑。

使用道具

8
funwin 发表于 2010-3-22 00:28:23 |只看作者 |坛友微信交流群
i'm a bit unsure of whether this problem can be solved by using "by ....", like
data companynew(drop=flag);
  set company;
  by company;

使用道具

9
gzjb 发表于 2010-3-22 00:31:40 |只看作者 |坛友微信交流群
7# funwin

The code is based on your data. I haven't consider all cases. Now add the case you mentioned. The codes is following:

data company;
  input company$3. month sales;
  datalines;
001 200101 700
001 200102 650
001 200103 .
001 200104 1500
001 200105 850
001 200106 .
002 200305 100
002 200306 .
002 200307 .
002 200308 .
002 200309 3600
002 200310 1200
;
run;

proc sort data=company out=companysort;  by company month; run;

data companynew(drop=flag);
  set company;
  by company month;
  retain flag;
  if (sales=. and last.company=0) then  flag=1;
  else if flag=1 then do;
     sales=.;
      flag=0;
    end;
run;

proc print; id company; run;

SAS output:
                                      company     month    sales

                                       001      200101      700
                                       001      200102      650
                                       001      200103        .
                                       001      200104        .
                                       001      200105      850
                                       001      200106        .
                                       002      200305      100
                                       002      200306        .
                                       002      200307        .
                                       002      200308        .
                                       002      200309        .
                                       002      200310     1200

使用道具

10
gzjb 发表于 2010-3-22 03:59:46 |只看作者 |坛友微信交流群
5# funwin

These codes should solve your NEW problem. But I think best way is to output file to EXCEL , or other softwares, so you can easily do it.
We mainly need to focus on STATISTICAL ANALYSIS. GOD bless you.
*********************************************************************************

data company;
  input company$3. month sales;
  datalines;
001 200101 .
001 200102 650
001 200103 .
001 200104 1500
001 200105 850
002 200305 .
002 200306 .
002 200307 .
002 200308 .
002 200309 1600
002 200310 1200
002 200311 .
003 200305 80
003 200306 .
003 200307 190
003 200308 100
run;

proc sort data=company out=companysort;
by company month;
run;
  
data companynew;
  set companysort;
  by company month;
  retain cfla nmisc;
  if missing(sales) then if first.company then do; cfla=1; nmisc=1; end;
                          else if cfla=1 then nmisc+1;
                               else do;
                                  if last.company then cfla=1;
                                                   else cfla=0;
                                   nmisc=0;
                                end;
     else do; nmisc=0; cfla=0; end;
  run;

  data companymis(drop =nmisc flag cfla);
  set companynew;
  retain flag;
  if sales=. then if cfla=0 and nmisc=0 then flag=1;
                                         else flag=0;
  else if flag=1 then do;
     sales=.;
     flag=0;
    end;
run;

  
proc print; id company; run;

*** SAS output for data in the code ***;
                               company     month    sales

                                       001      200101        .
                                       001      200102      650
                                       001      200103        .
                                       001      200104        .
                                       001      200105      850
                                       002      200305        .
                                       002      200306        .
                                       002      200307        .
                                       002      200308        .
                                       002      200309     1600
                                       002      200310     1200
                                       002      200311        .
                                       003      200305       80
                                       003      200306        .
                                       003      200307        .
                                       003      200308      100

SAS output for data:
data company;
  input company$3. month sales;
  datalines;
001 200101 700
001 200102 650
001 200103 .
001 200104 1500
001 200105 850
001 200106 .
002 200305 100
002 200306 .
002 200307 .
002 200308 .
002 200309 3600
002 200310 1200
;
run;

results:
                                    company     month    sales

                                       001      200101      700
                                       001      200102      650
                                       001      200103        .
                                       001      200104        .
                                       001      200105      850
                                       001      200106        .
                                       002      200305      100
                                       002      200306        .
                                       002      200307        .
                                       002      200308        .
                                       002      200309        .
                                       002      200310     1200

使用道具

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

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

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

GMT+8, 2024-4-30 23:24