1. 数据准备程序
options formchar=' ____________';
libname oildata "F:\oil";
libname strategy "F:\strategy";
/*筛选数据，建立各种油品的数据集*/
%macro dataselect(city=,material=,data=);
data &data;
  set oildata.data_company;
keep DATE plant QL QKG TYPE NO INCOME;
where place =&city;
run;
/*运用sql过程步，计算出每个加油站销售汽油的价格*/
proc sql;
  create table &data as
  select DATE,plant,QL,INCOME,NO,TYPE INCOME/QL as price from &data;
run;
quit;
%mend dataselect;
/*数据转置*/
%macro transmacro(data=,out=);/*用于对数据集变量的转置，data=输入数据集，out=输出数据集，id=转置变量*/
proc sort data=&data;by plant NO;run;/*对需要转置数据的分组变量进行排序*/
proc transpose data=&data out=&out;/*利用transpose过程步进行数据集变量的转置*/
  by plant NO;
id &id;
run;
%mend transmacro;
%transmacro(data=data93,out=data93,id=DATE);
%transmacro(data=data97,out=data97,id=DATE);
%transmacro(data=data0,out=data0,id=DATE);
/*得到总的平均数据*/
%macro getmean(data=,out=);
  proc sort data=&data;
by _NAME_ ;
run;
  proc means data=&data mean;
    var _numeric_;
    by _NAME_ ;
    output out=&out;
  run;
  data &out;
set &out;
drop _TYPE_ _FREQ_;
if _STAT_='MEAN';
run;
  proc transpose data=&out out=&out;
    id _NAME_;
  run;
data &out(drop=_NAME_);
set &out;
DATE=_n_;
label INCOME='销售额' QL='消费量' price='销售价格';
run;
%mend getmean;
%getmean(data=data93,out=modeling93);
%getmean(data=data97,out=modeling97);
%getmean(data=data0,out=modeling0);
/*得到各个位置的均衡数据集*/
%macro getall(data=);
  proc sort data=&data;
by _NAME_ NO;
run;
  proc means data=&data mean;
    var _numeric_;
    by _NAME_ NO;
    output out=&data;
  run;
  data &data;
    set &data;
    if _STAT_="MEAN";
    drop _TYPE_ _FREQ_ _STAT_;
  run;
  proc transpose data=&data out=&data;
    id NO;
    by _NAME_;
  run;
%mend getall;
%getall(data=data93);
%getall(data=data97);
%getall(data=data0);
/*将各个变量合并到一个数据集中，构成三种油品的三个数据集*/
%macro classify(data=);
data A;
    set &data;
    DATE=_n_;
    where _NAME_='INCOME';
    rename A01=A01_V A02=A02_V A03=A03_V  A04=A04_V;
    drop A10 A11 _NAME_;
  run;
  data B;
    set &data;
    DATE=_n_;
    where _NAME_='QL';
    rename A01=A01_Q A02=A02_Q A03=A03_Q A04=A04_Q;
    drop A10 A11 _NAME_;
  run;
data C;
    set &data;
    DATE=_n_;
    rename A01=A01_D A02=A02_D A03=A03_D A04=A04_D;
    drop A10 A11 _NAME_;
  run;
data D;
    set &data;
    DATE=_n_;
    where _NAME_='price';
    rename A01=A01_P A02=A02_P A03=A03_P A04=A04_P;
    drop A10 A11 _NAME_;
  run;
data &data;merge A B C D;run;
%mend classify;
%classify(data=data93);
%classify(data=data97);
%classify(data=data0);
2. 整体和分位置建模
/*建立总的价格调整模型*/
/*拟合各个油品的目标方程*/
%macro builddestination(data=);
proc sql;
create table &data as select INCOME,QL,price,DATE,leadprice,price/leadprice as x from &data;
run;quit;
proc sql;
create table &data as select INCOME,QL,price,DATE,leadprice,x, log(INCOME) as log_v,log(QL) as log_Q,log(leadprice) as log_ leadprice,log(price) as log_P,log(x) as log_x
from &data;
quit;
proc glm data=&data;
  model  log_V =log_Q log_ leadprice log_x;
run;quit;
%mend builddestination;
%builddestination(data=oildata.modeling93);
%builddestination(data=oildata.modeling97);
%builddestination(data=oildata.modeling0);
proc glm data=oildata.modeling0;
   model  log_V =log_Q log_ leadprice log_x/noint;
run;quit;
/*拟合各个油品的约束方程*/
%macro buildrestrict(data=);
proc sql;
create table &data as select INCOME,QL,price,DATE, leadprice,x,log(-log_x) as llog_x, log(log_Q) as llog_Q,log(log_ leadprice) as llog_leadprice from &data;
run;quit;
proc reg data=&data;
model llog_Q=llog_x llog_ leadprice /noint;
run;quit;
%mend buildrestrict;
%buildrestrict(data=oildata.modeling93);
%buildrestrict(data=oildata.modeling97);
%buildrestrict(data=oildata.modeling0);
/*三种油品的总模型拟合完毕*/
/*建立各个油品，对应四个位置的调价策略*/
/*建立各种油品的目标方程与约束方程*/
%macro compute(V=,Q=,P=,data=);
proc sql;
  create table working as select DATE,&V,&Q,&P, leadprice,&P/ leadprice as x from &data;
  create table working as 
select DATE,log(&v) as log_v,log(&Q) as log_Q, log(leadprice) as log_ leadprice,log(x) as log_x from  working;
  create table working as select DATE,log_v,log_Q,log_leadprice,log_x,log(log_Q) as llog_Q, log(log_ leadprice) as llog_leadprice,log(-log_x) as llog_x from working;
run;quit;
proc glm data=working;
  model log_v=log_Q log_leadprice log_x/noint;
run;quit;
proc reg data=working;
  model llog_Q=llog_leadprice llog_x/noint;
run;quit;
%mend compute;
%compute(V=A01_V,Q=A01_Q,P=A01_P,data=oildata.data93);
%compute(V=A02_V,Q=A02_Q,P=A02_P,data=oildata.data93);
%compute(V=A03_V,Q=A03_Q,P=A03_P,data=oildata.data93);
%compute(V=A04_V,Q=A04_Q,P=A04_P,data=oildata.data93);
%compute(V=A01_V,Q=A01_Q,P=A01_P,data=oildata.data0);
%compute(V=A02_V,Q=A02_Q,P=A02_P,data=oildata.data0);
%compute(V=A03_V,Q=A03_Q,P=A03_P,data=oildata.data0);
%compute(V=A04_V,Q=A04_Q,P=A04_P,data=oildata.data0);
%compute(V=A01_V,Q=A01_Q,P=A01_P,data=oildata.data97);
%compute(V=A02_V,Q=A02_Q,P=A02_P,data=oildata.data97);
%compute(V=A03_V,Q=A03_Q,P=A03_P,data=oildata.data97);
%compute(V=A04_V,Q=A04_Q,P=A04_P,data=oildata.data97);