SAS循环或具有变量名称后缀的数组

时间:2013-09-27 14:48:18

标签: sas

我有2个表,T1包含10个变量和T2。包含100个变量。我必须通过将T1 10变量与相同10个变量的T2相乘而仅使用姓氏来进行求和。我想将变量名保存在循环中并输出为TOTAL_。

Ex: app 将替换为:store,ramp,abhi,coast,recycle,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;

1 个答案:

答案 0 :(得分: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);