DATA Step, Macro, Functions and more

if x then do y... else do z

Reply
New Contributor
Posts: 2

if x then do y... else do z

Hello,

 

I'm trying to solve a bug in my code where I have some if...then...do...else...do logic.  The bug is that the code 'does' both tasks regardless of passing/failing condition.  Specifically, the code is supposed to do nothing in a 'Success' (mkt_count > 350).  In a failure, (mkt count < 350), it should spit out an email.  However, an email is being generated in both cases (even though no warnings or errors are thrown).  Any idea what I'm doing wrong?  I'm using SAS Enterprise Guide 7 if that matters.

 

data _null_;

%let src_tbl='table a';

%let trg_tbl='table b';

if &mkt_count.>350 then

do;

put 'SUCCESS';

call symput('src_tbl','table a');

call symput('trg_tbl','table b');

end;

else

do;

put 'FAILURE';

call symput('src_tbl','table a');

call symput('trg_tbl','table b');

filename getdone email

from='admin@xyz.com'

to=(

'analyst@xyz.com'

)

subject="(Teradata &code_env) Subject line"

content_type="text/html";

ods listing close;

ods html style=styles.test body=getdone;

TITLE1 'This email is still under testing as it is throwing false alarm.';

ODS HTML CLOSE;

ODS LISTING;

end;

run;

 

SAS Super FREQ
Posts: 4,272

Re: if x then do y... else do z

Posted in reply to ursamajor

SAS has two kinds of statements. GLOBAL statement such as %LET, FILENAME, TITLE, and ODS statements are always executed. Think of them as being "lifted out" of a procedure.  The second kind of statements are program control statements (IF-THEN/ELSE, DO, function calls) that are executed within a DATA step.

 

In your example, the global statements are executed regardless of the logic of the IF/THEN/ELSE conditions. Run this little example to see that the LIBNAME statement executes even though it is in an ELSE block that never executes:

 

data _NULL_;
if 1 then do;
   x=1;
end;
else do;
   x=2;
   libname foo "C:/test/";
end;
run;

/* display value of libname */
data _null_;
   length Lname $ 100;
   Lname=pathname('foo','L');
   put Lname=;
run;
New Contributor
Posts: 2

Re: if x then do y... else do z

Thank you @Rick_SAS for that explanation. It certainly makes sense now. But what do you suggest would be a workaround? Is there any way to make sure that ELSE only executes when the IF condition is not met?
SAS Super FREQ
Posts: 4,272

Re: if x then do y... else do z

[ Edited ]
Posted in reply to ursamajor

I've never done this, so someone will have to help you with the details. What I would suggest, however, is to wrap the relevant "send email" statements into a macro, and then call the macro conditionally. For example, if the code to send the email is in the macro

 

%macro SendEmail();

...

%mend;

 

then you can process some data and execute the macro only when some condition is true, as follows:

 

data _null_;

if &mkt_count > 350 then do;  /* ??? how does &mkt_count get defined??? */

   put 'SUCCESS';

end;

else do;

   put 'FAILURE';

   call execute("%SendEmail()");

end;

run;

 

Ask a Question
Discussion stats
  • 3 replies
  • 109 views
  • 0 likes
  • 2 in conversation