datamart

1、取上周日日期和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;

2、轉(zhuǎn)置:
PROC TRANSPOSE DATA=newcatbycnt out=newcatbycnt1(drop=NAME);
BY CustomerID; /列不變/
ID newcat; /newcat變?yōu)樾?
VAR fag; /fag值/
run;

3、添加最后一筆往前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;

/************************************************************************/
4、/*********計(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);
/************************************************************************/

5、導(dǎo)出CSV:
proc export data=result
outfile='D:\Lancome_ec\報(bào)表\sampler.csv' dbms=csv replace;
run;
6、導(dǎo)入CSV:
%macro importcsv(dataset,path);
proc import datafile=&path out=&dataset;run;
%mend;
%importcsv(dataset=listwave,path="D:\Lancome_ec\報(bào)表\listwave.csv");
7、導(dǎo)入excel:
PROC IMPORT OUT= WORK.recommend_rule
DATAFILE= "E:\EC\kie\ff-test\recommend_rule.xlsx"
DBMS=EXCEL REPLACE;
SHEET="Sheet1$";
GETNAMES=YES;
RUN;
8、導(dǎo)出excel:
PROC EXPORT DATA=&input
OUTFILE= "E:\EC\kie\ff-test\result&output..xls"
DBMS=EXCEL REPLACE label;
SHEET="sheet1";
RUN;
9、/**************************excel多表導(dǎo)出*****************************/
方法1:
%macro report(input,output);
PROC EXPORT DATA=&input
OUTFILE= "E:\EC\kie\ff-test\定期項(xiàng)目\TMALL先試后買(mǎi)\Review\review&EC_date..xlsx"
DBMS=EXCEL REPLACE label;
SHEET="&output.";
RUN;

%mend;

%report(S2p_p2,先試后買(mǎi)推薦結(jié)果);
%report(P2p_p2,正裝推薦結(jié)果);
%report(Productnum,正裝回購(gòu)人數(shù));
%report(Samplenum,先試后買(mǎi)回購(gòu)人數(shù));
%report(p_time,正裝回購(gòu)參考日期);
%report(s_time,先試后買(mǎi)回購(gòu)參考日期);

方法2:
libname xlout excel "E:\EC\kie\ff-test\定期項(xiàng)目\TMALL先試后買(mǎi)\Review\review&EC_date..xlsx";
proc datasets lib=xlout kill;run;
data xlout.先試后買(mǎi)推薦結(jié)果;set S2p_p2;run;
data xlout.正裝推薦結(jié)果;set P2p_p2;run;
data xlout.正裝回購(gòu)人數(shù);set Productnum;run;
data xlout.先試后買(mǎi)回購(gòu)人數(shù);set Samplenum;run;
data xlout.正裝回購(gòu)參考日期;set p_time;run;
data xlout.先試后買(mǎi)回購(gòu)參考日期;set s_time;run;

libname xlout clear;
/***********************************************************************/

10、SAS 中可以利用PROC suveryselect 過(guò)程實(shí)現(xiàn)各種抽樣
其一般形式是:
PROC SURVEYSELECT
data=<源數(shù)據(jù)集名>
method = <srs l urs l sys >
out=<抽取樣本存放的數(shù)據(jù)集>
n=<抽取數(shù)量>(or samprate=抽樣比例)
seed =n;
strata <指定分層變量>;
id <指定抽取的樣本所保留的源數(shù)據(jù)集變量>;
run;
說(shuō)明:method用來(lái)指定隨機(jī)抽樣方法的,其中SRS是指不放回簡(jiǎn)單隨機(jī)抽樣(Simple Random Samping);urs是指放回簡(jiǎn)單隨機(jī)抽樣(Unrestricted Random Sampling);sys是指系統(tǒng)抽樣(Systematic Sampling)。
seed用來(lái)指定隨機(jī)種子數(shù),為非負(fù)整數(shù),取0則每次抽取的樣本不同,若取大于0的整數(shù),則下次抽樣時(shí)若輸入相同值即可得到相同的樣本;
id是指定從源數(shù)據(jù)集復(fù)制到樣本數(shù)據(jù)集的變量,若缺省,則復(fù)制所有變量。
例子:
a、簡(jiǎn)單無(wú)重復(fù)隨機(jī)抽樣舉例:
/按30%的比例從test數(shù)據(jù)集中抽取樣本,并把樣本輸出到results數(shù)據(jù)集中/
proc surveyselect data=test1 out=results1 method=srs samprate=0.3;
run;
b、分層等比例隨機(jī)抽樣舉例;
proc sort data=test2;
by 分層變量;
run; /先用分層變量對(duì)總體樣本進(jìn)行排序/
proc surveyselect data=test2 out=results2 method=srs samprate=0.1;
strata 分層變量;
run; /根據(jù)分層變量等比例從總體中抽取樣本/
c、分層不等比例抽樣舉例;
(1)手工設(shè)置抽樣比例或者抽樣數(shù)
proc sort data=test3;
by 分層變量;
run; /先用分層變量對(duì)總體樣本進(jìn)行排序/
proc surveyselect data=test3 out=results3 method=srs
samprate=(0.1,0.3,0.5,0.2); /根據(jù)分層情況設(shè)置每一層要抽取的比例/
strata 分層變量;
run; /根據(jù)分層變量不等比例從總體中抽取樣本/
proc surveyselect data=test3 out=results3 method=srs
n=(30,20,50,40); /根據(jù)分層情況設(shè)置每一層要抽取的樣本數(shù)/
strata 分層變量;
run;
(2)根據(jù)抽樣表進(jìn)行不等比例抽樣
proc sort data=test3;
by 分層變量;
run; /先用分層變量對(duì)總體樣本進(jìn)行排序/
proc surveyselect data=test3 out=results3 method=SRS
samprate=samp_table; /通過(guò)抽樣比例數(shù)據(jù)集進(jìn)行抽樣,samp_table數(shù)據(jù)集中要包括分層變量 以及每一分層對(duì)應(yīng)的抽樣比例或者數(shù)量,如果按比例抽樣變量必須用rate來(lái)命名抽樣比例,如果是按數(shù)量抽樣必須用nsize來(lái)命名抽樣數(shù)量/
strata 分層變量;
run;

11、輸出由freq出的類(lèi)別的頻數(shù)和百分比構(gòu)成的表
ods csv file="G:\EC\KIE\online datamart&date\datamart_對(duì)比.csv";
/輸出datamart_對(duì)比表,總表中control和test包括以下6個(gè)量/
proc freq data=t_test;table cluster;run;
proc freq data=c_control;table cluster;run;
proc freq data=t_test;table auscat;run;
proc freq data=c_control;table auscat;run;
proc freq data=t_test;table Frequency_online;run;
proc freq data=c_control;table Frequency_online;run;
ods csv close;
12.官網(wǎng)datamart字段
最后一筆往前rolling一年時(shí)間段:
Frequency_online 、AnnualSpending_online 、AUS_online 、ipt_online
數(shù)據(jù)截止日期往前rolling一年時(shí)間段:
Frequency_online_r12 、AnnualSpending_online_r12 、ipt_r12 am=AUS_online_r12

status字段中:
new_r12:首筆購(gòu)買(mǎi)在數(shù)據(jù)截止日到往前rolling一年的時(shí)間內(nèi);
newrepeat_r12:首筆購(gòu)買(mǎi)在數(shù)據(jù)截止日到往前rolling一年的時(shí)間內(nèi),并且次數(shù)>1;
Winback_R12:最后一筆購(gòu)買(mǎi)在數(shù)據(jù)截止日到往前rolling一年的時(shí)間內(nèi),并且最后一筆距離倒數(shù)第二筆超過(guò)365天;
Existing_r12:數(shù)據(jù)截止日期往前rolling一年時(shí)間段內(nèi)有購(gòu)買(mǎi)、早于數(shù)據(jù)截止日期往前rolling一年有過(guò)購(gòu)買(mǎi),并且不是Winback_R12的人;
Winback_YTD:最后一筆在今年購(gòu)買(mǎi),第一筆在去年之前,并且去年沒(méi)有購(gòu)買(mǎi)過(guò);
new_ytd:第一筆購(gòu)買(mǎi)在今年;
Existing_YTD:去年線上購(gòu)買(mǎi)過(guò),今年線上又購(gòu)買(mǎi);
Active:最后一筆距離數(shù)據(jù)截止日的時(shí)間在360天以內(nèi);sleeping:最后一筆距離數(shù)據(jù)截止日的時(shí)間在361天到720天之間;
Lost:最后一筆距離數(shù)據(jù)截止日的時(shí)間在721天到1080天之間;
Lapsed:最后一筆距離數(shù)據(jù)截止日的時(shí)間超過(guò)1081天;

cluster字段:
cluster:最后一筆往前rolling一年的人購(gòu)買(mǎi)產(chǎn)品類(lèi)別的cluster;
cluster_all:數(shù)據(jù)截止日往前rolling一年的人購(gòu)買(mǎi)產(chǎn)品類(lèi)別的cluster;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 處理日期的常見(jiàn)情景 NSDate -> String & String -> NSDate 日期比較 日期計(jì)算(基...
    KAKA_move閱讀 918評(píng)論 0 0
  • 日期的處理在大大小小的iOS項(xiàng)目中都十分常見(jiàn),隨著Swift3.0正式版的即將推出,語(yǔ)法的改變讓NSDate以及相...
    魚(yú)與愚七閱讀 855評(píng)論 0 0
  • 如何配置一個(gè)多頁(yè)應(yīng)用webpack工程化模板? webpack的四個(gè)核心概念: entry 入口:從哪里開(kāi)始 ou...
    leyou319閱讀 3,532評(píng)論 0 3
  • 很喜歡九月,即使現(xiàn)在離開(kāi)了學(xué)校,再也沒(méi)有”開(kāi)學(xué)“這個(gè)動(dòng)作。但九月仍然代表了一種躍躍欲試的可能性,代表了一種全新的開(kāi)...
    伊心同學(xué)閱讀 798評(píng)論 2 15
  • 忽然之間 終于明白 怕的不是你不愛(ài)我 而是我不再愛(ài)你 怕辜負(fù)星光的見(jiàn)證 怕忘記最初的感動(dòng) 怕遺失滿懷愛(ài)的初見(jiàn) 怕失...
    煙之閱讀 207評(píng)論 0 1

友情鏈接更多精彩內(nèi)容