0

我有 2 个表,T1 包含 10 个变量和 T2。包含 100 个变量。我必须通过将 T1 10 个变量乘以 T2 的相同 10 个变量(仅姓氏更改)来求和。我想将变量名称保持在循环中并输出为 TOTAL_。

例如:应用程序将替换为:store、ramp、abhi、coast、recyc、ind 等。

proc sql;
create table OHDT as 
select T2.Model,T2.Age,

sum( T1.Com_Pass   * T2.Com_Pass_app
    ,T1.Com_Frt    * T2.Com_Frt_app
    ,T1.Com_Other  * T2.Com_Other_app
    ,T1.BG_Corp    * T2.BG_Corp_app
    ,T1.BG_Fract   * T2.BG_Fract_app
    ,T1.BG_Gov     * T2.BG_Gov_app
    ,T1.BG_Other   * T2.BG_Other_app
    ,T1.BG_Owner   * T2.BG_Owner_app
    ,T1.BG_Part    * T2.BG_Part_app
    ,T1.mil_Agri   * T2.mil_Agri_app )   as Total_app,

from Market T1
inner join master T2
on 
T1.Model= T2.Model and T1.Age=T2.Age;
quit;
4

1 回答 1

0

您可以使用宏来遍历您的列表并为您生成 SQL。

%macro create_OHDT(apps);
%let n=%sysfunc(countw(&apps));
proc sql;
create table OHDT as 
select T2.Model,T2.Age

%do i=1 %to &n;
%let app = %scan(&apps,&i);
,sum( T1.Com_Pass   * T2.Com_Pass_&app
    ,T1.Com_Frt    * T2.Com_Frt_&app
    ,T1.Com_Other  * T2.Com_Other_&app
    ,T1.BG_Corp    * T2.BG_Corp_&app
    ,T1.BG_Fract   * T2.BG_Fract_&app
    ,T1.BG_Gov     * T2.BG_Gov_&app
    ,T1.BG_Other   * T2.BG_Other_&app
    ,T1.BG_Owner   * T2.BG_Owner_&app
    ,T1.BG_Part    * T2.BG_Part_&app
    ,T1.mil_Agri   * T2.mil_Agri_&app )   as Total_&app

%end;
from Market T1
inner join master T2
on 
T1.Model= T2.Model and T1.Age=T2.Age;
quit;
%mend;
%create_OHDT(store ramp abhi coast recyc ind);
于 2013-09-27T15:29:19.850 回答