如果condition为true,则执行宏

时间:2013-12-04 07:56:59

标签: sas

我有一个我想要只在某个表不为空时执行的宏 (否则宏会引发异常)。

我试图在宏内部进行类似的操作, 在行动开始之前:

%if &some_variable >= 0 %then %do;
%put TABLE IS NOT EMPTY;
%macro_name(var1= &var1,var2 = &var2);
%end;

%else  %put TABLE IS EMPTY!;

它似乎不起作用......

我错过了什么?

有更好的解决方案吗?

谢谢!


EDITED

这是我的代码如下。 我想做的是, 当变量MIN_X_INSURER_SERIAL_NO为null时,意义表为空 - 我不希望宏运行。 Simlpe就是这样。

proc sql;
select min(X_INSURER_SERIAL_NO), 
max(X_INSURER_SERIAL_NO)-min(X_INSURER_SERIAL_NO)+1   
into: MIN_X_INSURER_SERIAL_NO,  :diff
from STG.FICTIVE_PREMID_AV_POL;
quit;



proc sql;
create table  STG.FICTIVE_PREMID_AV_POLICIES like STG.FICTIVE_PREMID_REGULAR_POLICIES;quit;



%macro fictive_premid_AV (MIN_X_INSURER_SERIAL_NO = , diff = );



%do i = &MIN_X_INSURER_SERIAL_NO %to &diff;
%put value of i:  &i;
proc sql;
select 
distinct mIN_X_POLICY_VERSION, max_X_POLICY_VERSION   into:     mIN_X_POLICY_VERSION_av,  :max_X_POLICY_VERSION_av
from STG.FICTIVE_PREMID_AV_POL where x_insurer_serial_no = &i;
quit;



proc sql;
insert into STG.FICTIVE_PREMID_AV_POLICIES
select  X_POLICY_NO,  X_INSURANCE_PRODUCT_CD,   OTHER_AGR_ID ,X_POLICY_VERSION, max_X_POLICY_VERSION,  0     as     FICTIVE_IND, x_insurer_serial_no,
x_product_section_agr_cd,
x_section_cd,
x_product_section_cd
from STG.FICTIVE_PREMID_AV_POL
where X_INSURER_SERIAL_NO = &i
and   X_POLICY_VERSION  =  min_X_POLICY_VERSION ;
quit;

%do  j =  &mIN_X_POLICY_VERSION_av %to %eval(&max_X_POLICY_VERSION_av-1) ;


%put value of j:  &j;
proc sql;
create table fictive_premid_t as
select * from 
(
select X_POLICY_NO,  X_INSURANCE_PRODUCT_CD,   %eval(&j+1)   as X_POLICY_VERSION, OTHER_AGR_ID,     max_X_POLICY_VERSION,  1 as FICTIVE_IND, x_insurer_serial_no,
x_product_section_agr_cd,
x_section_cd,
x_product_section_cd
from STG.FICTIVE_PREMID_AV_POLICIES
where  X_POLICY_VERSION = &j 
and x_insurer_serial_no = &i

except
select X_POLICY_NO,  X_INSURANCE_PRODUCT_CD, %eval(&j+1)   as X_POLICY_VERSION, OTHER_AGR_ID,     max_X_POLICY_VERSION,  1 as FICTIVE_IND, x_insurer_serial_no,
x_product_section_agr_cd,
x_section_cd,
x_product_section_cd
from STG.FICTIVE_PREMID_AV_POL
where X_POLICY_VERSION   =  %eval(&j+1) 
and x_insurer_serial_no = &i

)
union 
select X_POLICY_NO,  X_INSURANCE_PRODUCT_CD,%eval(&j+1)   as X_POLICY_VERSION, OTHER_AGR_ID,     max_X_POLICY_VERSION,  0 as FICTIVE_IND, x_insurer_serial_no,
x_product_section_agr_cd,
x_section_cd,
x_product_section_cd
from STG.FICTIVE_PREMID_AV_POL
where X_POLICY_VERSION   =  %eval(&j+1) 
and x_insurer_serial_no = &i;



insert into STG.FICTIVE_PREMID_AV_POLICIES
select X_POLICY_NO,  X_INSURANCE_PRODUCT_CD,  OTHER_AGR_ID ,X_POLICY_VERSION, max_X_POLICY_VERSION,     FICTIVE_IND, x_insurer_serial_no,
x_product_section_agr_cd,
x_section_cd,
x_product_section_cd
from fictive_premid_t
where X_POLICY_VERSION<= max_X_POLICY_VERSION;
quit;

%end;

%end;

   %mend;



%fictive_premid_av (MIN_X_INSURER_SERIAL_NO = &MIN_X_INSURER_SERIAL_NO, diff = &diff );

2 个答案:

答案 0 :(得分:1)

你的第一行,然后也需要百分号:

%if &some_variable >= 0 %then %do;

请注意,您只能在宏中使用%if%then语句,而不能在开放代码中使用。 另外,我假设some_variable表示行数?并且您希望以这种方式传递表是否为空。在这种情况下,您可能需要&gt; 0而不是&gt; = 0。

答案 1 :(得分:1)

你只需要把你的条件放在不在它之外的宏中。像这样:

%macro fictive_premid_AV (MIN_X_INSURER_SERIAL_NO = , diff = );

   %if &MIN_X_INSURER_SERIAL_NO >= 0 %then %do;

      <do your macro>

   %end;

  %else %put TABLE IS EMPTY;

%mend fictive_premid_av;

%fictive_premid_av (MIN_X_INSURER_SERIAL_NO = &MIN_X_INSURER_SERIAL_NO, diff = &diff );