date9.===>16feb2018
yymmdd10.===>2018-04-22
yymmddn8.===>20180422
输入日期 日期宽度 INFORMAT
03/11/2014 10 mmddyy10.
03/11/14 8 mmddyy8.
December 11, 2012 20 worddate20.
14mar2011 9 date9.
14-mar-2011 11 date11.
14-mar-2011 15 anydtdte15.
Datetime 对应的几种格式,供参考(例如:‘30May00 10:03:17.2’dt)
30May2000:10:03:17.2 DATETIME20.
30May00 10:03:17.2 DATETIME18.
30May2000/10:03 DATETIME15.
Day函数:得到日期的天,例如:day(‘2016-09-01’d)=1;
Month函数:得到日期的月份,例如:month(‘2016-09-01’d)=9;
Year函数:得到日期的年份,例如:year(‘2016-09-01’d)=2016;
Hour函数:得到时间的小时,例如:hour(‘18:10:01’t)=18;
Minute函数:得到时间的分钟,例如:minute(‘18:10:01’t)=10;
Datepart函数:获取日期时间类型中的日期部分;
Timepart函数:获取日期时间类型中的时间部分。
例如:
data time;
format time datetime18.;
time='30May18 10:03:10'dt;
a=put(timepart(time),time.);
run;
yymmdd10.:这种格式可以将要日期表示为: yyyy-MM-dd的样式;
hhmmss.:这种格式可以将时间类型格式化为:HH:mm:ss的形式。
intck:根据间隔,计算两个日期之间的间隔数;
intnx:计算某个间隔数之后的一个日期。
data samprate1;set samprate1;if n=1;run; /取第一条记录/
/取第一条记录的seed,取最优随机数/
proc sql noprint;
select distinct seed into:seed separated by " "
from samprate1;
quit;/用变量值创建一个宏变量/
/*记录temp_c_t3的数据量,名为num_of_records /
proc sql;
select count() into: num_of_records from temp_c_t3;
quit;
取上周日日期和rolling12月的日期:
data timing;
format date1 yymmdd10. date2 yymmdd10.;
date2=intnx('week',today(),0);/上周末,数据截止日/
if mod(year(date2),4)=0 then do;
if (month(date2)100+day(date2))>228 then date1=date2-365;
else date1=date2-364;
end;
else if mod(year(date2)-1,4)=0 then do;
if month(date2)>2 then date1=date2-364;
else date1=date2-365;
end;
else date1=date2-364;/闰年,平年往前R12的date1计算/
month=year(date2)10000+month(date2)100+day(date2);/数据截止日*/
run;
添加最后一笔往前rolling一年的日期oneyearago
data lastandfirst;
set lastandfirst;
format oneyearago yymmdd10.;
if month(lastpurchase) =2 and day(lastpurchase)=29 then do;
oneyearago=mdy(month(lastpurchase),day(lastpurchase)-1,
year(lastpurchase)-1)+1;
end;
else do;
oneyearago=mdy(month(lastpurchase),day(lastpurchase),
year(lastpurchase)-1)+1;
end;
run; /添加最后一笔往前rolling一年的日期oneyearago/
proc sort data=trans;by customer_id;run;
proc sort data=lastandfirst;by customer_id;run;
data trans;
merge trans(in=a) lastandfirst;
by customer_id;
if a;
oneyear=0;
if oneyearago<=purchasetime<=lasttime then oneyear=1;
run;
/********计算最后一笔往前rolling一年的av am af ipt**********/
%macro rfm(input,timevar,output);
proc sql;
create table a1 as
select customer_id,purchasetime,sum(price) as salesbyday,sum(unit) as ui
from &input where &timevar=1 group by 1,2 having sum(price) ne 0;
quit;
proc sort data=a1;by customer_id purchasetime;run;
data &output(keep=customer_id av am af ipt);
set a1;
by customer_id;
if first.customer_id then do;
f=0;
v=0;
u=0;
end;
if salesbyday>0 then q=1;
else q=-1;
f+q;
v+salesbyday;
u+ui;
if last.customer_id then do;
if f=0 and v>0 then f=1;
if f ne 0 then do;
m=v/f;
ipt=u/f;
end;
av=v;
am=m;
af=f;
if f>0 then output;
end;
run;
%mend;
%rfm(trans,oneyear,rfm_1year);