<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: if x then do y... else do z in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/if-x-then-do-y-else-do-z/m-p/460454#M117042</link>
    <description>&lt;P&gt;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.&amp;nbsp; The second kind of statements are program control statements (IF-THEN/ELSE, DO, function calls) that are executed within a DATA step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 07 May 2018 14:56:43 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2018-05-07T14:56:43Z</dc:date>
    <item>
      <title>if x then do y... else do z</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-x-then-do-y-else-do-z/m-p/460430#M117026</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to solve a bug in my code where I have some if...then...do...else...do logic.&amp;nbsp; The bug is that the code 'does' both tasks regardless of passing/failing condition.&amp;nbsp; Specifically, the code is supposed to do nothing in a 'Success' (mkt_count &amp;gt; 350).&amp;nbsp; In a failure, (mkt count &amp;lt; 350), it should spit out an email.&amp;nbsp; However, an email is being generated in both cases (even though no warnings or errors are thrown).&amp;nbsp; Any idea what I'm doing wrong?&amp;nbsp; I'm using SAS Enterprise Guide&amp;nbsp;7&amp;nbsp;if that matters.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;_null_&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; src_tbl='table a';&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; trg_tbl='table b';&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; &amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;mkt_count.&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&amp;gt;&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;350&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;do&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;put&lt;/FONT&gt; &lt;FONT color="#800080" face="Courier New" size="3"&gt;'SUCCESS'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;call&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; symput(&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'src_tbl'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;,&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'table a'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;call&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; symput(&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'trg_tbl'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;,&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'table b'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;end&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;else&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;do&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;put&lt;/FONT&gt; &lt;FONT color="#800080" face="Courier New" size="3"&gt;'FAILURE'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;call&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; symput(&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'src_tbl'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;,&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'table a'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;call&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; symput(&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'trg_tbl'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;,&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'table b'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;filename&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; getdone &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;email&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;from&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'admin@xyz.com'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;to&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=(&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'analyst@xyz.com'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;)&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;subject&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"(Teradata &amp;amp;code_env) Subject line"&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;content_type&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"text/html"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;ods&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;listing&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;close&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;ods&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;html&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;style&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=styles.test &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;body&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=getdone;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;TITLE1&lt;/FONT&gt; &lt;FONT color="#800080" face="Courier New" size="3"&gt;'This email is still under testing as it is throwing false alarm.'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;ODS&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;HTML&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;CLOSE&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;ODS&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;LISTING&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;end&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 May 2018 14:11:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-x-then-do-y-else-do-z/m-p/460430#M117026</guid>
      <dc:creator>ursamajor</dc:creator>
      <dc:date>2018-05-07T14:11:31Z</dc:date>
    </item>
    <item>
      <title>Re: if x then do y... else do z</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-x-then-do-y-else-do-z/m-p/460454#M117042</link>
      <description>&lt;P&gt;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.&amp;nbsp; The second kind of statements are program control statements (IF-THEN/ELSE, DO, function calls) that are executed within a DATA step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 07 May 2018 14:56:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-x-then-do-y-else-do-z/m-p/460454#M117042</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2018-05-07T14:56:43Z</dc:date>
    </item>
    <item>
      <title>Re: if x then do y... else do z</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-x-then-do-y-else-do-z/m-p/460534#M117062</link>
      <description>Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt; 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?</description>
      <pubDate>Mon, 07 May 2018 17:53:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-x-then-do-y-else-do-z/m-p/460534#M117062</guid>
      <dc:creator>ursamajor</dc:creator>
      <dc:date>2018-05-07T17:53:44Z</dc:date>
    </item>
    <item>
      <title>Re: if x then do y... else do z</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-x-then-do-y-else-do-z/m-p/460543#M117068</link>
      <description>&lt;P&gt;I've never done this,&amp;nbsp;so someone will have to help you with the details. What I would suggest,&amp;nbsp;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&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro SendEmail();&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;%mend;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;then you can process some data and execute the macro only when some condition is true,&amp;nbsp;as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data _null_;&lt;/P&gt;
&lt;P&gt;if &amp;amp;mkt_count &amp;gt; 350 then do;&amp;nbsp; /* ??? how&amp;nbsp;does &amp;amp;mkt_count get defined??? */&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;put 'SUCCESS';&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;else do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;put 'FAILURE';&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;call execute("%&lt;SPAN&gt;SendEmail()");&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 May 2018 18:22:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-x-then-do-y-else-do-z/m-p/460543#M117068</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2018-05-07T18:22:48Z</dc:date>
    </item>
    <item>
      <title>Re: if x then do y... else do z</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-x-then-do-y-else-do-z/m-p/613269#M179090</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;provided a great explanation about global statements versus those that are scoped to your DATA step.&amp;nbsp; Here's an example that shows how to conditionally send an e-mail based on the values that you find in your data.&amp;nbsp; This approach &lt;A href="https://blogs.sas.com/content/sasdummy/2018/07/05/if-then-else-sas-programs/" target="_self"&gt;uses the %IF-%THEN-%ELSE statements in open code&lt;/A&gt;, which has been possible since SAS 9.4 Maint 5 (released in 2017).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Pretend we are counting records where RESULT=1 */
proc sql noprint;
  select count(result) into: mkt_count
    from source.dataset
      where result=1;
quit;

/* If beyond a certain threshold, send e-mail */
options nocenter emailhost='yourmailhost.company.com' emailsys=smtp;
%if &amp;amp;mkt_count. &amp;gt; 350 %then
  %do;
    filename msg email to=(
      'analyst@xyz.com'
      )   
      FROM = "admin@xyz.com"
      subject="Alert: Count = &amp;amp;mkt_count., exceeds target"
      type='text/html'
    ;
    ods msoffice2k (id=email)
      file=msg (title="Alert")
      style=dove;
    ods noproctitle;
 
  /* SAS proc to generate the body of the e-mail */
    title "Results distribution";
    proc freq data=source.dataset;
      table result;
    run;

    ods msoffice2k (id=email) close;
  %end;
 /* under threshold, no alert needed */
 %else %do;
  %PUT NOTE: Count is &amp;amp;mkt_count. -- under threshold;
 %end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Dec 2019 16:22:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-x-then-do-y-else-do-z/m-p/613269#M179090</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2019-12-20T16:22:00Z</dc:date>
    </item>
  </channel>
</rss>

