<?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: Variable defined in %let won't work in WHERE statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Variable-defined-in-let-won-t-work-in-WHERE-statement/m-p/590160#M168871</link>
    <description>&lt;P&gt;So&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;has pointed out the error, but it might be helpful to follow certain advice before you try to turn code into code with macro variables and/or code with macros.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The advice is to get the code to work on one or two situations without macros and without macro variables. If your code doesn't work without macros and without macro variables, then it will not work if you are using macros and macro variables. If it does work without macros and without macro variables, then you have a good chance it will work with macros and with macro variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 19 Sep 2019 19:21:15 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2019-09-19T19:21:15Z</dc:date>
    <item>
      <title>Variable defined in %let won't work in WHERE statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variable-defined-in-let-won-t-work-in-WHERE-statement/m-p/590097#M168853</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm automating a report using SAS and have created some %let statements to define dates. I've found that variables defined in the %let statements won't work when used in a WHERE statement in a datastep but will work in an if statement.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone explain why this is?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let date1 = 01JAN2018 ;
%let date2 = 01JAN2019 ;

data d2; 
set d1 ;
where "&amp;amp;date1"d =&amp;lt; d1_date &amp;lt; "&amp;amp;date2"d ;
run; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Sep 2019 16:13:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variable-defined-in-let-won-t-work-in-WHERE-statement/m-p/590097#M168853</guid>
      <dc:creator>RoddyJ</dc:creator>
      <dc:date>2019-09-19T16:13:39Z</dc:date>
    </item>
    <item>
      <title>Re: Variable defined in %let won't work in WHERE statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variable-defined-in-let-won-t-work-in-WHERE-statement/m-p/590104#M168854</link>
      <description>&lt;P&gt;it should work fine, something else is going wrong. Post your log from that portion of the code that shows the error.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Common mistakes are single quotes, not having the date as a literal or having a datetime variable instead of a date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/289103"&gt;@RoddyJ&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm automating a report using SAS and have created some %let statements to define dates. I've found that variables defined in the %let statements won't work when used in a WHERE statement in a datastep but will work in an if statement.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anyone explain why this is?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let date1 = 01JAN2018 ;
%let date2 = 01JAN2019 ;

data d2; 
set d1 ;
where "&amp;amp;date1"d =&amp;lt; d1_date &amp;lt; "&amp;amp;date2"d ;
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2019 16:35:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variable-defined-in-let-won-t-work-in-WHERE-statement/m-p/590104#M168854</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-09-19T16:35:14Z</dc:date>
    </item>
    <item>
      <title>Re: Variable defined in %let won't work in WHERE statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variable-defined-in-let-won-t-work-in-WHERE-statement/m-p/590109#M168856</link>
      <description>&lt;P&gt;Use correct syntax:&lt;/P&gt;
&lt;PRE&gt; 85         where "&amp;amp;date1"d =&amp;lt; d1_date &amp;lt; "&amp;amp;date2"d ;
                             _
                             22
                             76
 ERROR: Syntaxfehler bei Verarbeitung der Where-Bedingung.
 ERROR 22-322: Syntaxfehler, erwartet wird eines der folgenden: ein Name, eine Zeichenkette in Hochkommata, 
               eine numerische Konstante, eine Datetime-Konstante, ein fehlender Wert, INPUT, PUT.  
 
 ERROR 76-322: Syntax error, statement will be ignored.
&lt;/PRE&gt;
&lt;P&gt;The correct syntax for the "less or equal" operator will work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data d1;
input d1_date date9.;
datalines;
01jan2017
30aug2018
;

%let date1 = 01JAN2018 ;
%let date2 = 01JAN2019 ;

data d2; 
set d1 ;
where "&amp;amp;date1"d &amp;lt;= d1_date &amp;lt; "&amp;amp;date2"d ;
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Crucial part of the log:&lt;/P&gt;
&lt;PRE&gt;83         data d2;
84         set d1 ;
85         where "&amp;amp;date1"d &amp;lt;= d1_date &amp;lt; "&amp;amp;date2"d ;
86         run;
 
NOTE: There were 1 observations read from the data set WORK.D1.
      WHERE (d1_date&amp;gt;='01JAN2018'D and d1_date&amp;lt;'01JAN2019'D);
NOTE: The data set WORK.D2 has 1 observations and 1 variables.
&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Sep 2019 16:48:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variable-defined-in-let-won-t-work-in-WHERE-statement/m-p/590109#M168856</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-09-19T16:48:02Z</dc:date>
    </item>
    <item>
      <title>Re: Variable defined in %let won't work in WHERE statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variable-defined-in-let-won-t-work-in-WHERE-statement/m-p/590150#M168869</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/289103"&gt;@RoddyJ&lt;/a&gt;: What&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;said.&lt;/P&gt;
&lt;P&gt;In the WHERE clause (no matter whether in the DATA step or a proc), the &amp;lt; or &amp;gt; operators must preceded the = operator, that is &amp;lt;= and &amp;gt;= are correct, but =&amp;gt; and =&amp;lt; are deemed syntax errors. Note that the IF statement in the DATA step doesn't care, and its compiler evaluates all of the above as correct.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The simplest way around having to remember what must precede what is to use mnemonic operators instead, i.e. LE for &amp;lt;= and GE for &amp;gt;= both in IF and WHERE. Your WHERE statement would be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token statement"&gt;where&lt;/SPAN&gt; &lt;SPAN class="token string"&gt;"&amp;amp;date1"&lt;/SPAN&gt;d LE d1_date LT &lt;SPAN class="token string"&gt;"&amp;amp;date2"&lt;/SPAN&gt;d &lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In fact, when the interval endpoints are included in the WHERE comparison, it's even simpler to use the BETWEEN operator (alas, not available with the IF statement). In your case, the upper endpoint is excluded, but since you're dealing with SAS dates (i.e. the number of days since the beginning of 1960) here, you can still use:&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token statement"&gt;where&lt;/SPAN&gt; d1_date between &lt;SPAN class="token string"&gt;"&amp;amp;date1"&lt;/SPAN&gt;d and &lt;SPAN class="token string"&gt;"&amp;amp;date2"&lt;/SPAN&gt;d - 1 &lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2019 18:27:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variable-defined-in-let-won-t-work-in-WHERE-statement/m-p/590150#M168869</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-09-19T18:27:02Z</dc:date>
    </item>
    <item>
      <title>Re: Variable defined in %let won't work in WHERE statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variable-defined-in-let-won-t-work-in-WHERE-statement/m-p/590160#M168871</link>
      <description>&lt;P&gt;So&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;has pointed out the error, but it might be helpful to follow certain advice before you try to turn code into code with macro variables and/or code with macros.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The advice is to get the code to work on one or two situations without macros and without macro variables. If your code doesn't work without macros and without macro variables, then it will not work if you are using macros and macro variables. If it does work without macros and without macro variables, then you have a good chance it will work with macros and with macro variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2019 19:21:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variable-defined-in-let-won-t-work-in-WHERE-statement/m-p/590160#M168871</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-09-19T19:21:15Z</dc:date>
    </item>
  </channel>
</rss>

