<?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: OR statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/OR-statement/m-p/848536#M335471</link>
    <description>&lt;P&gt;AND and OR are&amp;nbsp;&lt;STRONG&gt;not&lt;/STRONG&gt; &lt;EM&gt;statements&lt;/EM&gt;, they are logical&amp;nbsp;&lt;EM&gt;operators&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;DATA, SET, WHERE and RUN in your example are statements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your multiple &lt;U&gt;unnecessary&lt;/U&gt; parentheses cause the limited intelligence of the Enhanced Editor to run out of steam. What's even worse, they make your code hard to understand, and must therefore be avoided.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where
  year(datepart(m_date)) = 2022 and year(datepart(c_date)) &amp;gt;= 2022
  or
  year(datepart(m_date)) = . and year(datepart(c_date)) = 2022
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is logically equivalent and much easier to read, as the logical structure is expressed by using separate lines for parts of the condition. Also note the consistent use of whitespace, and that the evaluation of m_date comes before that of c_date in both parts. Shuffling variables around between code parts can (and&amp;nbsp;&lt;EM&gt;will&lt;/EM&gt;) cause mistakes in the future when code is maintained.&lt;/P&gt;</description>
    <pubDate>Thu, 08 Dec 2022 14:34:26 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2022-12-08T14:34:26Z</dc:date>
    <item>
      <title>OR statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/OR-statement/m-p/848524#M335465</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I think the code I run below doesn't work properly because the latest AND statement is not BLUE but grey instead. When I run the code, I don't get any errors though. Is there anything wrong with the syntax?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data data2;
set data1;
where (( (year(datepart(m_date))= 2022) AND year (datepart(c_date)) &amp;gt;= 2022 )
OR ( (year(datepart(c_date))=2022) AND ( missing (year(datepart(m_date))))));
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2022 12:24:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/OR-statement/m-p/848524#M335465</guid>
      <dc:creator>znhnm</dc:creator>
      <dc:date>2022-12-08T12:24:06Z</dc:date>
    </item>
    <item>
      <title>Re: OR statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/OR-statement/m-p/848525#M335466</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/411017"&gt;@znhnm&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;When I run the code, I don't get any errors though. Is there anything wrong with the syntax?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Are there errors in the log? If so, show us the log (the ENTIRE log for this data step, not selected parts of the log for this data step)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If there are no errors in the log, then there is nothing wrong with the syntax, so there must be something wrong with the logic, but I have no idea what logic you want. Can you explain the logic in words?&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2022 12:26:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/OR-statement/m-p/848525#M335466</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-12-08T12:26:41Z</dc:date>
    </item>
    <item>
      <title>Re: OR statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/OR-statement/m-p/848527#M335467</link>
      <description>&lt;P&gt;Besides the points by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;, I think you have a few parentheses you don't need. This is simpler and probably easier to debug.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data data2;
	set data1;
	where ( year(datepart(m_date)) = 2022 AND year(datepart(c_date)) &amp;gt;= 2022 )
	   OR ( year(datepart(c_date)) = 2022 AND missing(year(datepart(_date))) ) 
	;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Dec 2022 12:30:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/OR-statement/m-p/848527#M335467</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-12-08T12:30:27Z</dc:date>
    </item>
    <item>
      <title>Re: OR statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/OR-statement/m-p/848529#M335468</link>
      <description>&lt;P&gt;The editor (IDE) does the colorizing of your code, and it tries to do it well.&amp;nbsp; But the IDE just has simple coloring rules. It doesn't have all the complexity of the SAS compiler. Sometimes it colors code wrong.&amp;nbsp; But this doesn't effect how the code compiles and executes.&amp;nbsp; So when code is colored wrong, it's helpful to take a second look at it to make sure it's correct, but you don't need to worry about the color.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2022 12:45:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/OR-statement/m-p/848529#M335468</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-12-08T12:45:37Z</dc:date>
    </item>
    <item>
      <title>Re: OR statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/OR-statement/m-p/848536#M335471</link>
      <description>&lt;P&gt;AND and OR are&amp;nbsp;&lt;STRONG&gt;not&lt;/STRONG&gt; &lt;EM&gt;statements&lt;/EM&gt;, they are logical&amp;nbsp;&lt;EM&gt;operators&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;DATA, SET, WHERE and RUN in your example are statements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your multiple &lt;U&gt;unnecessary&lt;/U&gt; parentheses cause the limited intelligence of the Enhanced Editor to run out of steam. What's even worse, they make your code hard to understand, and must therefore be avoided.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where
  year(datepart(m_date)) = 2022 and year(datepart(c_date)) &amp;gt;= 2022
  or
  year(datepart(m_date)) = . and year(datepart(c_date)) = 2022
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is logically equivalent and much easier to read, as the logical structure is expressed by using separate lines for parts of the condition. Also note the consistent use of whitespace, and that the evaluation of m_date comes before that of c_date in both parts. Shuffling variables around between code parts can (and&amp;nbsp;&lt;EM&gt;will&lt;/EM&gt;) cause mistakes in the future when code is maintained.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2022 14:34:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/OR-statement/m-p/848536#M335471</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-12-08T14:34:26Z</dc:date>
    </item>
    <item>
      <title>Re: OR statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/OR-statement/m-p/848543#M335474</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;I like the multi-line approach, but in order to understand this, you need to know the precedence of AND and OR:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where
  year(datepart(m_date)) = 2022 and year(datepart(c_date)) &amp;gt;= 2022
  or
  year(datepart(m_date)) = . and year(datepart(c_date)) = 2022
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I often use extra parentheses, just to make the order of operations explicit, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where
  (year(datepart(m_date)) = 2022 and year(datepart(c_date)) &amp;gt;= 2022)
  or
  (year(datepart(m_date)) = . and year(datepart(c_date)) = 2022)
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or even:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where
  (
   year(datepart(m_date)) = 2022 and year(datepart(c_date)) &amp;gt;= 2022
  )
  or
  (
  year(datepart(m_date)) = . and year(datepart(c_date)) = 2022
  )
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Dec 2022 15:12:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/OR-statement/m-p/848543#M335474</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-12-08T15:12:09Z</dc:date>
    </item>
    <item>
      <title>Re: OR statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/OR-statement/m-p/848550#M335476</link>
      <description>&lt;P&gt;Please do not rely on syntax highlighting to determine valid code. It is helpful and odd coloring &lt;STRONG&gt;may&lt;/STRONG&gt; sometimes indicate the presence of characters off screen to the right. But the highlighter is sometime wrong. FWIW none of the "AND" appear in blue when pasted into my SAS session.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Not "wrong" but the only way for a Year(datepart(somevariable)) can be missing is if the Somevariable is missing (or not a valid datetime to begin with resulting in a "year" that is out of range for the datepart function) so you could simplify to&lt;/P&gt;
&lt;PRE&gt;missing(m_date)&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Dec 2022 16:11:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/OR-statement/m-p/848550#M335476</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-12-08T16:11:08Z</dc:date>
    </item>
    <item>
      <title>Re: OR statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/OR-statement/m-p/848598#M335500</link>
      <description>&lt;P&gt;General advice:&amp;nbsp; pick a solution you feel comfortable with.&amp;nbsp; Don't agree with a solution that you don't understand.&amp;nbsp; If you really want to simplify the code, you could use:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data data2;
set data1;
if year(datepart(m_date))=2022 AND year (datepart(c_date)) &amp;gt;= 2022 then output;
if year(datepart(c_date))=2022 AND missing(year(datepart(m_date))) then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It's clumsy, since you can't add more programming statements to the same DATA step (because the observations have already been output by the OUTPUT statement).&amp;nbsp; But if it makes the code readable in your eyes, that's an important feature.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2022 18:20:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/OR-statement/m-p/848598#M335500</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2022-12-08T18:20:56Z</dc:date>
    </item>
  </channel>
</rss>

