楼主: 456852
3790 16

[原创博文] 敢请高手解惑一个数据问题 [推广有奖]

11
hssnow 发表于 2011-1-29 22:13:08
9# baoaibaobao
呵呵,非常感谢指正,学习了~
My Blog: http:/hssnow.name/
宁静致远

12
baoaibaobao 发表于 2011-1-29 22:20:37
10# soporaeternus photo相当给力啊~

13
hssnow 发表于 2011-1-29 22:41:00
10# soporaeternus
呵呵,前辈的code很漂亮,不过小弟不是很理解啊,能大概解释一下,datediff是怎么得到的?on...的作用么?非常感谢!
My Blog: http:/hssnow.name/
宁静致远

14
bobguy 发表于 2011-2-3 05:05:51
1# 456852

I modify hssnow's program a bit. Hope this is what you want.
data a;
    input customer $ product $ date date9. category $;
datalines;
1               001 12Feb2010          A
1               002 13Feb2010          B
1               003 19Feb2010          B
1               004 01Mar2010          A
2               005 12Mar2010          B
2               006 15Apr2010          A
2               007 18Apr2010          B
;
run;


data b ;*(drop=iddate);
    set a;
    by customer;
    retain iddate;
    if first.customer then do;
       count=0;
       iddate=.;
    end;
    count+(    category='A');
    if count=1 and iddate=. then iddate=date;
    if iddate ne . then datediff=date-iddate;
    format     date iddate yymmdd10.;
run;

proc print;run;

15
shenliang_111 发表于 2011-8-24 10:46:26
try this;
proc sort data=aa;
by customer product date;
run;
data new(drop=datett);
datett=0;
do _n_=1 by 1 until(last.customer);
set aa;
by customer;
if _n_=1 then call missing(datediff);
  else  if datett=0 then call missing(datediff);
         else datediff=date-datett;
if category='A' then datett=date;
output;
end;
run;
需要注意的是楼主给出的数据集和给出的说明中两类日期不同,一个是3月1号 一个是2月22号...

16
shenliang_111 发表于 2011-12-21 14:19:14
shenliang_111 发表于 2011-8-24 10:46
try this;
proc sort data=aa;
by customer product date;
  1. 再次简化:
  2. data result2;
  3. time=.;
  4. do _n_=1 by 1 until(last.customer);
  5.    set  zachary;
  6.    by customer;
  7.    datediff=date-time;
  8.    time=ifn(category='A',date,time);
  9.    output;
  10. end;
  11. run;
复制代码

17
shenliang_111 发表于 2011-12-21 14:53:24
shenliang_111 发表于 2011-12-21 14:19
  1. sql方法(该方法的思路都是向soporaeternus学习的)
  2. proc sql;
  3. select a.customer,a.product,a.date,a.category,(a.date-b.date) as datediff
  4. from (select *,monotonic() as n from zachary) a
  5. left join (select *,monotonic() as n from zachary where category='A') b
  6. on  a.customer=b.customer and a.n>b.n
  7. group by 1,2
  8. having calculated datediff=min(calculated datediff)
  9. order by a.customer,a.product;
  10. quit;
复制代码

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-2 05:38