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 對(duì)應(yīng)的幾種格式蒙揣,供參考(例如:‘30May00 10:03:17.2’dt)
30May2000:10:03:17.2 DATETIME20.
30May00 10:03:17.2 DATETIME18.
30May2000/10:03 DATETIME15.
Day函數(shù):得到日期的天贮尉,例如:day(‘2016-09-01’d)=1;
Month函數(shù):得到日期的月份,例如:month(‘2016-09-01’d)=9娘香;
Year函數(shù):得到日期的年份情连,例如:year(‘2016-09-01’d)=2016蛔外;
Hour函數(shù):得到時(shí)間的小時(shí)冀值,例如:hour(‘18:10:01’t)=18;
Minute函數(shù):得到時(shí)間的分鐘叛赚,例如:minute(‘18:10:01’t)=10澡绩;
Datepart函數(shù):獲取日期時(shí)間類(lèi)型中的日期部分;
Timepart函數(shù):獲取日期時(shí)間類(lèi)型中的時(shí)間部分俺附。
例如:
data time;
format time datetime18.;
time='30May18 10:03:10'dt;
a=put(timepart(time),time.);
run;
yymmdd10.:這種格式可以將要日期表示為: yyyy-MM-dd的樣式肥卡;
hhmmss.:這種格式可以將時(shí)間類(lèi)型格式化為:HH:mm:ss的形式。
intck:根據(jù)間隔事镣,計(jì)算兩個(gè)日期之間的間隔數(shù)步鉴;
intnx:計(jì)算某個(gè)間隔數(shù)之后的一個(gè)日期。
data samprate1;set samprate1;if n=1;run; /取第一條記錄/
/取第一條記錄的seed蛮浑,取最優(yōu)隨機(jī)數(shù)/
proc sql noprint;
select distinct seed into:seed separated by " "
from samprate1;
quit;/用變量值創(chuàng)建一個(gè)宏變量/
/*記錄temp_c_t3的數(shù)據(jù)量唠叛,名為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);/上周末,數(shù)據(jù)截止日/
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計(jì)算/
month=year(date2)10000+month(date2)100+day(date2);/數(shù)據(jù)截止日*/
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;
/********計(jì)算最后一筆往前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);