user2518751
user2518751

Reputation: 735

If condition is true then execute a macro

I have a macro I wanto to execute only when a certain table is not empty (Otherwise the macro will raise an exception).

I've tried to to something similar to this inside the macro, before the action begins:

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

%else  %put TABLE IS EMPTY!;

It doesn't seem to work...

What am I missing?

Any better solution?

Thanks!


EDITED

This is my code below. What I want to do is, when variable MIN_X_INSURER_SERIAL_NO is null, meanning table is empty - I don't want the macro to run. Simlpe as that.

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 );

Upvotes: 2

Views: 4176

Answers (2)

Dmitry Shopin
Dmitry Shopin

Reputation: 1763

You just need to put your condition inside the macro not outside of it. Like this:

%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 );

Upvotes: 1

mvherweg
mvherweg

Reputation: 1283

Your first line, then and do also require the percentage sign:

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

Note that you can only use %if %then statements within a macro, not in open code. Also, i'm assuming that some_variable indicates number of rows? And that you want to pass on this way whether the table is empty or not. In that case, you probably want >0 instead of >=0.

Upvotes: 2

Related Questions