楼主: 调殇
6186 8

[问答] 如何在一连串不连续的日期中自动补齐日期 [推广有奖]

  • 9关注
  • 1粉丝

讲师

2%

还不是VIP/贵宾

-

威望
0
论坛币
155 个
通用积分
2.0000
学术水平
2 点
热心指数
0 点
信用等级
0 点
经验
11367 点
帖子
189
精华
0
在线时间
581 小时
注册时间
2010-4-20
最后登录
2020-11-23

楼主
调殇 发表于 2013-3-20 20:51:41 |AI写论文
20论坛币
工作需要,要经常更新一些数据。但源数据中日期是不连续的,有断层。现在需要补齐缺失的日期,并且根据usid来生产序号。具体文件见附件。谢谢各位大神帮忙了。ps:只要能解决问题即可,不限定用SAS,EXCEL也可。 期望效果.xls (52.5 KB)

原文件.xls (377.5 KB)

最佳答案

bobguy 查看完整内容

This one is clear in logic. data have; input id date: yymmdd10.; format date yymmdd10.; cards; 1 2012-02-16 1 2012-02-17 1 2012-02-20 1 2012-03-01 1 2012-03-03 2 2012-02-20 2 2012-02-23 2 2012-02-24 2 2012-03-03 3 2012-03-01 3 2012-03-02 ; run; proc means data=have nway noprint; class id ; var date ; output out=minmax(keep=id min_dat ...
关键词:EXCEL exce xcel 解决问题 sid 如何

回帖推荐

bobguy 发表于3楼  查看完整内容

This one is clear in logic. data have; input id date: yymmdd10.; format date yymmdd10.; cards; 1 2012-02-16 1 2012-02-17 1 2012-02-20 1 2012-03-01 1 2012-03-03 2 2012-02-20 2 2012-02-23 2 2012-02-24 2 2012-03-03 3 2012-03-01 3 2012-03-02 ; run; proc means data=have nway noprint; class id ; var date ; output out=minmax(keep=id min_dat ...
有道无术,术尚可进;有术无道,止于术也。

沙发
bobguy 发表于 2013-3-20 20:51:42
This one is clear in logic.

data have;
  input id date: yymmdd10.;
  format date yymmdd10.;
  cards;
  1  2012-02-16
  1  2012-02-17
  1  2012-02-20
  1  2012-03-01
  1  2012-03-03
  2  2012-02-20
  2  2012-02-23
  2  2012-02-24
  2  2012-03-03
  3  2012-03-01
  3  2012-03-02
;
run;

proc means data=have nway noprint;
class id ;
var date ;
output out=minmax(keep=id min_date max_date) min=min_date max=max_date;
run;

data template;
   set minmax;
   do date=min_date to max_date;
     output;
        end;
        format date yymmdd10.;
        drop min_date max_date;
run;

proc print;run;

藤椅
farmman60 发表于 2013-3-21 01:26:47
I try to use fake data which is the same as your data  and apply the program to get the result you need.

data have;
  input id date: yymmdd10.;
  format date yymmdd10.;
  cards;
  1  2012-02-16
  1  2012-02-17
  1  2012-02-20
  1  2012-03-01
  1  2012-03-03
  2  2012-02-20
  2  2012-02-23
  2  2012-02-24
  2  2012-03-03
  3  2012-03-01
  3  2012-03-02
;
run;

data want(rename=(_date=date));
   set have;
   by id;
   retain  _date count_day;
   format _date yymmdd10.;
   if first.id then do;
      count_day=1;
          _date=date;
          output;
    end;
     else do;
               _date=intnx('day',_date,1);
                  if _date<date then do until (_date=date);
              count_day=count_day;
               output;
              _date=intnx('day',_date,1);
                       end;
                 if _date=date then do;
                                count_day+1;
                                output;
                             end;
     end;
drop date;
run;
proc print;
run;
            

板凳
playmore 发表于 2013-3-21 08:34:04
用proc timeseries就可以解决
两三行就可以搞定
去官网搜下怎么写吧
playmore邀请您访问ChinaTeX论坛!!!进入ChinaTeX论坛

报纸
farmman60 发表于 2013-3-22 09:52:18
In LZ' excel files, It seems that series  date without stop is needed to be produced, meanwhile a statistical variable for total days is also needed to be calculated.   

地板
yongyitian 发表于 2013-3-22 11:33:11
加一个用do loop 的方法。
The first row of the original excel file need to be changed
for sucessfull import.

假如你已经把excel数据import到 dataset: data0320.
变量名为 ID, DATE,DAYS,NUM
Try the following code.

proc sort data=data0320;
by ID date;
run;

data test_A;
     format date date_tmp yymmdd10.;
     retain date_tmp;
     set data0330;   
         by id;
         if first.id then  do;
                 date_tmp = date;
                 output;
                 end;
          interval = intck('day', date_tmp, date);
       if interval = 1 then do;
                     date_tmp = date;
                     output;
                     end;
       if interval > 1 then  do;
          do i = 1 to interval;
                     date_tmp = intnx('day', date_tmp, 1);
                     output;
          end;
       end;
     drop interval i days;
run;
data test_result (rename=(date_tmp=date));
      set test_a (drop=date);
          by id;
          if first.id then days=1;
          else days+1;
run;

7
调殇 发表于 2013-3-23 22:52:56
farmman60 发表于 2013-3-21 01:26
I try to use fake data which is the same as your data  and apply the program to get the result you n ...
谢谢您的帮忙,您的代码给了我启示,但count_day的生成方式不是我期望的效果。我期望的是,如某个usid为294的人在2013.02.16~2013.02.27时间段内对应标号count_day为1~10.
有道无术,术尚可进;有术无道,止于术也。

8
调殇 发表于 2013-3-23 22:58:57
bobguy 发表于 2013-3-21 07:35
This one is clear in logic.

data have;
谢谢您的帮忙,您的代码很巧妙,也让我看到自己逻辑上灵活性的欠缺。但您只是把日期补齐,未根据usid来编号。当然根据某个变量来编号对您来说也是小菜一碟。  我是在您的代码的基础上把自己想达到的效果完成的。非常谢谢。
有道无术,术尚可进;有术无道,止于术也。

9
调殇 发表于 2013-3-23 23:01:08
playmore 发表于 2013-3-21 08:34
用proc timeseries就可以解决
两三行就可以搞定
去官网搜下怎么写吧
呵呵,看了proc timeseries过程,确实不错。SAS想学好真心不容易!
有道无术,术尚可进;有术无道,止于术也。

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

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