我有2个表,我希望通过匹配T1的MODEL和AGE与MODEL和AGE_BT T2进行输出。我已经使用了代码并且得到了空白的输出。输出应该看起来像下面的输出表。
Table:1
MODEL AGE1 AGE2 AGE3 AGE4
SLOT1 0-10 10-20 20-35 35p
SLOT1 0-10 10-20 20-35 35p
SLOT1 0-10 10-20 20-35 35p
SLOT2 0-10 10-20 20-35 35p
SLOT2 0-10 10-20 20-35 35p
SLOT3 0-10 10-20 20-35 35p
Table:2
MODEL AGE_BT FHA
SLOT1 0-10 600
SLOT1 10-20 435
SLOT1 20-35 210
SLOT1 35P 10
SLOT2 0-10 391
SLOT2 10-20 385
SLOT2 20-35 246
SLOT2 35P 0
SLOT3 0-10 391
SLOT3 10-20 385
SLOT3 20-35 246
SLOT3 35P 0
OUTPUT TABLE
MODEL AGE1 AGE2 AGE3 AGE4 SEG_AGE1 SEG_AGE2 SEG_AGE3 SEG_AGE4
SLOT1 0-10 10-20 20-35 35p 600 435 210 10
SLOT1 0-10 10-20 20-35 35p 600 435 210 10
SLOT1 0-10 10-20 20-35 35p 600 435 210 10
SLOT1 0-10 10-20 20-35 35p 600 435 210 10
SLOT1 0-10 10-20 20-35 35p 600 435 210 10
SLOT1 0-10 10-20 20-35 35p 600 435 210 10
SLOT2 0-10 10-20 20-35 35p 391 385 246 0
SLOT2 0-10 10-20 20-35 35p 391 385 246 0
SLOT2 0-10 10-20 20-35 35p 391 385 246 0
SLOT2 0-10 10-20 20-35 35p 391 385 246 0
%macro age(ageap);
%let n=%sysfunc(countw(&ageap));
proc sql;
create table segment_test2_ as
select distinct T1.*
%do i=1 %to &n;
%let ap = %scan(&ageap,&i);
,T2.FHA as SEG_&ap
%end;
from segment_test1 T1
left join ANNUAL_FH T2
on T1.Model=T2.Model and T1.&ap=T2.AGE_BT;
quit;
%mend;
%age(AGE1 AGE2 AGE3 AGE4);
答案 0 :(得分:3)
使用MPRINT,您应该看到以下代码:
74 options mprint;
75 %age(AGE1 AGE2 AGE3 AGE4);
MPRINT(AGE): proc sql;
MPRINT(AGE): create table segment_test2_ as select distinct T1.* ,T2.FHA as SEG_AGE1 ,T2.FHA as SEG_AGE2 ,T2.FHA as SEG_AGE3
,T2.FHA as SEG_AGE4 from segment_test1 T1
left join ANNUAL_FH T2 on T1.Model=T2.Model and T1.AGE4=T2.AGE_BT;
最后注意T1.AGE4=T2.AGE_BT
。根据您的样本,AGE_BT必须保持左对齐和低位,可能等于AGE4,如下所示:
T1.AGE4=lowcase(left(T2.AGE_BT))
答案 1 :(得分:0)
OPTIONS SYMBOLGEN MPRINT MLOGIC;
%macro AG;
%LET AA=1;
%do i=0 %to 3;
%let apS = %EVAL(&AA+&i);
proc sql;
create table SEGMENT_&apS as
select distinct T1.*
,T2.Average_FH/12 as Segment_&aps
from segment T1
left join FINAL.ANNUAL_FH_AVERAGE T2
on T1.Model=T2.Model and T1.age&aps=T2.Age_bracket ;
quit;
%end;
%mend AG;
%AG