全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
3029 2
2012-12-23
求助各位sas大牛,我现在手里有这样一个数据集,需要做标题所示的事情,具体阐述如下:
飞信截图20121223202859.jpg
这是我的数据表的一部分,第一列是日期(0812代表2008年12月),第二列是产品编码,就是说这款产品从08年12月一直卖到了11年8月,但是中间有些月份是没有卖的,比如说0908到0911之间就缺了两个月,0911到1004缺了四个月,原始数据表中有很多产品,我现在要剔除这样的产品,即任意两个销售记录间的时间间隔超过3个月(大于等于3)的,像图片中展示的这个产品就应该剔除,因为0911到1004缺了四个月。好像sas里没有现成的函数实现这个事情。。。。我想了好久不知道该如何写code啊,求助各位大牛帮帮忙,在下感激不尽!
二维码

扫码加我 拉你入群

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

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

全部回复
2012-12-24 07:56:53
Here is the program to identify no sale in any consecutive three month from a given product. I left how to remove them for you. It should be much easier.


*create testing data;
data t1;
   length yymm $4.;
   prodid=1980;
   do year=8 to 9;
      do month = 1 to 12;
            yymm=catt(put(year,z2.), put(month,z2.));
                sales=ceil(ranuni(123)*50);
                if yymm not in ('0801', '0802', '0812', '0903','0904','0905') then output;
                *output;
          end;
        end;
   prodid=1981;
   do year=8 to 9;
      do month = 1 to 12;
            yymm=catt(put(year,z2.), put(month,z2.));
                sales=ceil(ranuni(123)*50);
                *if yymm not in ('0802', '0812', '0903') then output;
                output;
          end;
        end;
        keep prodid yymm sales;
run;

***build a template with no missing month sales;
proc sql;
create table sales_info as
select prodid, input(catt(min(yymm),'01'), yymmdd6.) as LYYMM,
                input(catt(max(yymm),'01'), yymmdd6.) as HYYMM
  
  from t1
  group by prodid
  order by prodid  
  ;
  quit;

data prod_yymm;
   length yymm $4.;
   set sales_info;
   by prodid;
   date=LYYMM;
   do until (date>HYYMM);
      yymm=put(date,yymmdd6.);
      output;
          date= intnx('month', date,1);          
   end;
   keep prodid yymm;
run;

data check;
  merge prod_yymm t1;
  by prodid yymm;
  sales1=lag1(sales);
  sales2=lag2(sales);
  if first.prodid then cnt=0;
  cnt+1;
  if cnt in (1) then do;
    sales1=.; sales2=.;
  end;
  if cnt=2 then sales2=.;
  missing_3_month=(sum(sales, sales1, sales2)=.);
run;

proc print;run;

二维码

扫码加我 拉你入群

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

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

2012-12-24 12:40:11
bobguy 发表于 2012-12-24 07:56
Here is the program to identify no sale in any consecutive three month from a given product. I left  ...
谢谢牛人!我看到论坛里好多sas的技术贴都是你回复的,膜拜!另外昨天我发完贴后又在继续想,下面是写的一个程序,请牛人帮忙看一下:
/******将0807改为200807,方便下面转换成日期形式**********/
data market.beer2;
set market.beer1;
sdate=month+200000;
drop month;
run;
/********将200807转换为日期2008-07形式**********/
data market.beer3;
set market.beer2;
time=mdy(mod(sdate,100),1,int(sdate/100));
format time yymmd.;
drop sdate;
run;
proc summary data=market.beer3 nway;
var time;
class procode;
output out=market.cal min=mintime max=maxtime;
run;

data market.beer4;
merge market.beer3 market.cal;
by procode;
keep procode time mintime _FREQ_;
run;
/*******计算时间差,这里算出的是天数********/
data Tmp1.beer7;
set Tmp1.beer4;
y=time-mintime;
run;
/******需要把算出的天数转换成月份,这里还没想到更好的方法,intck函数应该可以,不知如何实现*********/
data Tmp1.beer8;
set Tmp1.beer7;
interval=int((y+2)/30);
run;
/*******计算同一变量两两观测之间的差值,lag()dif()函数*******/
data Tmp1.beer9 replace;
set Tmp1.beer8;
z=lag(interval);
d=dif(interval);
run;
/******筛选出两两观测销售记录缺失之间不超过三个月的产品*******/
data Tmp1.new1 Tmp1.new2 replace;
set Tmp1.beer9;
if d >= 4 then output Tmp1.new1;
else output Tmp1.new2;
run;
data Tmp1.result replace;
merge Tmp1.new2 (in=a) Tmp1.new1 (in=b);
by procode;
if a=1 and b=0;
run;

想请问牛人的是,intck函数在这里应该怎么使?因为我想计算2008-09和2008-12之间月份的数,我写了几个都说语法错误,最后无奈,用了int函数。。。算是变相生成了结果吧。
二维码

扫码加我 拉你入群

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

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

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群