楼主: nancy911
4501 24

[原创博文] sas code求救 [推广有奖]

11
pccbanboo11 在职认证  发表于 2010-5-11 11:32:50
具体语句怕写错了
思路 是 最后一个表里面 限定时间,然后对时间变量写个宏

12
sushe1527 发表于 2010-5-11 11:43:21
nancy911 发表于 2010-5-11 11:18
哇塞,你神人啊,还能把说的话擦掉,牛牛牛!
就是简单的重新编辑呀
001                2009-04-01
001                2009-05-01
001                2009-07-01

如果这样的话,那第三行填什么呢?前面隔了一个月,算是new么?
但是前面两个定义是repeate了,这样001能既是new又是repeate么?

13
nancy911 发表于 2010-5-11 11:50:32
不是要填什么,是要计算每个月repeat,new,lasped各有多少人

14
sushe1527 发表于 2010-5-11 11:52:31
nancy911 发表于 2010-5-11 11:50
不是要填什么,是要计算每个月repeat,new,lasped各有多少人
我知道的,那上面第三行的七月是repeate还是new啊?你判断好了形态才能累加什么的吧?

15
nancy911 发表于 2010-5-11 11:56:05
恩,上月没买,这个月买的就算new,001 7月是new

16
crackman 发表于 2010-5-11 12:03:55
我不想吃粽子了
想吃宜家的烤肠和肉狗还有冰激凌

17
nancy911 发表于 2010-5-11 12:57:27
nobody can help me? 在线等,不是要一个月一个月的跑吧,呜呜呜呜呜呜!

18
sushe1527 发表于 2010-5-11 22:02:53
/*妹别哭哥帮你写一段,等高人,我这个太麻烦了, 但还能用*/

/*数据是我编的 包含重复的 随便测哈 只是09年的,如果只是要月,不考虑跨年因素,取month1即可,希望有帮助*/

data a;
input customerid$  purchasetime : yymmdd10.;
format purchasetime  yymmdd10.;
month=month(purchasetime);
month1=year(purchasetime)*12+month(purchasetime);
cards;
001               2009-04-01
002               2009-04-03
002               2009-04-04
009               2009-04-05
002               2009-04-06
001               2009-05-01
002               2009-05-02
002               2009-05-03
002               2009-05-04
008               2009-05-03
006               2009-06-03
007               2009-06-07
005               2009-07-03
006               2009-07-04
007               2009-07-05
005               2009-08-03
run;
proc sort data=a out=a nodupkey;by customerid month  ;run;
proc sort data=a;
by customerid month;run;
data b;set a;
by customerid month notsorted;
if first.month then z=0;
if dif(month1)=1 then z=0;
z+1;
run;
proc sort data=b;by customerid;run;
data b;set b;by customerid; length type$ 10.;
if z=1 then type="new";
if dif(month1) and not first.customerid then type="repeat";
if z>=2  & dif(month1)=0  then type="repeat";
run;
/* s_new.*/
data fx;set b;if type="new";run;
proc sort data=fx;by month; run;
data fy;set fx;
by month customerid notsorted;
if first.month then s_new=0;
if first.customerid then s_new=0;s_new+1;run;
proc sort data=fx ;by customerid purchasetime;run;
proc sort data=fy;by customerid purchasetime;run;
data f;merge b fy; by customerid purchasetime;run;
data f;set f;if s_new=. then s_new=0;run;
data fs;set f;if type=repeat;run;
data fs;
set f;
by  customerid type notsorted;
if  first.customerid & first.type
then s_repeat=0;
if type="repeat" then s_repeat+1;
run;
/*********/
proc sort data=fs;by month;run;
data t;set fs;
by  month  notsorted;
if first.month then k+1;
run;
data j1;set fs; by  month  notsorted; if last.month ;run;
data j1;set j1;td=_n_;run;proc sort data=j1;by td;run;
data f1;set fs;by month notsorted;drop  type z;if last.month;run;
proc sql noprint ;select max(k)  into : h from t;quit;
%macro t;
%do i=1 %to &h;
data t&i;
set t;
if k=&i;
keep customerid;
run;
%end;
%do i=1 %to &h-1;
%do j=%eval(&i+1) %to %eval(&i+1);
proc sql; create table p&i as
select  distinct(customerid) from t&i
where  not exists
(select distinct(customerid)
from t&j where t&i..customerid=t&j..customerid)
;quit;
data p&i;set p&i;if customerid then td=&i;run;
%end;%end;
data all;
set %do i=1 %to %eval(&h-1);
p&i %end;;run;
%mend;
%t;
data aa;
do td=1 to %eval(&h-1);output;end;run;
data s;merge aa all;by td;run;
data s;set s;if customerid ne "" then s_lapsed=1;if customerid eq "" then s_lapsed=0; run;
proc sort data=s;by td;run;
data s1;set s;by td notsorted;if first.td and  s_lapsed=0 then k=1;
if first.td and s_lapsed=1 then k=0;k+1;if last.td ;
run; data s1;set s1;by td notsorted;if first.td and s_lapsed=0 then k=1;run;
data s1;set s1;drop s_lapsed;rename k=s_lapsed;run;
data s1;set s1;if customerid="" then s_lapsed=0;run;
data s11;td=0;s_lapsed=0;run;
data s2;set s11 s1 ;run;
data s2;set s2;td=td+1;run;
proc sort data=s2;by td;run;
data final1;merge j1 s2;by td;run;
proc sort data=final1;by td;run;
data final1;set final1;drop customerid;run;
proc sort data=final1;by   purchasetime;run;
proc sort data=fs;by   purchasetime;run;
data final2;merge fs final1;by  purchasetime;run;
data final2;set final2;if s_lapsed=. then s_lapsed=0;drop td;run;
data final2;set final2;drop  month1 z;run;
proc sort data=final2;by purchasetime;run;
proc print data=final2;title "数据全部结果";run;
/*下面的计数前2列是计数,后1列是保值 所以分开,要不会出问题的*/
proc sort data=final;by month;run;
data final;
set final2;
if s_new ne 0 then s_new=1;
if s_repeat ne 0 then s_repeat=1;
run;
data final;
set final;
by month notsorted;
if first.month then new=0;new+s_new;
if first.month  then repeat=0;repeat+s_repeat;
lapsed=s_lapsed;
if last.month ;
drop s_new s_repeat s_lapsed ;
run;
data final;set final;by month notsorted ;
if _n_=1 then repeat=0;
run;
proc print data=final; title " 精简最终结果 ";run;

19
juicyjuice 发表于 2010-5-12 02:02:59
这里好好哦 还有人帮忙写程序 希望下次我有问题大家也能一起帮忙讨论

20
peijiamei 发表于 2010-5-12 08:43:06
这个帖子有意思,顶一顶
我的微博:http://t.sina.com.cn/1087192374
欢迎互相加关注!

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

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