楼主: beci426
3961 25

求一个简单的小程序 [推广有奖]

11
823954913 发表于 2013-6-16 09:10:23
我来补充一下下。
data t1;
       input id $ date quan price;
datalines;
1        1        10        2
1        2        9        2
2        1        8        2
2        2        7        2
2        3        6        2
3        2        5        2
mm       5        4        2
;
run;
proc sort data=t1(keep=id) out=t2 nodupkey;by id ;run;
data t2;
    set t2;
        do date=1 to 10;
            call missing(quan,price);
                output;
        end;
run;      
data t3;
update  t2 t1;
by id date;
run;

12
bobguy 发表于 2013-6-16 09:53:40
Here is a simple data step solution in a simple logic. Assume data is sorted by id date and the variable date is a sas numerical variable.

k=1;  <---- 1 is lower bound
do s=k to 3;  <-- 3 is upper bound.

data t1;
    input id date quan price;
datalines;
1        1        10        2
1        2        9        2
2        1        8        2
2        2        7        2
2        3        6        2
3        2        5        2
;
run;

  data t1;
    retain k;
    set t1;
        by id;
        if first.id then k=1;
        do s=k to date-1;
          quan_new=.; price_new=.;
          output;
        end;
        quan_new=quan ; price_new=price;
        output;
        k=date+1;
        if last.id then do;
          do s=k to 3;
             quan_new=.; price_new=.;
             output;
           end;
        end;
        drop date    quan    price k;
        rename s=date;
  
  run;

  proc print;run;

13
yongyitian 发表于 2013-6-16 10:13:31
/*  Here is another way, it use do-loop and sort           */
/*  Try the following code to see if it is faster                 */
/*  than merge and hash table for this special problem   */


data test;                 /* create the sample data set */  
   do ID = 1000001 to 1100000;
      do date = ceil(ranuni(123)*10) to ceil(ranuni(123)*10);
         quan = ceil(ranuni(123)*100);
        price = round(ranuni(123)*100,0.01);
        output;
      end;
   end;
run;

proc sort data=test;       /* the sorting is required if real data was not sorted */
by id date;        run;

data test1;
  set test;
  by id;
    if first.id then do;   output;
      if date ^= 1 then do;
         do i =  date-1 to 1 by -1;
           date = i;   call missing(quan, price);
           output;     /* fill the missing date             */  
         end;          /* end of loop for fill missing date */
      end;             /* end of loop for data^=1           */  
    end;               /* end of loop for first.id          */   
   if not last.ID then output;
   if last.ID then do;   output;
      if date ^=10 then do;
         do i = date+1 to 10;
           date = i;  call missing(quan, price); output;
         end;
      end;
    end;
drop i;
run;

proc sort data=test1 out = result nodupkey;
by id date; run;  

proc print data=result(obs=100); title 'result'; run;

14
boe 发表于 2013-6-16 10:48:57
823954913 发表于 2013-6-16 09:10
我来补充一下下。
data t1;
       input id $ date quan price;
嗯,学习了。效率应该可以。。。
Gorgeous girl , I love !

15
boe 发表于 2013-6-16 10:55:56
yongyitian 发表于 2013-6-16 10:13
/*  Here is another way, it use do-loop and sort           */
/*  Try the following code to see if  ...
排序过程可能会耗去很多时间啊。。。。maybe。。。。
Gorgeous girl , I love !

16
yongyitian 发表于 2013-6-16 11:12:42
boe 发表于 2013-6-16 10:55
排序过程可能会耗去很多时间啊。。。。maybe。。。。
Yes, you are right. It must be taken into account.
Sometime sorting use more time than computing.
However, for this case the hash table may be too large.

17
邓贵大 发表于 2013-6-16 11:49:09
  1. data hell;
  2.     input id date quan price dummy $;
  3. datalines;
  4. 1        1        10        2        curious
  5. 1        2        9        2        george
  6. 2        1        8        2        deng
  7. 2        2        7        2        yu
  8. 2        3        6        2        jiao
  9. 3        2        5        2        chouren
  10. ;
  11. proc format;
  12.         value mydate
  13.         1='1'
  14.         2='2'
  15.         3='3'
  16.         4='4'
  17.         5='5'
  18.         6='6'
  19.         7='7'
  20.         8='8'
  21.         9='9'
  22.         10='10'
  23.         ;
  24. proc means data=hell noprint nway completetypes order=internal max;
  25.         by ID;
  26.         format date mydate.;
  27.         class date/preloadfmt;
  28.         output out=hell1(drop=_type_ _freq_) max=quan price idgrp(max(price) out(dummy)=dummy);
  29. run;
复制代码
黄泉下阴气太重,出来凑个热闹,不敢跟大家比EFFICIENCY
已有 2 人评分学术水平 热心指数 信用等级 收起 理由
823954913 + 1 真不是一般人能想到的!deng yu jiao 果然是.
boe + 1 + 1 + 1 可能是最好的解决方案

总评分: 学术水平 + 2  热心指数 + 1  信用等级 + 1   查看全部评分

Be still, my soul: the hour is hastening on
When we shall be forever with the Lord.
When disappointment, grief and fear are gone,
Sorrow forgot, love's purest joys restored.

18
boe 发表于 2013-6-16 12:16:33
邓贵大 发表于 2013-6-16 11:49
黄泉下阴气太重,出来凑个热闹,不敢跟大家比EFFICIENCY
Your excellent SAS skills should have inhibited the dummy of id=2,but be careful even now!
Gorgeous girl , I love !

19
bigDatafan 发表于 2013-6-16 15:58:09
学习了。各位大神。

20
老师她摸我 发表于 2013-6-16 18:45:52
beci426 发表于 2013-6-15 17:04
您好,非常感谢您的回答!
有两个小的问题
1 如果要是id中不仅是数字还有字母,而且id数量众多。应该如 ...
下面这个就不限ID的变量类型了!
  1. data test;
  2.        input id $ date quan price;
  3. datalines;
  4. 1        1        10        2
  5. 1        2        9        2
  6. 2        1        8        2
  7. 2        2        7        2
  8. 2        3        6        2
  9. x        3        5        2
  10. 3        2        4        2
  11. ;
  12. run;

  13. data _null_;
  14.         if _n_=1 then do;
  15.             if 0 then set test;
  16.                 declare hash h(dataset:'test',ordered:'y');
  17.                 h.definekey('id','date');
  18.                 h.definedata(all:'y');
  19.                 h.definedone();
  20.         end;
  21. set test end=eof;
  22. do date=1 to 10;
  23.         if h.find() ne 0 then do;
  24.                 call missing(quan,price);
  25.                 h.add();
  26.         end;
  27. end;
  28. if eof then h.output(dataset:'want');
  29. run;
复制代码


已有 1 人评分学术水平 热心指数 信用等级 收起 理由
boe + 1 + 1 + 1 精彩帖子

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

欢迎加入SAS群:144839730-蜗牛

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

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