您的位置:首页 > 大数据 > 人工智能

SAS - Campaign QC New

2016-02-26 13:56 579 查看
%put cur_time=%sysfunc(putn(%sysfunc(time()), time9.));

options nomprint nosource nosource2;

proc printto log="&lib.log.txt" new;
run;

%let lib=\\Hqdellsas04\exports\APJ\EM\QC\;
%let path=\\Hqdellsas04\exports\APJ\EM\QC\;

libname mylib "&lib.";
LIBNAME smb_prod NETEZZA DATABASE=smb_prod_view_ea SERVER=hqdellnz06 AUTHDOMAIN="hqdellnz06" DBCONINIT='SET SERIALIZABLE = FALSE;';

/*  Test */
%let inorder=44;
%let campcode=EA3_CAMP8555;
%put ***************************  Campaign QC : &campcode. ***************************************;

%let camp_table=;

%macro import_qc();
%put ***************************  QC List File Check ***************************************;
proc import datafile="&lib.qc_list.xlsx" dbms=xlsx out=qc_list replace;
getnames=yes;
run;

data qc_list;
set qc_list;
country=upcase(country);
campaign=upcase(campaign);
output_table=upcase(output_table);
drop_date=upcase(drop_date);

today=put(today(),yymmddn8.);
weekday=weekday(today());
select ;
when(drop_date='THIS TUESDAY') date_inc=1;
when(drop_date='THIS WEDNESDAY') date_inc=2;
when(drop_date='THIS THURSDAY') date_inc=3;
when(drop_date='THIS FRIDAY') date_inc=4;
when(drop_date='THIS SATURDAY') date_inc=5;
when(drop_date='THIS SUNDAY') date_inc=6;
when(drop_date='NEXT MONDAY') date_inc=7;
when(drop_date='NEXT TUESDAY') date_inc=8;
when(drop_date='NEXT WEDNESDAY') date_inc=9;
when(drop_date='NEXT THURSDAY') date_inc=10;
when(drop_date='NEXT FRIDAY') date_inc=11;
when(drop_date='NEXT SATURDAY') date_inc=12;
when(drop_date='NEXT SUNDAY') date_inc=13;
otherwise date_inc=0;
end;
expect_dropdate=put(today()+date_inc-(weekday-2), yymmddn8.);
run;

/* Check QC List */
proc sql errorstop;
select count(0) into :country_missing from qc_list where missing(country)=1;
select count(0) into :hierarchy_missing from qc_list where missing(hierarchy)=1;
select count(0) into :output_table_missing from qc_list where missing(output_table)=1;
select count(0) into :drop_date_missing from qc_list where missing(drop_date)=1;
select count(0) into :file_naming_missing from qc_list where missing(naming)=1;
select count(hierarchy)-count(distinct hierarchy) into :dup_hierarchy from qc_list;
quit;

%put country_missing=&country_missing.;
%put hierarchy_missing=&hierarchy_missing.;
%put output_table_missing=&output_table_missing.;
%put drop_date_missing=&drop_date_missing.;
%put file_naming_missing=&file_naming_missing.;
%put dup_hierarchy=&dup_hierarchy.;

%if  &country_missing. ne 0 %then %do;
data _null_;
call symput('MAMsg', 'Check QC List, Country Missing');  call symput('SYSCC', 1012);run;
%abort ;
%end;
%if  &hierarchy_missing. ne 0 %then %do;
data _null_;
call symput('MAMsg', 'Check QC List, Campaign Hierarchy Missing');  call symput('SYSCC', 1012);run;
%abort ;
%end;
%if  &output_table_missing. ne 0 %then %do;
data _null_;
call symput('MAMsg', 'Check QC List, Output Table Missing');  call symput('SYSCC', 1012);run;
%abort ;
%end;
%if  &drop_date_missing. ne 0 %then %do;
data _null_;
call symput('MAMsg', 'Check QC List, Drop Date Missing');  call symput('SYSCC', 1012);run;
%abort ;
%end;
%if  &file_naming_missing. ne 0 %then %do;
data _null_;
call symput('MAMsg', 'Check QC List, File Naming Missing');  call symput('SYSCC', 1012);run;
%abort ;
%end;
%if  &dup_hierarchy. ne 0 %then %do;
data _null_;
call symput('MAMsg', 'Check QC List, Duplicate Hierarchy');  call symput('SYSCC', 1012);run;
%abort ;
%end;
%mend import_qc;

%macro filepath_check(path);
%put ************************** Output File Path Check ****************************************;
%let rc=%sysfunc(filename(myfile, &path));
%put rc=&rc;

%if  NOT %sysfunc(fexist(&myfile)) %then %do;
data _null_;
call symput('MAMsg', 'Filepath Not Exists');  call symput('SYSCC', 1012);run;
%abort ;
%end;

%let index= %sysfunc(compress(%sysfunc(substr(&path., %sysfunc(length(&path.)),1))));

%if &index=\ %then
%put The last character is "\";
%else %do;
%put The last character is not "\";
data _null_;
call symput('MAMsg', 'Filepath is not ending with \');  call symput('SYSCC', 1012);run;
%abort ;
%end;
%mend filepath_check;

%macro table_exist(tablename);
%let dsn=smb_prod.&tablename.;
%let exists=%sysfunc(exist(&dsn));
%if not &exists %then %do;
%put ****************** ERROR ******************;
%put Table &tablename. not exists in campaign database.;
data _null_;
call symput('MAMsg', "Table Not Exist: &tablename. ");  call symput('SYSCC', 1012);run;
%abort ;
%end;
%else %do;
%put Table &tablename. exists in campaign database.;
%end;
%mend table_exist;

%macro table_exist_check(order);
%put ************************** Table Existence Check ****************************************;
data _null_;
set qc_list(where=(hierarchy=&order.));
call symput('region', country);

data tmp;
set qc_list(where=(country="®ion."))  end=eof;
if hierarchy<=&order.;

data _NULL_;
set tmp end=eof;
if eof then do;
call symput('ntables', _N_);
end;
run;

%do i=1 %to &ntables. ;
data _null_;
set tmp;
if _N_=&i.;
call symput('tablename',output_table);
run;
%table_exist(tablename=&tablename.);
%end;
%mend table_exist_check;

%macro drop_date_check(order);
%put ************************** Drop_dt / Posting_dt Check ****************************************;
data _null_;
set qc_list(where=(hierarchy=&order.));
call symput('campaign_name', campaign);
call symput('expect_dropdate', expect_dropdate);
call symput('output_table', output_table);
run;

proc sql errorstop;
select distinct drop_dt format=yymmddN8. into :stg_dropdate
from smb_prod.stg_promo_history
where camp_cd="&campcode" and curr_flag='Y';

select distinct posting_dt into :posting_dt
from smb_prod.&output_table;

select count(0)  into  :stg_counts
from smb_prod.stg_promo_history
where camp_cd="&campcode" and curr_flag='Y';

select count(0) into :table_counts
from smb_prod.&output_table;
quit;

%if &stg_counts. =0  %then %do ;
%put No Counts in stg_promo_history table for &campaign_name.;
data _null_;
call symput('MAMsg', 'Check Counts, No Counts in stg_promo_history for this campaign');  call symput('SYSCC', 1012);stop; run;
%abort ;
%end;

%if &table_counts. =0  %then %do ;
%put No Counts in output table for &campaign_name.;
data _null_;
call symput('MAMsg', 'Check Counts, No Counts in output table for this campaign');  call symput('SYSCC', 1012);stop; run;
%abort ;
%end;

%if &table_counts. ne &stg_counts.  %then %do ;
%put Inconsistent counts in stg_promo_history and output table for &campaign_name.;
data _null_;
call symput('MAMsg', 'Check Counts, No Counts in output table for this campaign');  call symput('SYSCC', 1012);stop; run;
%abort ;
%end;

%if &stg_dropdate. ne &expect_dropdate. %then %do;
data _null_;
call symput('MAMsg', 'Check Drop_dt, unexpected date in stg_promo_history');  call symput('SYSCC', 1012);run;
%abort ;
%end;

%if &posting_dt. ne &expect_dropdate. %then %do;
data _null_;
call symput('MAMsg', 'Check Drop_dt, unexpected posting_dt in output table');  call symput('SYSCC', 1012);run;
%abort ;
%end;

%mend drop_date_check;

%macro data_check(order);
%put ************************** Data Check ****************************************;
data _null_;
set qc_list(where=(hierarchy=&order.));
call symput('region', country);

data tmp;
set qc_list(where=(country="®ion."))  end=eof;
if hierarchy<=&order.;

data _NULL_;
set tmp end=eof;
if eof then do;
call symput('ntables', _N_);
end;

data tmp1;
format campaign_name $100. country_cd $2. email_addr $varying250. hierarchy 8.0;
stop;
run;

%do i=1 %to &ntables. ;
data _null_;
set tmp;
if _N_=&i.;
call symput('tablename',output_table);
call symput('hierarchy', hierarchy);
run;

data tmp2;
format campaign_name $100. country_cd $2. email_addr $varying250. hierarchy 8.0;
set smb_prod.&tablename.(keep=campaign_name country_cd email_addr);
hierarchy=&hierarchy.;
data tmp1;
set tmp1 tmp2;
run;
%end;

proc sql errorstop;
select count(cats(country_cd,email_addr)), count(distinct cats(country_cd,email_addr))  into :counts1 , :counts2
from tmp1
where hierarchy=&order. ;

select count(cats(country_cd,email_addr)), count(distinct cats(country_cd,email_addr))  into :counts3 , :counts4
from tmp1;

select distinct campaign_name into :dup_campaign separated by ' , '
from tmp1
where cats(country_cd, email_addr) in
( select cats(country_cd, email_addr)
from tmp1
group by cats(country_cd, email_addr)
having count(distinct hierarchy)>1
) ;
quit;

%if &counts1. ne &counts2.  %then %do ;
%put ************************** Error ****************************************;
%put Duplicate Country_cd and Email_addr in output table for &campaign_name.;
data _null_;
call symput('MAMsg', 'Duplicate Country_cd and Email_addr in output table for this campaign');  call symput('SYSCC', 1012);stop; run;
%abort ;
%end;
%else
%put Data Check: Distinct country_cd and Email_addr for this campaign;

%if &counts3. ne &counts4. %then %do;
%put ************************** Error ****************************************;
%put Duplicate Country_cd and Email_addr for this campaign and lower hierarchy campaign;
%put Duplicate campaigns are : &dup_campaign. ;
data _null_;
call symput('MAMsg', 'Duplicate Country_cd and Email_addr for this campaign and lower hierarchy campaign');  call symput('SYSCC', 1012);run;
%abort ;
%end;
%else
%put Data Check: Distinct country_cd and Email_addr for this campaign and lower hierarchy campaign;

%mend data_check;

%macro file_output(order);
data _null_;
set qc_list(where=(hierarchy=&order.));
call symput('output_table', output_table);
run;

proc sql errorstop;
create table vars as
select  name
from dictionary.columns
where  upcase(memname)="&output_table.";
quit;

proc transpose data=vars out=header(drop=_name_ _label_);
VAR name;
quit;

%mend file_output;

%import_qc();
%filepath_check(path=&path.);
%table_exist_check(order=&inorder.);
%drop_date_check(order=&inorder.);
%data_check(order=&inorder.);
%file_output(order=&inorder.);

proc printto;
run;

libname _ALL_ clear;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: